HHH-10354 - Rename 'field access' to 'extended enhancement'

This commit is contained in:
barreiro 2015-12-04 04:39:16 +00:00 committed by Steve Ebersole
parent 20ebd8f5ca
commit 6828f5ee9b
15 changed files with 92 additions and 167 deletions

View File

@ -69,8 +69,8 @@ public class PersistentAttributesEnhancer extends Enhancer {
enhanceAttributesAccess( managedCtClass, attrDescriptorMap );
// same thing for direct access to fields of other entities
if ( this.enhancementContext.doFieldAccessEnhancement( managedCtClass ) ) {
enhanceFieldAccess( managedCtClass );
if ( this.enhancementContext.doExtendedEnhancement( managedCtClass ) ) {
extendedEnhancement( managedCtClass );
}
}
@ -390,7 +390,7 @@ public class PersistentAttributesEnhancer extends Enhancer {
" if ($1 != null && %s) {%n" +
" Object[] array = $1.toArray();%n" +
" for (int i = 0; i < array.length; i++) {%n" +
" %s target = (%<s) array[i];%n" +
" %s target = (%s) array[i];%n" +
" if (%s) {%n" +
" java.util.Collection c = target.%s();%n" +
" if (c != this && c != null) { c.add(this); }%n" +
@ -399,6 +399,7 @@ public class PersistentAttributesEnhancer extends Enhancer {
" }%n",
newAssociationLoaded,
targetEntity.getName(),
targetEntity.getName(),
targetElementLoaded,
mappedByGetterName
)
@ -537,16 +538,18 @@ public class PersistentAttributesEnhancer extends Enhancer {
}
}
// --- //
/**
* Replace access to fields of entities (for example, entity.field) with a call to the enhanced getter / setter
* (in this example, entity.$$_hibernate_read_field()). It's assumed that the target entity is enhanced as well.
*
* @param managedCtClass Class to enhance
* @param aCtClass Class to enhance (not an entity class).
*/
public void enhanceFieldAccess(CtClass managedCtClass) {
final ConstPool constPool = managedCtClass.getClassFile().getConstPool();
public void extendedEnhancement(CtClass aCtClass) {
final ConstPool constPool = aCtClass.getClassFile().getConstPool();
for ( Object oMethod : managedCtClass.getClassFile().getMethods() ) {
for ( Object oMethod : aCtClass.getClassFile().getMethods() ) {
final MethodInfo methodInfo = (MethodInfo) oMethod;
final String methodName = methodInfo.getName();
@ -570,14 +573,18 @@ public class PersistentAttributesEnhancer extends Enhancer {
if ( !enhancementContext.isEntityClass( targetCtClass ) && !enhancementContext.isCompositeClass( targetCtClass ) ) {
continue;
}
if ( targetCtClass == managedCtClass
if ( targetCtClass == aCtClass
|| !enhancementContext.isPersistentField( targetCtClass.getField( fieldName ) )
|| PersistentAttributesHelper.hasAnnotation( targetCtClass, fieldName, Id.class )
|| "this$0".equals( fieldName ) ) {
continue;
}
log.debugf( "Transforming access to field [%s] from method [%s]", fieldName, methodName );
log.debugf( "Extended enhancement: Transforming access to field [%s.%s] from method [%s#%s]",
fieldClassName,
fieldName,
aCtClass.getName(),
methodName );
if ( op == Opcode.GETFIELD ) {
int fieldReaderMethodIndex = constPool.addMethodrefInfo(
@ -603,14 +610,14 @@ public class PersistentAttributesEnhancer extends Enhancer {
}
catch (BadBytecode bb) {
final String msg = String.format(
"Unable to perform field access transformation in method [%s]",
"Unable to perform extended enhancement in method [%s]",
methodName
);
throw new EnhancementException( msg, bb );
}
catch (NotFoundException nfe) {
final String msg = String.format(
"Unable to perform field access transformation in method [%s]",
"Unable to perform extended enhancement in method [%s]",
methodName
);
throw new EnhancementException( msg, nfe );

View File

@ -61,7 +61,7 @@ public class DefaultEnhancementContext implements EnhancementContext {
/**
* @return false
*/
public boolean doFieldAccessEnhancement(CtClass classDescriptor) {
public boolean doExtendedEnhancement(CtClass classDescriptor) {
return false;
}

View File

@ -81,7 +81,7 @@ public interface EnhancementContext {
* @return {@code true} indicates that any direct access to fields of entities should be routed to the enhanced
* getter / setter method.
*/
public boolean doFieldAccessEnhancement(CtClass classDescriptor);
public boolean doExtendedEnhancement(CtClass classDescriptor);
/**
* Does the given class define any lazy loadable attributes?

View File

@ -146,9 +146,9 @@ public class Enhancer {
log.debugf( "Enhancing [%s] as Composite", managedCtClass.getName() );
new CompositeEnhancer( enhancementContext ).enhance( managedCtClass );
}
else if ( enhancementContext.doFieldAccessEnhancement( managedCtClass ) ) {
log.debugf( "Enhancing field access in [%s]", managedCtClass.getName() );
new PersistentAttributesEnhancer( enhancementContext ).enhanceFieldAccess( managedCtClass );
else if ( enhancementContext.doExtendedEnhancement( managedCtClass ) ) {
log.debugf( "Extended enhancement of [%s]", managedCtClass.getName() );
new PersistentAttributesEnhancer( enhancementContext ).extendedEnhancement( managedCtClass );
}
else {
log.debugf( "Skipping enhancement of [%s]: not entity or composite", managedCtClass.getName() );

View File

@ -185,7 +185,7 @@ public class EnhancementTask extends Task implements EnhancementContext {
}
@Override
public boolean doFieldAccessEnhancement(CtClass classDescriptor) {
public boolean doExtendedEnhancement(CtClass classDescriptor) {
return false;
}

View File

@ -16,8 +16,8 @@ import org.hibernate.test.bytecode.enhancement.association.OneToOneAssociationTe
import org.hibernate.test.bytecode.enhancement.basic.BasicEnhancementTestTask;
import org.hibernate.test.bytecode.enhancement.basic.HHH9529TestTask;
import org.hibernate.test.bytecode.enhancement.dirty.DirtyTrackingTestTask;
import org.hibernate.test.bytecode.enhancement.field.FieldAccessBidirectionalTestTasK;
import org.hibernate.test.bytecode.enhancement.field.FieldAccessEnhancementTestTask;
import org.hibernate.test.bytecode.enhancement.extended.ExtendedAssociationManagementTestTasK;
import org.hibernate.test.bytecode.enhancement.extended.ExtendedEnhancementTestTask;
import org.hibernate.test.bytecode.enhancement.join.HHH3949TestTask1;
import org.hibernate.test.bytecode.enhancement.join.HHH3949TestTask2;
import org.hibernate.test.bytecode.enhancement.join.HHH3949TestTask3;
@ -106,9 +106,9 @@ public class EnhancerTest extends BaseUnitTestCase {
}
@Test
public void testFieldAccess() {
EnhancerTestUtils.runEnhancerTestTask( FieldAccessEnhancementTestTask.class );
EnhancerTestUtils.runEnhancerTestTask( FieldAccessBidirectionalTestTasK.class );
public void testExtendedEnhancement() {
EnhancerTestUtils.runEnhancerTestTask( ExtendedEnhancementTestTask.class );
EnhancerTestUtils.runEnhancerTestTask( ExtendedAssociationManagementTestTasK.class );
}
@Test

View File

@ -4,7 +4,7 @@
* 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.field;
package org.hibernate.test.bytecode.enhancement.extended;
import java.util.UUID;
import javax.persistence.Entity;
@ -12,14 +12,14 @@ import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils;
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
import org.junit.Assert;
/**
* @author Luis Barreiro
*/
public class FieldAccessBidirectionalTestTasK extends AbstractEnhancerTestTask {
public class ExtendedAssociationManagementTestTasK extends AbstractEnhancerTestTask {
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {Customer.class, User.class};

View File

@ -4,7 +4,7 @@
* 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.field;
package org.hibernate.test.bytecode.enhancement.extended;
import java.util.Arrays;
import java.util.List;
@ -15,15 +15,15 @@ import javax.persistence.OneToMany;
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
import org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils;
import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask;
import org.hibernate.test.bytecode.enhancement.basic.ObjectAttributeMarkerInterceptor;
import org.junit.Assert;
/**
* @author Luis Barreiro
*/
public class FieldAccessEnhancementTestTask extends AbstractEnhancerTestTask {
public class ExtendedEnhancementTestTask extends AbstractEnhancerTestTask {
public Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {SimpleEntity.class};

View File

@ -6,7 +6,6 @@
*/
package org.hibernate.test.bytecode.enhancement.pk;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;

View File

@ -1,134 +1,62 @@
/*
* SPECjEnterprise2010 - a benchmark for enterprise middleware
* Copyright 1995-2010 Standard Performance Evaluation Corporation
* All Rights Reserved
* 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.pk;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlSchemaType;
/**
* Represent a work order that evolves through multiple processing stages.
*
*/
@SuppressWarnings("serial")
@NamedQueries({
@NamedQuery(name=WorkOrder.QUERY_ALL,
query="select w from WorkOrder w"),
// @NamedQuery(name=WorkOrder.QUERY_BY_STATUS,
// query="select w from WorkOrder w where w.status = :status"),
@NamedQuery(name=WorkOrder.QUERY_BY_OID_OLID,
query="select w from WorkOrder w where w.location = :location and w.salesId = :salesId and w.orderLineId = :orderLineId"),
@NamedQuery(name=WorkOrder.QUERY_COUNT,
query="select COUNT(a) from WorkOrder a")
})
@Entity
@Table(name = "M_WORKORDER")
@XmlAccessorType(XmlAccessType.PROPERTY)
@IdClass(WorkOrderPK.class)
public class WorkOrder {
public static final String QUERY_ALL = "WorkOrder.selectAll";
//public static final String QUERY_BY_STATUS = "WorkOrder.selectByStatus";
public static final String QUERY_BY_OID_OLID = "WorkOrder.selectByOID_OLID";
public static final String QUERY_COUNT = "WorkOrder.count";
@Id
@TableGenerator(name = "workorder",
table = "U_SEQUENCES",
pkColumnName = "S_ID",
valueColumnName = "S_NEXTNUM",
pkColumnValue = "workorder",
allocationSize = 1000)
@GeneratedValue(strategy = GenerationType.TABLE, generator = "workorder")
@Column(name = "WO_NUMBER")
private int id;
@Id
@Column(name = "WO_LOCATION")
private int location;
@Column(name = "WO_O_ID")
private int salesId;
@Column(name = "WO_OL_ID")
private int orderLineId;
@Column(name = "WO_ORIG_QTY")
private int originalQuantity;
@Column(name = "WO_COMP_QTY")
private int completedQuantity;
@Column(name = "WO_DUE_DATE")
@Temporal(TemporalType.TIMESTAMP)
private Calendar dueDate;
@Column(name = "WO_START_DATE")
@Temporal(TemporalType.TIMESTAMP)
private Calendar startDate;
@Column(name = "WO_ASSEMBLY_ID")
private String assemblyId;
@Version
@Column(name = "WO_VERSION")
private int version;
/**
* Public no-arg constructor required by JAXB specification.
*/
public WorkOrder() {
this("", 1, 0, Calendar.getInstance());
}
/**
* Construct with proper state. The status at construction is OPEN.
* @param location
*/
public WorkOrder(String assemblyId, int origQty, int location, Calendar dueDate) {
if (origQty < 1)
throw new IllegalArgumentException("WorkOrder can not be created " +
" with original quantity " + origQty + ". Must be > 0");
throw new IllegalArgumentException("WorkOrder can not be created with original quantity " + origQty + ". Must be > 0");
if (dueDate == null)
throw new IllegalArgumentException("WorkOrder can not be created " +
" with null due Date");
throw new IllegalArgumentException("WorkOrder can not be created with null due Date");
this.assemblyId = assemblyId;
originalQuantity = origQty;
this.originalQuantity = origQty;
this.dueDate = dueDate;
this.location=location;
}
/**
* Construct with proper state. The status at construction is OPEN.
* @param location
*/
public WorkOrder(String assemblyId, int salesId, int oLineId, int origQty,
int location, Calendar dueDate) {
this(assemblyId, origQty, location, dueDate);
this.salesId = salesId;
orderLineId = oLineId;
}
public int getId() {
return id;
}
@ -136,6 +64,8 @@ public class WorkOrder {
public void setId(int id) {
this.id = id;
}
// --- //
public String getAssemblyId() {
return assemblyId;
@ -153,23 +83,14 @@ public class WorkOrder {
return dueDate;
}
public int getOrderLineId() {
return orderLineId;
}
public int getOriginalQuantity() {
return originalQuantity;
}
public int getSalesId() {
return salesId;
}
public int getLocation() {
return location;
}
@XmlSchemaType(name = "dateTime")
public Calendar getStartDate() {
return startDate;
}
@ -178,9 +99,30 @@ public class WorkOrder {
return version;
}
// ======================================================================
// --- //
public void setStartDate(Calendar instance) {
startDate = instance;
}
public void setLocation(int location) {
this.location = location;
}
public void setDueDate(Calendar dueDate) {
this.dueDate = dueDate;
}
public void setAssemblyId(String assemblyId) {
this.assemblyId = assemblyId;
}
public void setOriginalQuantity(int originalQuantity) {
this.originalQuantity = originalQuantity;
}
// Processing methods
// ======================================================================
/**
* Moves to the next state of processing.
* Return true if the new status can be updated again.
@ -202,6 +144,8 @@ public class WorkOrder {
public void setStatusCancelled() {
}
// --- //
public boolean equals(Object other) {
if (this == other)
@ -216,37 +160,8 @@ public class WorkOrder {
return PRIME * new Integer(id).hashCode();
}
public String toString() {
return "WorkOrder:["+ getId() + "]" ;
}
public void setStartDate(Calendar instance) {
startDate = instance;
}
public void setLocation(int location) {
this.location = location;
}
public void setDueDate(Calendar dueDate) {
this.dueDate = dueDate;
}
public void setAssemblyId(String assemblyId) {
this.assemblyId = assemblyId;
}
public void setOriginalQuantity(int originalQuantity) {
this.originalQuantity = originalQuantity;
}
public void setSalesId(int salesId) {
this.salesId = salesId;
}
public void setOrderLineId(int orderLineId) {
this.orderLineId = orderLineId;
}
}

View File

@ -1,12 +1,8 @@
/*
* SPECjEnterprise2010 - a benchmark for enterprise middleware
* Copyright 1995-2010 Standard Performance Evaluation Corporation
* All Rights Reserved
* Hibernate, Relational Persistence for Idiomatic Java
*
* History:
* Date ID, Company Description
* ---------- ---------------- ----------------------------------------------
* 2009/05/31 Anoop Gupta, Oracle Created for SPECjEnterprise2010
* 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.pk;

View File

@ -25,7 +25,7 @@ public class JpaRuntimeEnhancementTest extends BaseUnitTestCase {
// public ClassLoader buildIsolatedClassLoader() {
// final EnhancementContext enhancementContext = new DefaultEnhancementContext() {
// @Override
// public boolean doFieldAccessEnhancement(CtClass classDescriptor) {
// public boolean doExtendedEnhancement(CtClass classDescriptor) {
// return classDescriptor.getPackageName().startsWith( "org.hibernate.jpa.test.enhancement.domain" );
// }
// };

View File

@ -18,7 +18,7 @@ import org.hibernate.bytecode.enhance.spi.DefaultEnhancementContext;
public class EnhancerTestContext extends DefaultEnhancementContext {
@Override
public boolean doFieldAccessEnhancement(CtClass classDescriptor) {
public boolean doExtendedEnhancement(CtClass classDescriptor) {
return true;
}
}

View File

@ -70,11 +70,11 @@ public class MavenEnhancePlugin extends AbstractMojo {
@Parameter(property = "enableAssociationManagement", defaultValue = "true")
private boolean enableAssociationManagement = true;
@Parameter(property = "enableFieldAccessEnhancement", defaultValue = "false")
private boolean enableFieldAccessEnhancement = false;
@Parameter(property = "enableExtendedEnhancement", defaultValue = "false")
private boolean enableExtendedEnhancement = false;
private boolean shouldApply() {
return enableLazyInitialization || enableDirtyTracking || enableAssociationManagement || enableFieldAccessEnhancement;
return enableLazyInitialization || enableDirtyTracking || enableAssociationManagement || enableExtendedEnhancement;
}
public void execute() throws MojoExecutionException, MojoFailureException {
@ -125,11 +125,15 @@ public class MavenEnhancePlugin extends AbstractMojo {
}
@Override
public boolean doFieldAccessEnhancement(CtClass classDescriptor) {
return enableFieldAccessEnhancement;
public boolean doExtendedEnhancement(CtClass classDescriptor) {
return enableExtendedEnhancement;
}
};
if ( enableExtendedEnhancement ) {
getLog().warn( "Extended enhancement is enabled. Classes other than entities may be modified. You should consider access the entities using getter/setter methods and disable this property. Use at your own risk." );
}
final Enhancer enhancer = new Enhancer( enhancementContext );
final ClassPool classPool = new ClassPool( false );

View File

@ -111,11 +111,15 @@ public class HibernatePlugin implements Plugin<Project> {
}
@Override
public boolean doFieldAccessEnhancement(CtClass classDescriptor) {
return hibernateExtension.enhance.getEnableFieldAccessEnhancement();
public boolean doExtendedEnhancement(CtClass classDescriptor) {
return hibernateExtension.enhance.getEnableFieldAccessEnhancement();
}
};
if ( hibernateExtension.enhance.getEnableFieldAccessEnhancement() ) {
logger.warn( "Extended enhancement is enabled. Classes other than entities may be modified. You should consider access the entities using getter/setter methods and disable this property. Use at your own risk." );
}
final Enhancer enhancer = new Enhancer( enhancementContext );
final ClassPool classPool = new ClassPool( false );