HHH-13759 Small refactoring for PersistentAttributeTransformer

This change makes it easier to identify where we behave differently
  because we are dealing with a class annotated with @MappedSuperclasss.
This commit is contained in:
Davide D'Alto 2019-12-11 16:36:31 +00:00 committed by Sanne Grinovero
parent 57a45dab19
commit 6cf7fdaa97
2 changed files with 25 additions and 18 deletions
hibernate-core/src/main/java/org/hibernate/bytecode/enhance/internal/bytebuddy

View File

@ -285,7 +285,7 @@ public class EnhancerImpl implements Enhancer {
}
}
return createTransformer( managedCtClass ).applyTo( builder, false );
return createTransformer( managedCtClass ).applyTo( builder );
}
else if ( enhancementContext.isCompositeClass( managedCtClass ) ) {
log.debugf( "Enhancing [%s] as Composite", managedCtClass.getName() );
@ -318,13 +318,13 @@ public class EnhancerImpl implements Enhancer {
.intercept( implementationClearOwner );
}
return createTransformer( managedCtClass ).applyTo( builder, false );
return createTransformer( managedCtClass ).applyTo( builder );
}
else if ( enhancementContext.isMappedSuperclassClass( managedCtClass ) ) {
log.debugf( "Enhancing [%s] as MappedSuperclass", managedCtClass.getName() );
builder = builder.implement( ManagedMappedSuperclass.class );
return createTransformer( managedCtClass ).applyTo( builder, true );
return createTransformer( managedCtClass ).applyTo( builder );
}
else if ( enhancementContext.doExtendedEnhancement( managedCtClass ) ) {
log.debugf( "Extended enhancement of [%s]", managedCtClass.getName() );

View File

@ -175,7 +175,7 @@ final class PersistentAttributeTransformer implements AsmVisitorWrapper.ForDecla
return false;
}
DynamicType.Builder<?> applyTo(DynamicType.Builder<?> builder, boolean accessor) {
DynamicType.Builder<?> applyTo(DynamicType.Builder<?> builder) {
boolean compositeOwner = false;
builder = builder.visit( new AsmVisitorWrapper.ForDeclaredMethods().invokable( NOT_HIBERNATE_GENERATED, this ) );
@ -186,10 +186,7 @@ final class PersistentAttributeTransformer implements AsmVisitorWrapper.ForDecla
enhancedField.getType().asErasure(),
Visibility.PUBLIC
)
.intercept(
accessor
? FieldAccessor.ofField( enhancedField.getName() ).in( enhancedField.getDeclaringType().asErasure() )
: fieldReader( enhancedField )
.intercept( fieldReader( enhancedField )
)
.defineMethod(
EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + enhancedField.getName(),
@ -197,12 +194,10 @@ final class PersistentAttributeTransformer implements AsmVisitorWrapper.ForDecla
Visibility.PUBLIC
)
.withParameters( enhancedField.getType().asErasure() )
.intercept( accessor
? FieldAccessor.ofField( enhancedField.getName() ).in( enhancedField.getDeclaringType().asErasure() )
: fieldWriter( enhancedField ) );
.intercept( fieldWriter( enhancedField ) );
if ( !compositeOwner
&& !accessor
&& !enhancementContext.isMappedSuperclassClass( managedCtClass )
&& enhancedField.hasAnnotation( Embedded.class )
&& enhancementContext.isCompositeClass( enhancedField.getType().asErasure() )
&& enhancementContext.doDirtyCheckingInline( managedCtClass ) ) {
@ -228,6 +223,9 @@ final class PersistentAttributeTransformer implements AsmVisitorWrapper.ForDecla
}
private Implementation fieldReader(AnnotatedFieldDescription enhancedField) {
if ( enhancementContext.isMappedSuperclassClass( managedCtClass ) ) {
return FieldAccessor.ofField( enhancedField.getName() ).in( enhancedField.getDeclaringType().asErasure() );
}
if ( !enhancementContext.hasLazyLoadableAttributes( managedCtClass ) || !enhancementContext.isLazyLoadable( enhancedField ) ) {
if ( enhancedField.getDeclaringType().asErasure().equals( managedCtClass ) ) {
return FieldAccessor.ofField( enhancedField.getName() ).in( enhancedField.getDeclaringType().asErasure() );
@ -242,20 +240,29 @@ final class PersistentAttributeTransformer implements AsmVisitorWrapper.ForDecla
}
private Implementation fieldWriter(AnnotatedFieldDescription enhancedField) {
Implementation implementation;
Implementation implementation = fieldWriterImplementation( enhancedField );
if ( !enhancementContext.isMappedSuperclassClass( managedCtClass ) ) {
implementation = InlineDirtyCheckingHandler.wrap( managedCtClass, enhancementContext, enhancedField, implementation );
implementation = BiDirectionalAssociationHandler.wrap( managedCtClass, enhancementContext, enhancedField, implementation );
}
return implementation;
}
private Implementation fieldWriterImplementation(AnnotatedFieldDescription enhancedField) {
if ( enhancementContext.isMappedSuperclassClass( managedCtClass ) ) {
return FieldAccessor.ofField( enhancedField.getName() ).in( enhancedField.getDeclaringType().asErasure() );
}
if ( !enhancementContext.hasLazyLoadableAttributes( managedCtClass ) || !enhancementContext.isLazyLoadable( enhancedField ) ) {
if ( enhancedField.getDeclaringType().asErasure().equals( managedCtClass ) ) {
implementation = FieldAccessor.ofField( enhancedField.getName() ).in( enhancedField.getDeclaringType().asErasure() );
return FieldAccessor.ofField( enhancedField.getName() ).in( enhancedField.getDeclaringType().asErasure() );
}
else {
implementation = new Implementation.Simple( new FieldMethodWriter( managedCtClass, enhancedField ) );
return new Implementation.Simple( new FieldMethodWriter( managedCtClass, enhancedField ) );
}
}
else {
implementation = new Implementation.Simple( FieldWriterAppender.of( managedCtClass, enhancedField ) );
return new Implementation.Simple( FieldWriterAppender.of( managedCtClass, enhancedField ) );
}
implementation = InlineDirtyCheckingHandler.wrap( managedCtClass, enhancementContext, enhancedField, implementation );
return BiDirectionalAssociationHandler.wrap( managedCtClass, enhancementContext, enhancedField, implementation );
}
DynamicType.Builder<?> applyExtended(DynamicType.Builder<?> builder) {