Package net.bytebuddy.implementation
Class HashCodeMethod
java.lang.Object
net.bytebuddy.implementation.HashCodeMethod
- All Implemented Interfaces:
InstrumentedType.Prepareable
,Implementation
An implementation of
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
A byte code appender to implement a hash code method.protected static interface
A guard againstnull
values for fields with reference types.protected static interface
An offset provider is responsible for supplying the initial hash code.protected static enum
A value transformer that is responsible for resolving a field value to anint
value.Nested classes/interfaces inherited from interface net.bytebuddy.implementation.Implementation
Implementation.Composable, Implementation.Compound, Implementation.Context, Implementation.Simple, Implementation.SpecialMethodInvocation, Implementation.Target
Nested classes/interfaces inherited from interface net.bytebuddy.dynamic.scaffold.InstrumentedType.Prepareable
InstrumentedType.Prepareable.NoOp
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
The default multiplier for each value before adding a field's hash code value which should be a prime number.private static final int
The default offset which should be a prime number.private static final MethodDescription.InDefinedShape
TheObject.getClass()
method.private static final MethodDescription.InDefinedShape
TheObject.hashCode()
method.private final ElementMatcher.Junction
<? super FieldDescription.InDefinedShape> A matcher to determine that a field should be considered by its identity.private final ElementMatcher.Junction
<? super FieldDescription.InDefinedShape> A matcher to filter fields that should not be used for a hash codes computation.private final int
A multiplier for each value before adding a field's hash code value.private final ElementMatcher.Junction
<? super FieldDescription.InDefinedShape> A matcher to determine fields of a reference type that cannot benull
.private final HashCodeMethod.OffsetProvider
The hash code's offset provider. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
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, ElementMatcher.Junction<? super FieldDescription.InDefinedShape> identity) Creates a new hash code method implementation. -
Method Summary
Modifier and TypeMethodDescriptionappender
(Implementation.Target implementationTarget) Creates a byte code appender that determines the implementation of the instrumented type's methods.prepare
(InstrumentedType instrumentedType) Prepares a given instrumented type.static HashCodeMethod
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
Creates a hash code method implementation that bases the hash code on the instrumented type's super class's hash code value.static HashCodeMethod
usingTypeHashOffset
(boolean dynamic) Creates a hash code method implementation that bases the hash code on the instrumented type's class constant's hash code..withIdentityFields
(ElementMatcher<? super FieldDescription.InDefinedShape> identity) Returns a new version of this hash code method implementation that considers the matched fields by their identity.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.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.withNonNullableFields
(ElementMatcher<? super FieldDescription.InDefinedShape> nonNullable) Returns a new version of this hash code method implementation that does not apply anull
value check for the specified fields if they have a reference type additionally to any previously specified fields.
-
Field Details
-
DEFAULT_OFFSET
private static final int DEFAULT_OFFSETThe default offset which should be a prime number.- See Also:
-
DEFAULT_MULTIPLIER
private static final int DEFAULT_MULTIPLIERThe default multiplier for each value before adding a field's hash code value which should be a prime number.- See Also:
-
HASH_CODE
TheObject.hashCode()
method. -
GET_CLASS
TheObject.getClass()
method. -
offsetProvider
The hash code's offset provider. -
multiplier
private final int multiplierA multiplier for each value before adding a field's hash code value. -
ignored
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 benull
. -
identity
A matcher to determine that a field should be considered by its identity.
-
-
Constructor Details
-
HashCodeMethod
Creates a new hash code method implementation.- Parameters:
offsetProvider
- The hash code's offset provider.
-
HashCodeMethod
private HashCodeMethod(HashCodeMethod.OffsetProvider offsetProvider, int multiplier, ElementMatcher.Junction<? super FieldDescription.InDefinedShape> ignored, ElementMatcher.Junction<? super FieldDescription.InDefinedShape> nonNullable, ElementMatcher.Junction<? super FieldDescription.InDefinedShape> identity) Creates a new hash code method implementation.- Parameters:
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 benull
.identity
- A matcher to determine that a field should be considered by its identity.
-
-
Method Details
-
usingSuperClassOffset
Creates a hash code method implementation that bases the hash code on the instrumented type's super class's hash code value.- Returns:
- A hash code method implementation that bases the hash code on the instrumented type's super class's hash code value.
-
usingTypeHashOffset
Creates a hash code method implementation that bases the hash code on the instrumented type's class constant's hash code..- Parameters:
dynamic
-true
if the type should be resolved from the instance and not be set as the declaring class.- Returns:
- A hash code method implementation that bases the hash code on the instrumented type's class constant's hash code.
-
usingDefaultOffset
Creates a hash code method implementation that bases the hash code on a fixed value.- Returns:
- A hash code method implementation that bases the hash code on a fixed value.
-
usingOffset
Creates a hash code method implementation that bases the hash code on a fixed value.- Parameters:
value
- The fixed value.- Returns:
- A hash code method implementation that bases the hash code on a fixed value.
-
withIgnoredFields
public 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.- Parameters:
ignored
- A matcher to specify any fields that should be ignored.- Returns:
- A new version of this hash code method implementation that also ignores any fields matched by the provided matcher.
-
withNonNullableFields
public HashCodeMethod withNonNullableFields(ElementMatcher<? super FieldDescription.InDefinedShape> nonNullable) Returns a new version of this hash code method implementation that does not apply anull
value 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 againstnull
values.- Returns:
- A new version of this hash code method implementation that also does not apply
null
value checks to any fields matched by the provided matcher.
-
withIdentityFields
public HashCodeMethod withIdentityFields(ElementMatcher<? super FieldDescription.InDefinedShape> identity) Returns a new version of this hash code method implementation that considers the matched fields by their identity.- Parameters:
identity
- A matcher to specify any fields that should be considered by their identity.- Returns:
- A new version of this hash code method implementation that also considers the matched fields by their identity.
-
withMultiplier
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.- Parameters:
multiplier
- The multiplier to use for any hash code before adding any field's hash code.- 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.
-
prepare
Prepares a given instrumented type.- Specified by:
prepare
in interfaceInstrumentedType.Prepareable
- Parameters:
instrumentedType
- The instrumented type in its current form.- Returns:
- The prepared instrumented type.
-
appender
Creates a byte code appender that determines the implementation of the instrumented type's methods.- Specified by:
appender
in 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)
.
-