HHH-11050 - [bytecode enhancement] Add support for getters/setters through delegation
This commit is contained in:
parent
d23deb37cd
commit
15502f8a4a
|
@ -8,6 +8,7 @@ package org.hibernate.bytecode.enhance.internal;
|
|||
|
||||
import javassist.CannotCompileException;
|
||||
import javassist.CtClass;
|
||||
import javassist.CtField;
|
||||
import javassist.CtMethod;
|
||||
import javassist.CtNewMethod;
|
||||
import javassist.NotFoundException;
|
||||
|
@ -35,10 +36,10 @@ public class MethodWriter {
|
|||
* @throws CannotCompileException
|
||||
*/
|
||||
public static CtMethod write(CtClass target, String format, Object ... args) throws CannotCompileException {
|
||||
final String body = String.format( format, args );
|
||||
String body = String.format( format, args );
|
||||
// System.out.printf( "writing method into [%s]:%n%s%n", target.getName(), body );
|
||||
log.debugf( "writing method into [%s]:%n%s", target.getName(), body );
|
||||
final CtMethod method = CtNewMethod.make( body, target );
|
||||
CtMethod method = CtNewMethod.make( body, target );
|
||||
target.addMethod( method );
|
||||
return method;
|
||||
}
|
||||
|
@ -46,35 +47,55 @@ public class MethodWriter {
|
|||
/* --- */
|
||||
|
||||
public static CtMethod addGetter(CtClass target, String field, String name) {
|
||||
CtField actualField = null;
|
||||
try {
|
||||
actualField = target.getField( field );
|
||||
log.debugf( "Writing getter method [%s] into [%s] for field [%s]", name, target.getName(), field );
|
||||
final CtMethod method = CtNewMethod.getter( name, target.getField( field ) );
|
||||
CtMethod method = CtNewMethod.getter( name, target.getField( field ) );
|
||||
target.addMethod( method );
|
||||
return method;
|
||||
}
|
||||
catch (CannotCompileException cce) {
|
||||
final String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field );
|
||||
try {
|
||||
// Fall back to create a getter from delegation.
|
||||
CtMethod method = CtNewMethod.delegator( CtNewMethod.getter( name, actualField ), target );
|
||||
target.addMethod( method );
|
||||
return method;
|
||||
}
|
||||
catch (CannotCompileException ignored) {
|
||||
String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field );
|
||||
throw new EnhancementException( msg, cce );
|
||||
}
|
||||
}
|
||||
catch (NotFoundException nfe) {
|
||||
final String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field );
|
||||
String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field );
|
||||
throw new EnhancementException( msg, nfe );
|
||||
}
|
||||
}
|
||||
|
||||
public static CtMethod addSetter(CtClass target, String field, String name) {
|
||||
CtField actualField = null;
|
||||
try {
|
||||
actualField = target.getField( field );
|
||||
log.debugf( "Writing setter method [%s] into [%s] for field [%s]", name, target.getName(), field );
|
||||
final CtMethod method = CtNewMethod.setter( name, target.getField( field ) );
|
||||
CtMethod method = CtNewMethod.setter( name, actualField );
|
||||
target.addMethod( method );
|
||||
return method;
|
||||
}
|
||||
catch (CannotCompileException cce) {
|
||||
final String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field );
|
||||
try {
|
||||
// Fall back to create a getter from delegation.
|
||||
CtMethod method = CtNewMethod.delegator( CtNewMethod.setter( name, actualField ), target );
|
||||
target.addMethod( method );
|
||||
return method;
|
||||
}
|
||||
catch (CannotCompileException ignored) {
|
||||
String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field );
|
||||
throw new EnhancementException( msg, cce );
|
||||
}
|
||||
}
|
||||
catch (NotFoundException nfe) {
|
||||
final String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field );
|
||||
String msg = String.format( "Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field );
|
||||
throw new EnhancementException( msg, nfe );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -347,10 +347,10 @@ public class PersistentAttributesHelper {
|
|||
private static String inferGenericTypeName(CtClass ctClass, SignatureAttribute.Type genericSignature) {
|
||||
// infer targetEntity from generic type signature
|
||||
if ( isAssignable( ctClass, Collection.class.getName() ) ) {
|
||||
return ( (SignatureAttribute.ClassType) genericSignature ).getTypeArguments()[0].toString();
|
||||
return ( (SignatureAttribute.ClassType) genericSignature ).getTypeArguments()[0].getType().jvmTypeName();
|
||||
}
|
||||
if ( isAssignable( ctClass, Map.class.getName() ) ) {
|
||||
return ( (SignatureAttribute.ClassType) genericSignature ).getTypeArguments()[1].toString();
|
||||
return ( (SignatureAttribute.ClassType) genericSignature ).getTypeArguments()[1].getType().jvmTypeName();
|
||||
}
|
||||
return ctClass.getName();
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import javassist.CtClass;
|
|||
|
||||
import org.hibernate.test.bytecode.enhancement.lazy.group.LazyGroupUpdateTestTask;
|
||||
import org.hibernate.test.bytecode.enhancement.lazy.group.SimpleLazyGroupUpdateTestTask;
|
||||
import org.hibernate.test.bytecode.enhancement.association.InheritedAttributeAssociationTestTask;
|
||||
import org.hibernate.test.bytecode.enhancement.otherentityentrycontext.OtherEntityEntryContextTestTask;
|
||||
import org.hibernate.testing.FailureExpected;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
|
@ -94,6 +95,7 @@ public class EnhancerTest extends BaseUnitTestCase {
|
|||
EnhancerTestUtils.runEnhancerTestTask( OneToOneAssociationTestTask.class );
|
||||
EnhancerTestUtils.runEnhancerTestTask( OneToManyAssociationTestTask.class );
|
||||
EnhancerTestUtils.runEnhancerTestTask( ManyToManyAssociationTestTask.class );
|
||||
EnhancerTestUtils.runEnhancerTestTask( InheritedAttributeAssociationTestTask.class );
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
||||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.bytecode.enhancement.association;
|
||||
|
||||
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils;
|
||||
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.DiscriminatorType;
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.MappedSuperclass;
|
||||
import javax.persistence.OneToMany;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Luis Barreiro
|
||||
*/
|
||||
public class InheritedAttributeAssociationTestTask extends AbstractEnhancerTestTask {
|
||||
|
||||
public Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] { Author.class, Item.class, ChildItem.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepare() {
|
||||
}
|
||||
|
||||
@Override
|
||||
@TestForIssue( jiraKey = "HHH-11050")
|
||||
public void execute() {
|
||||
// The mapping is wrong but the point is that the enhancement phase does not need to fail. See JIRA for further detail
|
||||
|
||||
// If enhancement of 'items' attribute fails, 'name' won't be enhanced
|
||||
Author author = new Author();
|
||||
author.name = "Bernardo Soares";
|
||||
EnhancerTestUtils.checkDirtyTracking( author, "name" );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cleanup() {
|
||||
}
|
||||
|
||||
@Entity
|
||||
public static class Author {
|
||||
|
||||
@Id @GeneratedValue
|
||||
Long id;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY, mappedBy="author")
|
||||
List<ChildItem> items;
|
||||
|
||||
// keep this field after 'items'
|
||||
String name;
|
||||
}
|
||||
|
||||
@MappedSuperclass
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
|
||||
public static abstract class Item {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
Long id;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
Author author;
|
||||
}
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("child")
|
||||
public static class ChildItem extends Item {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue