From 8fc0bf520245545df6c6c10b0fe4e2d23aa48a0a Mon Sep 17 00:00:00 2001 From: barreiro Date: Wed, 18 Jan 2017 03:39:42 +0000 Subject: [PATCH] HHH-11284 - HHH-11404 - Fix enhancement in cases where there is some sort of inheritance --- .../internal/bytebuddy/EnhancerImpl.java | 14 ++++++----- .../internal/javassist/EnhancerImpl.java | 25 ++++++++----------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java index c0c0d20f86..43ef42992c 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy/EnhancerImpl.java @@ -274,6 +274,8 @@ public class EnhancerImpl implements Enhancer { } else if ( enhancementContext.isMappedSuperclassClass( managedCtClass ) ) { log.infof( "Enhancing [%s] as MappedSuperclass", managedCtClass.getName() ); + + builder = builder.implement( ManagedMappedSuperclass.class ); return transformer.applyTo( builder, true ); } else if ( enhancementContext.doExtendedEnhancement( managedCtClass ) ) { @@ -286,14 +288,14 @@ public class EnhancerImpl implements Enhancer { } } + // See HHH-10977 HHH-11284 HHH-11404 --- check for declaration of Managed interface on the class, not inherited private boolean alreadyEnhanced(TypeDescription managedCtClass) { - if ( !managedCtClass.isAssignableTo( Managed.class ) ) { - return false; + for ( TypeDescription.Generic declaredInterface : managedCtClass.getInterfaces() ) { + if ( declaredInterface.asErasure().isAssignableTo( Managed.class ) ) { + return true; + } } - // HHH-10977 - When a mapped superclass gets enhanced before a subclassing entity, the entity does not get enhanced, but it implements the Managed interface - return enhancementContext.isEntityClass( managedCtClass ) && managedCtClass.isAssignableTo( ManagedEntity.class ) - || enhancementContext.isCompositeClass( managedCtClass ) && managedCtClass.isAssignableTo( ManagedComposite.class ) - || enhancementContext.isMappedSuperclassClass( managedCtClass ) && managedCtClass.isAssignableTo( ManagedMappedSuperclass.class ); + return false; } private DynamicType.Builder addInterceptorHandling(DynamicType.Builder builder, TypeDescription managedCtClass) { diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/javassist/EnhancerImpl.java b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/javassist/EnhancerImpl.java index 468b276995..b82a9a42d4 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/javassist/EnhancerImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/javassist/EnhancerImpl.java @@ -18,15 +18,13 @@ import javassist.ClassPool; import javassist.CtClass; import javassist.LoaderClassPath; +import javassist.NotFoundException; import org.hibernate.HibernateException; import org.hibernate.bytecode.enhance.spi.EnhancementContext; import org.hibernate.bytecode.enhance.spi.EnhancementException; import org.hibernate.bytecode.enhance.spi.Enhancer; import org.hibernate.bytecode.enhance.spi.EnhancerConstants; import org.hibernate.engine.spi.Managed; -import org.hibernate.engine.spi.ManagedComposite; -import org.hibernate.engine.spi.ManagedEntity; -import org.hibernate.engine.spi.ManagedMappedSuperclass; import org.hibernate.engine.spi.PersistentAttributeInterceptable; import org.hibernate.engine.spi.PersistentAttributeInterceptor; import org.hibernate.internal.CoreLogging; @@ -159,20 +157,19 @@ public class EnhancerImpl implements Enhancer { } } + // See HHH-10977 HHH-11284 HHH-11404 --- check for declaration of Managed interface on the class, not inherited private boolean alreadyEnhanced(CtClass managedCtClass) { - if ( !PersistentAttributesHelper.isAssignable( managedCtClass, Managed.class.getName() ) ) { + try { + for ( CtClass declaredInterface : managedCtClass.getInterfaces() ) { + if ( PersistentAttributesHelper.isAssignable( declaredInterface, Managed.class.getName() ) ) { + return true; + } + } return false; } - // HHH-10977 - When a mapped superclass gets enhanced before a subclassing entity, the entity does not get enhanced, but it implements the Managed interface - return enhancementContext.isEntityClass( managedCtClass ) && PersistentAttributesHelper.isAssignable( managedCtClass, ManagedEntity.class.getName() ) - || enhancementContext.isCompositeClass( managedCtClass ) && PersistentAttributesHelper.isAssignable( - managedCtClass, - ManagedComposite.class.getName() - ) - || enhancementContext.isMappedSuperclassClass( managedCtClass ) && PersistentAttributesHelper.isAssignable( - managedCtClass, - ManagedMappedSuperclass.class.getName() - ); + catch ( NotFoundException e ) { + throw new HibernateException( "Unable to transform class: " + e.getMessage() , e ); + } } private byte[] getByteCode(CtClass managedCtClass) {