@HashCodeAndEqualsPlugin.Enhance public class EqualsMethod extends java.lang.Object implements Implementation
Object.equals(Object)
that takes a class's declared fields into consideration. Equality is resolved by comparing two
instances of the same or a compatible class field by field where reference fields must either both be null
or where the field value of
the instance upon which the method is invoked returns true
upon calling the value's equals
method. For arrays, the corresponding
utilities of Arrays
are used.Modifier and Type | Class and Description |
---|---|
protected static class |
EqualsMethod.Appender
A byte code appender to implement the
EqualsMethod . |
protected static class |
EqualsMethod.CompoundComparator
A compound comparator that compares the values of multiple fields.
|
protected static class |
EqualsMethod.ConditionalReturn
A conditional return aborts the equality computation if a given condition was reached.
|
protected static class |
EqualsMethod.NaturalOrderComparator
A comparator that retains the natural order.
|
protected static interface |
EqualsMethod.NullValueGuard
Guards a field value against a potential
null value. |
protected static class |
EqualsMethod.SuperClassCheck
Checks the equality contract against the super class.
|
protected static class |
EqualsMethod.TypeCompatibilityCheck
Checks the overall type of the provided argument.
|
protected static class |
EqualsMethod.TypePropertyComparator
A comparator that sorts fields by a type property.
|
protected static class |
EqualsMethod.ValueComparator
A value comparator is responsible to compare to values of a given type.
|
Implementation.Composable, Implementation.Compound, Implementation.Context, Implementation.Simple, Implementation.SpecialMethodInvocation, Implementation.Target
Modifier and Type | Field and Description |
---|---|
private java.util.Comparator<? super FieldDescription.InDefinedShape> |
comparator
The comparator to apply for ordering fields.
|
private static MethodDescription.InDefinedShape |
EQUALS
The
Object.equals(Object) method. |
private ElementMatcher.Junction<? super FieldDescription.InDefinedShape> |
ignored
A matcher to filter fields that should not be used for a equality resolution.
|
private ElementMatcher.Junction<? super FieldDescription.InDefinedShape> |
nonNullable
A matcher to determine fields of a reference type that cannot be
null . |
private EqualsMethod.SuperClassCheck |
superClassCheck
The baseline equality to check.
|
private EqualsMethod.TypeCompatibilityCheck |
typeCompatibilityCheck
The instance type compatibility check.
|
Modifier | Constructor and Description |
---|---|
protected |
EqualsMethod(EqualsMethod.SuperClassCheck superClassCheck)
Creates a new equals method implementation.
|
private |
EqualsMethod(EqualsMethod.SuperClassCheck superClassCheck,
EqualsMethod.TypeCompatibilityCheck typeCompatibilityCheck,
ElementMatcher.Junction<? super FieldDescription.InDefinedShape> ignored,
ElementMatcher.Junction<? super FieldDescription.InDefinedShape> nonNullable,
java.util.Comparator<? super FieldDescription.InDefinedShape> comparator)
Creates a new equals method implementation.
|
Modifier and Type | Method and Description |
---|---|
ByteCodeAppender |
appender(Implementation.Target implementationTarget)
Creates a byte code appender that determines the implementation of the instrumented type's methods.
|
static EqualsMethod |
isolated()
Creates an equals method implementation that does not invoke the super class's
Object.equals(Object) method. |
InstrumentedType |
prepare(InstrumentedType instrumentedType)
Prepares a given instrumented type.
|
static EqualsMethod |
requiringSuperClassEquality()
Creates an equals implementation that invokes the super class's
Object.equals(Object) method first. |
EqualsMethod |
withEnumerationTypedFieldsFirst()
Returns a new version of this equals method that compares fields with enumeration types prior to fields with non-enumeration types.
|
EqualsMethod |
withFieldOrder(java.util.Comparator<? super FieldDescription.InDefinedShape> comparator)
Applies the supplied comparator to determine an order for fields for being compared.
|
EqualsMethod |
withIgnoredFields(ElementMatcher<? super FieldDescription.InDefinedShape> ignored)
Returns a new version of this equals method implementation that ignores the specified fields additionally to any
previously specified fields.
|
EqualsMethod |
withNonNullableFields(ElementMatcher<? super FieldDescription.InDefinedShape> nonNullable)
Returns a new version of this equals method implementation that does not apply a
null value check for the specified fields
if they have a reference type additionally to any previously specified fields. |
EqualsMethod |
withPrimitiveTypedFieldsFirst()
Returns a new version of this equals method that compares fields with primitive types prior to fields with non-primitive types.
|
EqualsMethod |
withPrimitiveWrapperTypedFieldsFirst()
Returns a new version of this equals method that compares fields with primitive wrapper types prior to fields with non-primitive wrapper types.
|
EqualsMethod |
withStringTypedFieldsFirst()
Returns a new version of this equals method that compares fields with
String types prior to fields with non-String types. |
Implementation |
withSubclassEquality()
Returns a new version of this equals method implementation that permits subclasses of the instrumented type to be equal to instances
of the instrumented type instead of requiring an exact match.
|
private static final MethodDescription.InDefinedShape EQUALS
Object.equals(Object)
method.private final EqualsMethod.SuperClassCheck superClassCheck
private final EqualsMethod.TypeCompatibilityCheck typeCompatibilityCheck
private final ElementMatcher.Junction<? super FieldDescription.InDefinedShape> ignored
private final ElementMatcher.Junction<? super FieldDescription.InDefinedShape> nonNullable
null
.private final java.util.Comparator<? super FieldDescription.InDefinedShape> comparator
protected EqualsMethod(EqualsMethod.SuperClassCheck superClassCheck)
superClassCheck
- The baseline equality to check.private EqualsMethod(EqualsMethod.SuperClassCheck superClassCheck, EqualsMethod.TypeCompatibilityCheck typeCompatibilityCheck, ElementMatcher.Junction<? super FieldDescription.InDefinedShape> ignored, ElementMatcher.Junction<? super FieldDescription.InDefinedShape> nonNullable, java.util.Comparator<? super FieldDescription.InDefinedShape> comparator)
superClassCheck
- The baseline equality to check.typeCompatibilityCheck
- The instance type compatibility check.ignored
- A matcher to filter fields that should not be used for a equality resolution.nonNullable
- A matcher to determine fields of a reference type that cannot be null
.comparator
- The comparator to apply for ordering fields.public static EqualsMethod requiringSuperClassEquality()
Object.equals(Object)
method first.Object.equals(Object)
method first.public static EqualsMethod isolated()
Object.equals(Object)
method.Object.equals(Object)
method.public EqualsMethod withIgnoredFields(ElementMatcher<? super FieldDescription.InDefinedShape> ignored)
ignored
- A matcher to specify any fields that should be ignored.public EqualsMethod withNonNullableFields(ElementMatcher<? super FieldDescription.InDefinedShape> nonNullable)
null
value check for the specified fields
if they have a reference type additionally to any previously specified fields.nonNullable
- A matcher to specify any fields that should not be guarded against null
values.null
value checks to any fields matched by
the provided matcher.public EqualsMethod withPrimitiveTypedFieldsFirst()
public EqualsMethod withEnumerationTypedFieldsFirst()
public EqualsMethod withPrimitiveWrapperTypedFieldsFirst()
public EqualsMethod withStringTypedFieldsFirst()
String
types prior to fields with non-String
types.String
-typed fields before fields with non-String
-typed fields.public EqualsMethod withFieldOrder(java.util.Comparator<? super FieldDescription.InDefinedShape> comparator)
comparator
- The comparator to apply.public Implementation withSubclassEquality()
public InstrumentedType prepare(InstrumentedType instrumentedType)
prepare
in interface InstrumentedType.Prepareable
instrumentedType
- The instrumented type in its current form.public ByteCodeAppender appender(Implementation.Target implementationTarget)
appender
in interface Implementation
implementationTarget
- The target of the current implementation.InstrumentedType.Prepareable.prepare(InstrumentedType)
.