mirror of
https://github.com/tiennm99/lombok.git
synced 2026-05-28 08:21:35 +00:00
93 lines
5.6 KiB
HTML
93 lines
5.6 KiB
HTML
<!DOCTYPE html>
|
|
<html><head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<link rel="stylesheet" type="text/css" href="../logi/reset.css" />
|
|
<link rel="stylesheet" type="text/css" href="features.css" />
|
|
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
|
|
<meta name="description" content="Spice up your java" />
|
|
<title>@Getter and @Setter</title>
|
|
</head><body><div id="pepper">
|
|
<div class="minimumHeight"></div>
|
|
<div class="meat">
|
|
<div class="header"><a href="../index.html">Project Lombok</a></div>
|
|
<h1>@Getter and @Setter</h1>
|
|
<div class="byline">Never write <code>public int getFoo() {return foo;}</code> again.</div>
|
|
<div class="overview">
|
|
<h3>Overview</h3>
|
|
<p>
|
|
You can annotate any field with <code>@Getter</code> and/or <code>@Setter</code>, to let lombok generate the default getter/setter automatically.<br />
|
|
A default getter simply returns the field, and is named <code>getFoo</code> if the field is called <code>foo</code> (or <code>isFoo</code>
|
|
if the field's type is <code>boolean</code>). A default setter is named <code>setFoo</code> if the field is called <code>foo</code>, returns <code>void</code>,
|
|
and takes 1 parameter of the same type as the field. It simply sets the field to this value.
|
|
</p><p>
|
|
The generated getter/setter method will be <code>public</code> unless you explicitly specify an <code>AccessLevel</code>, as shown in the example below.
|
|
Legal access levels are <code>PUBLIC</code>, <code>PROTECTED</code>, <code>PACKAGE</code>, and <code>PRIVATE</code>.
|
|
</p><p>
|
|
You can also put a <code>@Getter</code> and/or <code>@Setter</code> annotation on a class. In that case, it's as if you annotate all the non-static fields in that
|
|
class with the annotation.
|
|
</p><p>
|
|
You can always manually disable getter/setter generation for any field by using the special <code>AccessLevel.NONE</code> access level. This lets you override the
|
|
behaviour of a <code>@Getter</code>, <code>@Setter</code> or <code>@Data</code> annotation on a class.
|
|
</p>
|
|
</div>
|
|
<div class="snippets">
|
|
<div class="pre">
|
|
<h3>With Lombok</h3>
|
|
<div class="snippet">@HTML_PRE@</div>
|
|
</div>
|
|
<div class="sep"></div>
|
|
<div class="post">
|
|
<h3>Vanilla Java</h3>
|
|
<div class="snippet">@HTML_POST@</div>
|
|
</div>
|
|
</div>
|
|
<div style="clear: left;"></div>
|
|
<div class="overview">
|
|
<h3>Small print</h3><div class="smallprint">
|
|
<p>
|
|
For generating the method names, the first character of the field, if it is a lowercase character, is title-cased, otherwise, it is left unmodified.
|
|
Then, get/set/is is prefixed.
|
|
</p><p>
|
|
No method is generated if any method already exists with the same name (case insensitive) and same parameter count. For example, <code>getFoo()</code>
|
|
will not be generated if there's already a method <code>getFoo(String... x)</code> even though it is technically possible to make the method. This caveat
|
|
exists to prevent confusion. If the generation of a method is skipped for this reason, a warning is emitted instead. Varargs count as 0 to N parameters.
|
|
</p><p>
|
|
For <code>boolean</code> fields that start with <code>is</code> immediately followed by a title-case letter, nothing is prefixed to generate the getter name.
|
|
</p><p>
|
|
Any variation on <code>boolean</code> will <em>not</em> result in using the <code>is</code> prefix instead of the <code>get</code> prefix; for example,
|
|
returning <code>java.lang.Boolean</code> results in a <code>get</code> prefix, not an <code>is</code> prefix.
|
|
</p><p>
|
|
Any annotations named <code>@NonNull</code> (case insensitive) on the field are interpreted as: This field must not ever hold
|
|
<em>null</em>. Therefore, these annotations result in an explicit null check in the generated setter. Also, these
|
|
annotations (as well as any annotation named <code>@Nullable</code> or <code>@CheckForNull</code>) are copied to setter parameter and getter method.
|
|
</p><p>
|
|
You can annotate a class with a <code>@Getter</code> or <code>@Setter</code> annotation. Doing so is equivalent to annotating all non-static fields
|
|
in that class with that annotation. <code>@Getter</code>/<code>@Setter</code> annotations on fields take precedence over the ones on classes.
|
|
</p><p>
|
|
Using the <code>AccessLevel.NONE</code> access level simply generates nothing. It's useful only in combination with
|
|
<a href="Data.html"><code>@Data</code></a> or a class-wide <code>@Getter</code> or <code>@Setter</code>.
|
|
</p><p>
|
|
<code>@Getter</code> can also be used on enums. <code>@Setter</code> can't, not for a technical reason, but
|
|
for a pragmatic one: Setters on enums are an extremely bad idea.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div class="footer">
|
|
<a href="index.html">Back to features</a> | <span class="disabled">Previous feature</span> | <a href="GetterLazy.html">Next feature (@Getter(lazy=true))</a><br />
|
|
<a href="../credits.html" class="creditsLink">credits</a> | <span class="copyright">Copyright © 2009-2011 The Project Lombok Authors, licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a>.</span>
|
|
</div>
|
|
<div style="clear: both;"></div>
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
|
|
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
|
|
</script>
|
|
<script type="text/javascript">
|
|
try {
|
|
var pageTracker = _gat._getTracker("UA-9884254-1");
|
|
pageTracker._trackPageview();
|
|
} catch(err) {}
|
|
</script>
|
|
</body></html>
|