Package net.bytebuddy.build
Class HashCodeAndEqualsPlugin
- java.lang.Object
-
- net.bytebuddy.build.HashCodeAndEqualsPlugin
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,Plugin
,Plugin.Factory
,MethodAttributeAppender
,MethodAttributeAppender.Factory
,ElementMatcher<TypeDescription>
- Direct Known Subclasses:
HashCodeAndEqualsPlugin.WithNonNullableFields
@Enhance public class HashCodeAndEqualsPlugin extends java.lang.Object implements Plugin, Plugin.Factory, MethodAttributeAppender.Factory, MethodAttributeAppender
A build tool plugin that addsObject.hashCode()
andObject.equals(Object)
methods to a class if theHashCodeAndEqualsPlugin.Enhance
annotation is present and no explicit method declaration was added. This plugin does not need to be closed.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
HashCodeAndEqualsPlugin.AnnotationOrderComparator
A comparator that arranges fields in the order ofHashCodeAndEqualsPlugin.Sorted
.static interface
HashCodeAndEqualsPlugin.Enhance
Instructs theHashCodeAndEqualsPlugin
to generateObject.hashCode()
andObject.equals(Object)
for the annotated class unless these methods are already declared explicitly.static interface
HashCodeAndEqualsPlugin.Identity
Indicates that a field should be compared by identity.static interface
HashCodeAndEqualsPlugin.Sorted
Determines the sort order of fields for the equality check when implementing theObject.equals(Object)
method.static interface
HashCodeAndEqualsPlugin.ValueHandling
Determines how a field should be used within generated hash code and equality methods.protected static class
HashCodeAndEqualsPlugin.ValueMatcher
An element matcher for aHashCodeAndEqualsPlugin.ValueHandling
annotation.static class
HashCodeAndEqualsPlugin.WithNonNullableFields
A version of theHashCodeAndEqualsPlugin
that assumes that all fields are non-nullable unless they are explicitly marked.-
Nested classes/interfaces inherited from interface net.bytebuddy.matcher.ElementMatcher
ElementMatcher.Junction<S>
-
Nested classes/interfaces inherited from interface net.bytebuddy.implementation.attribute.MethodAttributeAppender
MethodAttributeAppender.Compound, MethodAttributeAppender.Explicit, MethodAttributeAppender.Factory, MethodAttributeAppender.ForInstrumentedMethod, MethodAttributeAppender.ForReceiverType, MethodAttributeAppender.NoOp
-
Nested classes/interfaces inherited from interface net.bytebuddy.implementation.attribute.MethodAttributeAppender.Factory
MethodAttributeAppender.Factory.Compound
-
Nested classes/interfaces inherited from interface net.bytebuddy.build.Plugin
Plugin.Engine, Plugin.Factory, Plugin.ForElementMatcher, Plugin.NoOp, Plugin.WithInitialization, Plugin.WithPreprocessor
-
Nested classes/interfaces inherited from interface net.bytebuddy.build.Plugin.Factory
Plugin.Factory.Simple, Plugin.Factory.UsingReflection
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description HashCodeAndEqualsPlugin()
Creates a new hash code equals plugin.HashCodeAndEqualsPlugin(java.lang.String annotationType)
Creates a new hash code equals plugin.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DynamicType.Builder<?>
apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator)
Applies this plugin.void
apply(org.objectweb.asm.MethodVisitor methodVisitor, MethodDescription methodDescription, AnnotationValueFilter annotationValueFilter)
Applies this attribute appender to a given method visitor.void
close()
Plugin
make()
Returns a plugin that can be used for a transformation and which is subsequently closed.MethodAttributeAppender
make(TypeDescription typeDescription)
Returns a method attribute appender that is applicable for a given type description.boolean
matches(TypeDescription target)
Matches a target against this element matcher.protected ElementMatcher<FieldDescription>
nonNullable(ElementMatcher<FieldDescription> matcher)
Resolves the matcher to identify non-nullable fields.
-
-
-
Field Detail
-
ENHANCE_INVOKE_SUPER
private static final MethodDescription.InDefinedShape ENHANCE_INVOKE_SUPER
A description of theHashCodeAndEqualsPlugin.Enhance.invokeSuper()
method.
-
ENHANCE_SIMPLE_COMPARISON_FIRST
private static final MethodDescription.InDefinedShape ENHANCE_SIMPLE_COMPARISON_FIRST
A description of theHashCodeAndEqualsPlugin.Enhance.simpleComparisonsFirst()
method.
-
ENHANCE_INCLUDE_SYNTHETIC_FIELDS
private static final MethodDescription.InDefinedShape ENHANCE_INCLUDE_SYNTHETIC_FIELDS
A description of theHashCodeAndEqualsPlugin.Enhance.includeSyntheticFields()
method.
-
ENHANCE_PERMIT_SUBCLASS_EQUALITY
private static final MethodDescription.InDefinedShape ENHANCE_PERMIT_SUBCLASS_EQUALITY
A description of theHashCodeAndEqualsPlugin.Enhance.permitSubclassEquality()
method.
-
ENHANCE_USE_TYPE_HASH_CONSTANT
private static final MethodDescription.InDefinedShape ENHANCE_USE_TYPE_HASH_CONSTANT
A description of theHashCodeAndEqualsPlugin.Enhance.useTypeHashConstant()
method.
-
VALUE_HANDLING_VALUE
private static final MethodDescription.InDefinedShape VALUE_HANDLING_VALUE
A description of theHashCodeAndEqualsPlugin.ValueHandling.value()
method.
-
SORTED_VALUE
private static final MethodDescription.InDefinedShape SORTED_VALUE
A description of theHashCodeAndEqualsPlugin.Sorted.value()
method.
-
annotationType
@MaybeNull @ValueHandling(REVERSE_NULLABILITY) private final java.lang.String annotationType
Defines the binary name of a runtime-visible annotation type that should be added to the parameter of theObject.equals(Object)
method, ornull
if no such name should be defined.
-
-
Constructor Detail
-
HashCodeAndEqualsPlugin
public HashCodeAndEqualsPlugin()
Creates a new hash code equals plugin.
-
HashCodeAndEqualsPlugin
public HashCodeAndEqualsPlugin(@MaybeNull java.lang.String annotationType)
Creates a new hash code equals plugin.- Parameters:
annotationType
- Defines the binary name of a runtime-visible annotation type that should be added to the parameter of theObject.equals(Object)
method, ornull
if no such name should be defined.
-
-
Method Detail
-
make
public Plugin make()
Returns a plugin that can be used for a transformation and which is subsequently closed.- Specified by:
make
in interfacePlugin.Factory
- Returns:
- The plugin to use for type transformations.
-
matches
public boolean matches(@MaybeNull TypeDescription target)
Matches a target against this element matcher.- Specified by:
matches
in interfaceElementMatcher<TypeDescription>
- Parameters:
target
- The instance to be matched ornull
.- Returns:
true
if the given element is matched by this matcher orfalse
otherwise.
-
apply
public DynamicType.Builder<?> apply(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassFileLocator classFileLocator)
Applies this plugin.- Specified by:
apply
in interfacePlugin
- Parameters:
builder
- The builder to use as a basis for the applied transformation.typeDescription
- The type being transformed.classFileLocator
- A class file locator that can locate other types in the scope of the project.- Returns:
- The supplied builder with additional transformations registered.
-
nonNullable
protected ElementMatcher<FieldDescription> nonNullable(ElementMatcher<FieldDescription> matcher)
Resolves the matcher to identify non-nullable fields.- Parameters:
matcher
- The matcher that identifies fields that are either nullable or non-nullable.- Returns:
- The actual matcher to identify non-nullable fields.
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
-
make
public MethodAttributeAppender make(TypeDescription typeDescription)
Returns a method attribute appender that is applicable for a given type description.- Specified by:
make
in interfaceMethodAttributeAppender.Factory
- Parameters:
typeDescription
- The type for which a method attribute appender is to be applied for.- Returns:
- The method attribute appender which should be applied for the given type.
-
apply
public void apply(org.objectweb.asm.MethodVisitor methodVisitor, MethodDescription methodDescription, AnnotationValueFilter annotationValueFilter)
Applies this attribute appender to a given method visitor.- Specified by:
apply
in interfaceMethodAttributeAppender
- Parameters:
methodVisitor
- The method visitor to which the attributes that are represented by this attribute appender are written to.methodDescription
- The description of the method for which the given method visitor creates an instrumentation for.annotationValueFilter
- The annotation value filter to apply when the annotations are written.
-
-