Package net.bytebuddy.implementation
Class EqualsMethod
- java.lang.Object
-
- net.bytebuddy.implementation.EqualsMethod
-
- All Implemented Interfaces:
InstrumentedType.Prepareable,Implementation
@Enhance public class EqualsMethod extends java.lang.Object implements Implementation
An implementation ofObject.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 benullor where the field value of the instance upon which the method is invoked returnstrueupon calling the value'sequalsmethod. For arrays, the corresponding utilities ofArraysare used.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classEqualsMethod.AppenderA byte code appender to implement theEqualsMethod.protected static classEqualsMethod.CompoundComparatorA compound comparator that compares the values of multiple fields.protected static classEqualsMethod.ConditionalReturnA conditional return aborts the equality computation if a given condition was reached.protected static classEqualsMethod.NaturalOrderComparatorA comparator that retains the natural order.protected static interfaceEqualsMethod.NullValueGuardGuards a field value against a potentialnullvalue.protected static classEqualsMethod.SuperClassCheckChecks the equality contract against the super class.protected static classEqualsMethod.TypeCompatibilityCheckChecks the overall type of the provided argument.protected static classEqualsMethod.TypePropertyComparatorA comparator that sorts fields by a type property.protected static classEqualsMethod.ValueComparatorA value comparator is responsible to compare to values of a given type.-
Nested classes/interfaces inherited from interface net.bytebuddy.implementation.Implementation
Implementation.Composable, Implementation.Compound, Implementation.Context, Implementation.Simple, Implementation.SpecialMethodInvocation, Implementation.Target
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Comparator<? super FieldDescription.InDefinedShape>comparatorThe comparator to apply for ordering fields.private static MethodDescription.InDefinedShapeEQUALSTheObject.equals(Object)method.private ElementMatcher.Junction<? super FieldDescription.InDefinedShape>ignoredA matcher to filter fields that should not be used for a equality resolution.private ElementMatcher.Junction<? super FieldDescription.InDefinedShape>nonNullableA matcher to determine fields of a reference type that cannot benull.private EqualsMethod.SuperClassChecksuperClassCheckThe baseline equality to check.private EqualsMethod.TypeCompatibilityChecktypeCompatibilityCheckThe instance type compatibility check.
-
Constructor Summary
Constructors Modifier Constructor Description protectedEqualsMethod(EqualsMethod.SuperClassCheck superClassCheck)Creates a new equals method implementation.privateEqualsMethod(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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ByteCodeAppenderappender(Implementation.Target implementationTarget)Creates a byte code appender that determines the implementation of the instrumented type's methods.static EqualsMethodisolated()Creates an equals method implementation that does not invoke the super class'sObject.equals(Object)method.InstrumentedTypeprepare(InstrumentedType instrumentedType)Prepares a given instrumented type.static EqualsMethodrequiringSuperClassEquality()Creates an equals implementation that invokes the super class'sObject.equals(Object)method first.EqualsMethodwithEnumerationTypedFieldsFirst()Returns a new version of this equals method that compares fields with enumeration types prior to fields with non-enumeration types.EqualsMethodwithFieldOrder(java.util.Comparator<? super FieldDescription.InDefinedShape> comparator)Applies the supplied comparator to determine an order for fields for being compared.EqualsMethodwithIgnoredFields(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.EqualsMethodwithNonNullableFields(ElementMatcher<? super FieldDescription.InDefinedShape> nonNullable)Returns a new version of this equals method implementation that does not apply anullvalue check for the specified fields if they have a reference type additionally to any previously specified fields.EqualsMethodwithPrimitiveTypedFieldsFirst()Returns a new version of this equals method that compares fields with primitive types prior to fields with non-primitive types.EqualsMethodwithPrimitiveWrapperTypedFieldsFirst()Returns a new version of this equals method that compares fields with primitive wrapper types prior to fields with non-primitive wrapper types.EqualsMethodwithStringTypedFieldsFirst()Returns a new version of this equals method that compares fields withStringtypes prior to fields with non-Stringtypes.ImplementationwithSubclassEquality()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.
-
-
-
Field Detail
-
EQUALS
private static final MethodDescription.InDefinedShape EQUALS
TheObject.equals(Object)method.
-
superClassCheck
private final EqualsMethod.SuperClassCheck superClassCheck
The baseline equality to check.
-
typeCompatibilityCheck
private final EqualsMethod.TypeCompatibilityCheck typeCompatibilityCheck
The instance type compatibility check.
-
ignored
private final ElementMatcher.Junction<? super FieldDescription.InDefinedShape> ignored
A matcher to filter fields that should not be used for a equality resolution.
-
nonNullable
private final ElementMatcher.Junction<? super FieldDescription.InDefinedShape> nonNullable
A matcher to determine fields of a reference type that cannot benull.
-
comparator
private final java.util.Comparator<? super FieldDescription.InDefinedShape> comparator
The comparator to apply for ordering fields.
-
-
Constructor Detail
-
EqualsMethod
protected EqualsMethod(EqualsMethod.SuperClassCheck superClassCheck)
Creates a new equals method implementation.- Parameters:
superClassCheck- The baseline equality to check.
-
EqualsMethod
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.- Parameters:
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 benull.comparator- The comparator to apply for ordering fields.
-
-
Method Detail
-
requiringSuperClassEquality
public static EqualsMethod requiringSuperClassEquality()
Creates an equals implementation that invokes the super class'sObject.equals(Object)method first.- Returns:
- An equals implementation that invokes the super class's
Object.equals(Object)method first.
-
isolated
public static EqualsMethod isolated()
Creates an equals method implementation that does not invoke the super class'sObject.equals(Object)method.- Returns:
- An equals method implementation that does not invoke the super class's
Object.equals(Object)method.
-
withIgnoredFields
public 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.- Parameters:
ignored- A matcher to specify any fields that should be ignored.- Returns:
- A new version of this equals method implementation that also ignores any fields matched by the provided matcher.
-
withNonNullableFields
public EqualsMethod withNonNullableFields(ElementMatcher<? super FieldDescription.InDefinedShape> nonNullable)
Returns a new version of this equals method implementation that does not apply anullvalue check for the specified fields if they have a reference type additionally to any previously specified fields.- Parameters:
nonNullable- A matcher to specify any fields that should not be guarded againstnullvalues.- Returns:
- A new version of this equals method implementation that also does not apply
nullvalue checks to any fields matched by the provided matcher.
-
withPrimitiveTypedFieldsFirst
public EqualsMethod withPrimitiveTypedFieldsFirst()
Returns a new version of this equals method that compares fields with primitive types prior to fields with non-primitive types.- Returns:
- A new version of this equals method that compares primitive-typed fields before fields with non-primitive-typed fields.
-
withEnumerationTypedFieldsFirst
public EqualsMethod withEnumerationTypedFieldsFirst()
Returns a new version of this equals method that compares fields with enumeration types prior to fields with non-enumeration types.- Returns:
- A new version of this equals method that compares enumeration-typed fields before fields with non-enumeration-typed fields.
-
withPrimitiveWrapperTypedFieldsFirst
public 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.- Returns:
- A new version of this equals method that compares primitive wrapper-typed fields before fields with non-primitive wrapper-typed fields.
-
withStringTypedFieldsFirst
public EqualsMethod withStringTypedFieldsFirst()
Returns a new version of this equals method that compares fields withStringtypes prior to fields with non-Stringtypes.- Returns:
- A new version of this equals method that compares
String-typed fields before fields with non-String-typed fields.
-
withFieldOrder
public EqualsMethod withFieldOrder(java.util.Comparator<? super FieldDescription.InDefinedShape> comparator)
Applies the supplied comparator to determine an order for fields for being compared. Fields with the lowest sort order are compared first. Any previously defined comparators are applied prior to the supplied comparator.- Parameters:
comparator- The comparator to apply.- Returns:
- A new version of this equals method that sorts fields in their application order using the supplied comparator.
-
withSubclassEquality
public 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.- 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.
-
prepare
public InstrumentedType prepare(InstrumentedType instrumentedType)
Prepares a given instrumented type.- Specified by:
preparein interfaceInstrumentedType.Prepareable- Parameters:
instrumentedType- The instrumented type in its current form.- Returns:
- The prepared instrumented type.
-
appender
public ByteCodeAppender appender(Implementation.Target implementationTarget)
Creates a byte code appender that determines the implementation of the instrumented type's methods.- Specified by:
appenderin interfaceImplementation- Parameters:
implementationTarget- The target of the current implementation.- Returns:
- A byte code appender for implementing methods delegated to this implementation. This byte code appender
is also responsible for handling methods that were added by this implementation on the call to
InstrumentedType.Prepareable.prepare(InstrumentedType).
-
-