mirror of
https://github.com/tiennm99/lombok.git
synced 2026-05-27 22:26:17 +00:00
f1124aad02
We've also added any fields that start with $ to the default excludes list. Lombok itself can generate these fields ($lock of @Synchronized, for example), and in general they probably should count as effectively not part of the class.
94 lines
6.1 KiB
HTML
94 lines
6.1 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>@Data</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>@EqualsAndHashCode</h1>
|
|
<div class="byline">Equality made easy: Generates <code>hashCode</code> and <code>equals</code> implementations from the fields of your object.</div>
|
|
<div class="overview">
|
|
<h3>Overview</h3>
|
|
<p>
|
|
Any class definition may be annotated with <code>@EqualsAndHashCode</code> to let lombok generate implementations of the
|
|
<code>equals(Object other)</code> and <code>hashCode()</code> methods. By default, it'll use all non-static, non-transient
|
|
fields, but you can exclude more fields by naming them in the optional <code>exclude</code> parameter to the annotation.
|
|
Alternatively, you can specify exactly which fields you wish to be used by naming them in the <code>of</code> parameter.
|
|
</p><p>
|
|
By setting <code>callSuper</code> to <em>true</em>, you can include the <code>equals</code> and <code>hashCode</code> methods of your superclass in the generated methods.
|
|
For <code>hashCode</code>, the result of <code>super.hashCode()</code> is included in the hash algorithm, and for <code>equals</code>, the generated method will return
|
|
false if the super implementation thinks it is not equal to the passed in object. Be aware that not all <code>equals</code> implementations handle this situation properly.
|
|
However, lombok-generated <code>equals</code> implementations <strong>do</strong> handle this situation properly, so you can safely call your superclass equals if it, too,
|
|
has a lombok-generated <code>equals</code> method.<br />
|
|
</p><p>
|
|
Setting <code>callSuper</code> to <em>true</em> when you don't extend anything (you extend <code>java.lang.Object</code>) is a compile-time error, because it would turn
|
|
the generated <code>equals()</code> and <code>hashCode()</code> implementations into having the same behaviour as simply inheriting these methods from <code>java.lang.Object</code>:
|
|
only the same object will be equal to each other and will have the same hashCode. Obviously, inheriting <code>java.lang.Object</code> is the right strategy if you want this behaviour.
|
|
Not setting <code>callSuper</code> to <em>true</em> when you extend another class generates a warning, because unless the superclass has no (equality-important) fields, lombok
|
|
cannot generate an implementation for you that takes into account the fields declared by your superclasses. You'll need to write your own implementations, or rely on the
|
|
<code>callSuper</code> chaining facility.
|
|
</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>
|
|
Arrays are 'deep' compared/hashCoded, which means that arrays that contain themselves will result in <code>StackOverflowError</code>s. However,
|
|
this behaviour is no different from e.g. <code>ArrayList</code>.
|
|
</p><p>
|
|
You may safely presume that the hashCode implementation used will not change between versions of lombok, however this guarantee is not set in stone;
|
|
if there's a significant performance improvement to be gained from using an alternate hash algorithm, that will be substituted in a future version.
|
|
</p><p>
|
|
For the purposes of equality, 2 <code>NaN</code> (not a number) values for floats and doubles are considered equal, eventhough 'NaN == NaN' would
|
|
return false. This is analogous to <code>java.lang.Double</code>'s equals method, and is in fact required to ensure that comparing an object
|
|
to an exact copy of itself returns <code>true</code> for equality.
|
|
</p><p>
|
|
If there is <em>any</em> method named either <code>hashCode</code> or <code>equals</code>, regardless of parameters or return type,
|
|
that method will not be generated, and a warning is emitted instead. <code>hashCode</code> and <code>equals</code> need to be in sync with
|
|
each other, which lombok cannot guarantee if it is only generating one of the two methods, hence you always get a warning if one <em>or</em> both
|
|
of the methods already exist.
|
|
</p><p>
|
|
Attempting to exclude fields that don't exist or would have been excluded anyway (because they are static or transient) results in warnings on the named fields.
|
|
You therefore don't have to worry about typos.
|
|
</p><p>
|
|
Having both <code>exclude</code> and <code>of</code> generates a warning; the <code>exclude</code> parameter will be ignored in that case.
|
|
</p><p>
|
|
By default, any variables that start with a $ symbol are excluded automatically. You can only include them by using the 'of' parameter.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div class="footer">
|
|
<a href="index.html">Back to features</a> | <a href="ToString.html">Previous feature (@ToString)</a> | <a href="Data.html">Next feature (@Data)</a><br />
|
|
<a href="../credits.html" class="creditsLink">credits</a> | <span class="copyright">Copyright © 2009 Reinier Zwitserloot and Roel Spilker, 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>
|