Skip to content

Commit 8fbcf8f

Browse files
author
Reinier Zwitserloot
committed
Split off generation of equals() and hashCode() methods form @DaTa into a new annotation, @EqualsAndHashCode.
Addresses issue #8
1 parent beb0213 commit 8fbcf8f

File tree

5 files changed

+1034
-582
lines changed

5 files changed

+1034
-582
lines changed

src/lombok/EqualsAndHashCode.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright © 2009 Reinier Zwitserloot and Roel Spilker.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package lombok;
23+
24+
import java.lang.annotation.ElementType;
25+
import java.lang.annotation.Retention;
26+
import java.lang.annotation.RetentionPolicy;
27+
import java.lang.annotation.Target;
28+
29+
/**
30+
* Generates implementations for the <code>equals</code> and <code>hashCode</code> methods inherited by all objects.
31+
* <p>
32+
* If either method already exists, then <code>&#64;EqualsAndHashCode</code> will not generate that particular method.
33+
* If they all exist, <code>&#64;EqualsAndHashCode</code> generates no methods, and emits a warning instead to highlight
34+
* that its doing nothing at all. The parameter list and return type are not relevant when deciding to skip generation of
35+
* a method; any method named <code>hashCode</code> will make <code>&#64;EqualsAndHashCode</code> not generate that method,
36+
* for example.
37+
* <p>
38+
* All fields that are non-static and non-transient are used in the equality check and hashCode generation. You can exclude
39+
* more fields by specifying them in the <code>exclude</code> parameter.
40+
* <p>
41+
* Normally, auto-generating <code>hashCode</code> and <code>equals</code> implementations in a subclass is a bad idea, as
42+
* the superclass also defines fields, for which equality checks/hashcodes won't be auto-generated. Therefore, a warning
43+
* is emitted when you try. Instead, you can set the <code>callSuper</code> parameter to <em>true</em> which will call
44+
* <code>super.equals</code> and <code>super.hashCode</code>. Doing this with <code>java.lang.Object</code> as superclass is
45+
* pointless, so, conversely, setting this flag when <em>NOT</em> extending something (other than Object) will also generate
46+
* a warning. Be aware that not all implementations of <code>equals</code> correctly handle being called from a subclass!
47+
* Fortunately, lombok-generated <code>equals</code> implementations do correctly handle it.
48+
* <p>
49+
* Array fields are handled by way of {@link java.util.Arrays#deepEquals(Object[], Object[])} where necessary, as well
50+
* as <code>deepHashCode</code>. The downside is that arrays with circular references (arrays that contain themselves,
51+
* possibly indirectly) results in calls to <code>hashCode</code> and <code>equals</code> throwing a
52+
* {@link java.lang.StackOverflowError}. However, the implementations for java's own {@link java.util.ArrayList} suffer
53+
* from the same flaw.
54+
*/
55+
@Target(ElementType.TYPE)
56+
@Retention(RetentionPolicy.SOURCE)
57+
public @interface EqualsAndHashCode {
58+
/**
59+
* Any fields listed here will not be taken into account in the generated
60+
* <code>equals</code> and <code>hashCode</code> implementations.
61+
*/
62+
String[] exclude() default {};
63+
64+
/**
65+
* Call on the superclass's implementations of <code>equals</code> and <code>hashCode</code> before calculating
66+
* for the fields in this class.
67+
*/
68+
boolean callSuper() default false;
69+
}

0 commit comments

Comments
 (0)