@HashCodeAndEqualsPlugin.Enhance public class HashCodeMethod extends java.lang.Object implements Implementation
Object.hashCode()
that takes a class's declared fields into consideration. A hash code is computed by transforming
primitive field types to an int
value and by summing those values up starting from a given offset after multiplying any previous value
with a multiplier. Reference values are checked against null
values unless specified otherwise.Modifier and Type | Class and Description |
---|---|
protected static class |
HashCodeMethod.Appender
A byte code appender to implement a hash code method.
|
protected static interface |
HashCodeMethod.NullValueGuard
A guard against
null values for fields with reference types. |
protected static interface |
HashCodeMethod.OffsetProvider
An offset provider is responsible for supplying the initial hash code.
|
protected static class |
HashCodeMethod.ValueTransformer
A value transformer that is responsible for resolving a field value to an
int value. |
Implementation.Composable, Implementation.Compound, Implementation.Context, Implementation.Simple, Implementation.SpecialMethodInvocation, Implementation.Target
Modifier and Type | Field and Description |
---|---|
private static int |
DEFAULT_MULTIPLIER
The default multiplier for each value before adding a field's hash code value which should be a prime number.
|
private static int |
DEFAULT_OFFSET
The default offset which should be a prime number.
|
private static MethodDescription.InDefinedShape |
HASH_CODE
The
Object.hashCode() method. |
private ElementMatcher.Junction<? super FieldDescription.InDefinedShape> |
ignored
A matcher to filter fields that should not be used for a hash codes computation.
|
private int |
multiplier
A multiplier for each value before adding a field's hash code value.
|
private ElementMatcher.Junction<? super FieldDescription.InDefinedShape> |
nonNullable
A matcher to determine fields of a reference type that cannot be
null . |
private HashCodeMethod.OffsetProvider |
offsetProvider
The hash code's offset provider.
|
Modifier | Constructor and Description |
---|---|
protected |
HashCodeMethod(HashCodeMethod.OffsetProvider offsetProvider)
Creates a new hash code method implementation.
|
private |
HashCodeMethod(HashCodeMethod.OffsetProvider offsetProvider,
int multiplier,
ElementMatcher.Junction<? super FieldDescription.InDefinedShape> ignored,
ElementMatcher.Junction<? super FieldDescription.InDefinedShape> nonNullable)
Creates a new hash code 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.
|
InstrumentedType |
prepare(InstrumentedType instrumentedType)
Prepares a given instrumented type.
|
static HashCodeMethod |
usingDefaultOffset()
Creates a hash code method implementation that bases the hash code on a fixed value.
|
static HashCodeMethod |
usingOffset(int value)
Creates a hash code method implementation that bases the hash code on a fixed value.
|
static HashCodeMethod |
usingSuperClassOffset()
Creates a hash code method implementation that bases the hash code on the instrumented type's super class's hash code value.
|
HashCodeMethod |
withIgnoredFields(ElementMatcher<? super FieldDescription.InDefinedShape> ignored)
Returns a new version of this hash code method implementation that ignores the specified fields additionally to any
previously specified fields.
|
Implementation |
withMultiplier(int multiplier)
Returns a new version of this hash code method implementation that uses the given multiplier onto any given hash code before adding a
field's hash code.
|
HashCodeMethod |
withNonNullableFields(ElementMatcher<? super FieldDescription.InDefinedShape> nonNullable)
Returns a new version of this hash code 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. |
private static final int DEFAULT_OFFSET
private static final int DEFAULT_MULTIPLIER
private static final MethodDescription.InDefinedShape HASH_CODE
Object.hashCode()
method.private final HashCodeMethod.OffsetProvider offsetProvider
private final int multiplier
private final ElementMatcher.Junction<? super FieldDescription.InDefinedShape> ignored
private final ElementMatcher.Junction<? super FieldDescription.InDefinedShape> nonNullable
null
.protected HashCodeMethod(HashCodeMethod.OffsetProvider offsetProvider)
offsetProvider
- The hash code's offset provider.private HashCodeMethod(HashCodeMethod.OffsetProvider offsetProvider, int multiplier, ElementMatcher.Junction<? super FieldDescription.InDefinedShape> ignored, ElementMatcher.Junction<? super FieldDescription.InDefinedShape> nonNullable)
offsetProvider
- The hash code's offset provider.multiplier
- A multiplier for each value before adding a field's hash code valueignored
- A matcher to filter fields that should not be used for a hash codes computation.nonNullable
- A matcher to determine fields of a reference type that cannot be null
.public static HashCodeMethod usingSuperClassOffset()
public static HashCodeMethod usingDefaultOffset()
public static HashCodeMethod usingOffset(int value)
value
- The fixed value.public HashCodeMethod withIgnoredFields(ElementMatcher<? super FieldDescription.InDefinedShape> ignored)
ignored
- A matcher to specify any fields that should be ignored.public HashCodeMethod 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 Implementation withMultiplier(int multiplier)
multiplier
- The multiplier to use for any hash code before adding any field's hash code.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)
.