final FieldInfo fieldInfo = persistentField.getFieldInfo();
final String fieldName = fieldInfo.getName();
final String writerName = EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + fieldName;
final CtMethod writer;
try {
if ( !enhancementContext.isLazyLoadable( persistentField ) ) {
// not lazy-loadable...
writer = CtNewMethod.setter( writerName, persistentField );
}
else {
final String methodBody = typeDescriptor.buildWriteInterceptionBodyFragment( fieldName );
writer = CtNewMethod.make(
Modifier.PRIVATE,
CtClass.voidType,
writerName,
new CtClass[] {persistentField.getType()},
null,
"{" + methodBody + "}",
managedCtClass
);
}
if ( enhancementContext.doDirtyCheckingInline( managedCtClass ) && !isComposite ) {
writer.insertBefore( typeDescriptor.buildInLineDirtyCheckingBodyFragment( persistentField ) );
}
if ( isComposite ) {
StringBuilder builder = new StringBuilder();
builder.append( " if( " )
.append( EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME )
.append( " != null) " )
.append( EnhancerConstants.TRACKER_COMPOSITE_FIELD_NAME )
.append( ".callOwner(\"." )
.append( persistentField.getName() )
.append( "\");" );
writer.insertBefore( builder.toString() );
}
//composite types
if ( persistentField.getAnnotation( Embedded.class ) != null ) {
//make sure to add the CompositeOwner interface
if ( !doClassInheritCompositeOwner( managedCtClass ) ) {
managedCtClass.addInterface( classPool.get( "org.hibernate.engine.spi.CompositeOwner" ) );
}
//if a composite have a embedded field we need to implement the method as well
if ( isComposite ) {
createTrackChangeCompositeMethod( managedCtClass );
}
writer.insertBefore( cleanupPreviousOwner( persistentField ) );
writer.insertAfter( compositeMethodBody( persistentField ) );
}
managedCtClass.addMethod( writer );
return writer;
}