Package net.bytebuddy.implementation
Class HashCodeMethod
- java.lang.Object
-
- net.bytebuddy.implementation.HashCodeMethod
-
- All Implemented Interfaces:
InstrumentedType.Prepareable,Implementation
public class HashCodeMethod extends java.lang.Object implements Implementation
An implementation ofObject.hashCode()that takes a class's declared fields into consideration. A hash code is computed by transforming primitive field types to anintvalue and by summing those values up starting from a given offset after multiplying any previous value with a multiplier. Reference values are checked againstnullvalues unless specified otherwise.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classHashCodeMethod.AppenderA byte code appender to implement a hash code method.protected static interfaceHashCodeMethod.NullValueGuardA guard againstnullvalues for fields with reference types.protected static interfaceHashCodeMethod.OffsetProviderAn offset provider is responsible for supplying the initial hash code.protected static classHashCodeMethod.ValueTransformerA value transformer that is responsible for resolving a field value to anintvalue.-
Nested classes/interfaces inherited from interface net.bytebuddy.implementation.Implementation
Implementation.Composable, Implementation.Compound, Implementation.Context, Implementation.Simple, Implementation.SpecialMethodInvocation, Implementation.Target
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedHashCodeMethod(HashCodeMethod.OffsetProvider offsetProvider)Creates a new hash code 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.InstrumentedTypeprepare(InstrumentedType instrumentedType)Prepares a given instrumented type.static HashCodeMethodusingDefaultOffset()Creates a hash code method implementation that bases the hash code on a fixed value.static HashCodeMethodusingOffset(int value)Creates a hash code method implementation that bases the hash code on a fixed value.static HashCodeMethodusingSuperClassOffset()Creates a hash code method implementation that bases the hash code on the instrumented type's super class's hash code value.HashCodeMethodwithIgnoredFields(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.ImplementationwithMultiplier(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.HashCodeMethodwithNonNullableFields(ElementMatcher<? super FieldDescription.InDefinedShape> nonNullable)Returns a new version of this hash code method implementation that does not apply anullvalue check for the specified fields if they have a reference type additionally to any previously specified fields.
-
-
-
Constructor Detail
-
HashCodeMethod
protected HashCodeMethod(HashCodeMethod.OffsetProvider offsetProvider)
Creates a new hash code method implementation.- Parameters:
offsetProvider- The hash code's offset provider.
-
-
Method Detail
-
usingSuperClassOffset
public 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.- Returns:
- A hash code method implementation that bases the hash code on the instrumented type's super class's hash code value.
-
usingDefaultOffset
public static HashCodeMethod 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
public static HashCodeMethod usingOffset(int value)
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 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 hash code method implementation that also does not apply
nullvalue checks to any fields matched by the provided matcher.
-
withMultiplier
public 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.- 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
public InstrumentedType prepare(InstrumentedType instrumentedType)
Description copied from interface:InstrumentedType.PrepareablePrepares 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)
Description copied from interface:ImplementationCreates 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).
-
-