From ddcb0bb0e67ca2a927a1a725e66efacda5adb1a7 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Tue, 13 Nov 2018 22:52:58 +0000 Subject: [PATCH] HHH-13100 All custom implementation of Byte Buddy "Implementation" s should have a proper equals and hashcode --- .../BiDirectionalAssociationHandler.java | 24 ++++++++++++++++- .../bytebuddy/FieldAccessEnhancer.java | 24 ++++++++++++++++- .../bytebuddy/FieldReaderAppender.java | 22 ++++++++++++++++ .../bytebuddy/InlineDirtyCheckingHandler.java | 21 ++++++++++++++- .../PersistentAttributeTransformer.java | 26 ++++++++++++++++++- 5 files changed, 113 insertions(+), 4 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/BiDirectionalAssociationHandler.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/BiDirectionalAssociationHandler.java index 8792cac4a1..63af04277e 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/BiDirectionalAssociationHandler.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/BiDirectionalAssociationHandler.java @@ -8,6 +8,8 @@ package org.hibernate.bytecode.enhance.internal.bytebuddy; import java.util.Collection; import java.util.Map; +import java.util.Objects; + import javax.persistence.Access; import javax.persistence.AccessType; import javax.persistence.ManyToMany; @@ -34,7 +36,7 @@ import net.bytebuddy.jar.asm.MethodVisitor; import net.bytebuddy.jar.asm.Opcodes; import net.bytebuddy.jar.asm.Type; -class BiDirectionalAssociationHandler implements Implementation { +final class BiDirectionalAssociationHandler implements Implementation { private static final CoreMessageLogger log = CoreLogging.messageLogger( BiDirectionalAssociationHandler.class ); @@ -327,4 +329,24 @@ class BiDirectionalAssociationHandler implements Implementation { }, implementationContext, instrumentedMethod ); } } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if ( o == null || BiDirectionalAssociationHandler.class != o.getClass() ) { + return false; + } + final BiDirectionalAssociationHandler that = (BiDirectionalAssociationHandler) o; + return Objects.equals( delegate, that.delegate ) && + Objects.equals( targetEntity, that.targetEntity ) && + Objects.equals( targetType, that.targetType ) && + Objects.equals( mappedBy, that.mappedBy ); + } + + @Override + public int hashCode() { + return Objects.hash( delegate, targetEntity, targetType, mappedBy ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/FieldAccessEnhancer.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/FieldAccessEnhancer.java index f543f6f870..796d324102 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/FieldAccessEnhancer.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/FieldAccessEnhancer.java @@ -28,7 +28,9 @@ import net.bytebuddy.pool.TypePool; import static net.bytebuddy.matcher.ElementMatchers.hasDescriptor; import static net.bytebuddy.matcher.ElementMatchers.named; -class FieldAccessEnhancer implements AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper { +import java.util.Objects; + +final class FieldAccessEnhancer implements AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper { private static final CoreMessageLogger log = CoreLogging.messageLogger( FieldAccessEnhancer.class ); @@ -130,4 +132,24 @@ class FieldAccessEnhancer implements AsmVisitorWrapper.ForDeclaredMethods.Method } return fields.getOnly(); } + + @Override + public boolean equals(final Object o) { + if ( this == o ) { + return true; + } + if ( o == null || FieldAccessEnhancer.class != o.getClass() ) { + return false; + } + final FieldAccessEnhancer that = (FieldAccessEnhancer) o; + return Objects.equals( managedCtClass, that.managedCtClass ) && + Objects.equals( enhancementContext, that.enhancementContext ) && + Objects.equals( classPool, that.classPool ); + } + + @Override + public int hashCode() { + return Objects.hash( managedCtClass, enhancementContext, classPool ); + } + } diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/FieldReaderAppender.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/FieldReaderAppender.java index 5098c04290..9163f91c1d 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/FieldReaderAppender.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/FieldReaderAppender.java @@ -6,6 +6,8 @@ */ package org.hibernate.bytecode.enhance.internal.bytebuddy; +import java.util.Objects; + import org.hibernate.bytecode.enhance.spi.EnhancerConstants; import org.hibernate.engine.spi.PersistentAttributeInterceptor; @@ -170,4 +172,24 @@ abstract class FieldReaderAppender implements ByteCodeAppender { ); } } + + @Override + public boolean equals(final Object o) { + if ( this == o ) { + return true; + } + if ( o == null || getClass() != o.getClass() ) { + return false; + } + final FieldReaderAppender that = (FieldReaderAppender) o; + return Objects.equals( managedCtClass, that.managedCtClass ) && + Objects.equals( persistentField, that.persistentField ) && + Objects.equals( persistentFieldAsDefined, that.persistentFieldAsDefined ); + } + + @Override + public int hashCode() { + return Objects.hash( managedCtClass, persistentField, persistentFieldAsDefined ); + } + } diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/InlineDirtyCheckingHandler.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/InlineDirtyCheckingHandler.java index 28b3a83480..e08cb52318 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/InlineDirtyCheckingHandler.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/InlineDirtyCheckingHandler.java @@ -27,7 +27,7 @@ import net.bytebuddy.jar.asm.MethodVisitor; import net.bytebuddy.jar.asm.Opcodes; import net.bytebuddy.jar.asm.Type; -class InlineDirtyCheckingHandler implements Implementation, ByteCodeAppender { +final class InlineDirtyCheckingHandler implements Implementation, ByteCodeAppender { private final Implementation delegate; @@ -151,4 +151,23 @@ class InlineDirtyCheckingHandler implements Implementation, ByteCodeAppender { } return new Size( 1 + 2 * persistentField.getType().asErasure().getStackSize().getSize(), instrumentedMethod.getStackSize() ); } + + @Override + public boolean equals(final Object o) { + if ( this == o ) { + return true; + } + if ( o == null || InlineDirtyCheckingHandler.class != o.getClass() ) { + return false; + } + final InlineDirtyCheckingHandler that = (InlineDirtyCheckingHandler) o; + return Objects.equals( delegate, that.delegate ) && + Objects.equals( managedCtClass, that.managedCtClass ) && + Objects.equals( persistentField, that.persistentField ); + } + + @Override + public int hashCode() { + return Objects.hash( delegate, managedCtClass, persistentField ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/PersistentAttributeTransformer.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/PersistentAttributeTransformer.java index fd12ab426b..543820d4bd 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/PersistentAttributeTransformer.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/PersistentAttributeTransformer.java @@ -11,6 +11,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Objects; + import javax.persistence.Embedded; import net.bytebuddy.description.field.FieldList; @@ -41,7 +43,7 @@ import net.bytebuddy.pool.TypePool; import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; import static net.bytebuddy.matcher.ElementMatchers.not; -class PersistentAttributeTransformer implements AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper { +final class PersistentAttributeTransformer implements AsmVisitorWrapper.ForDeclaredMethods.MethodVisitorWrapper { private static final CoreMessageLogger log = CoreLogging.messageLogger( PersistentAttributeTransformer.class ); @@ -313,4 +315,26 @@ class PersistentAttributeTransformer implements AsmVisitorWrapper.ForDeclaredMet return new Size( 1 + persistentField.getType().getStackSize().getSize(), instrumentedMethod.getStackSize() ); } } + + @Override + public boolean equals(final Object o) { + if ( this == o ) { + return true; + } + if ( o == null || PersistentAttributeTransformer.class != o.getClass() ) { + return false; + } + final PersistentAttributeTransformer that = (PersistentAttributeTransformer) o; + return Objects.equals( managedCtClass, that.managedCtClass ) && + Objects.equals( enhancementContext, that.enhancementContext ) && + Objects.equals( classPool, that.classPool ) && + Arrays.equals( enhancedFields, that.enhancedFields ); + } + + @Override + public int hashCode() { + int result = Objects.hash( managedCtClass, enhancementContext, classPool ); + result = 31 * result + Arrays.hashCode( enhancedFields ); + return result; + } }