From c9738beaf4e60100dc6e32f80ac23bf8dbe3b0b7 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Thu, 18 Jul 2013 14:14:11 +0800 Subject: [PATCH] HHH-7919 : Miscellaneous bugfixes --- .../javassist/ProxyFactoryFactoryImpl.java | 23 +----- .../javassist/JavassistLazyInitializer.java | 71 ++++++++----------- .../pojo/javassist/JavassistProxyFactory.java | 3 +- .../tuple/entity/PojoEntityTuplizer.java | 69 +++--------------- 4 files changed, 43 insertions(+), 123 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/javassist/ProxyFactoryFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/javassist/ProxyFactoryFactoryImpl.java index 7dffeedd2c..5887acf0fa 100644 --- a/hibernate-core/src/main/java/org/hibernate/bytecode/internal/javassist/ProxyFactoryFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/bytecode/internal/javassist/ProxyFactoryFactoryImpl.java @@ -26,15 +26,14 @@ package org.hibernate.bytecode.internal.javassist; import java.lang.reflect.Method; import java.util.HashMap; -import javassist.util.proxy.MethodFilter; import javassist.util.proxy.MethodHandler; import javassist.util.proxy.ProxyObject; -import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; import org.hibernate.bytecode.spi.BasicProxyFactory; import org.hibernate.bytecode.spi.ProxyFactoryFactory; import org.hibernate.proxy.ProxyFactory; +import org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer; import org.hibernate.proxy.pojo.javassist.JavassistProxyFactory; /** @@ -69,19 +68,7 @@ public class ProxyFactoryFactoryImpl implements ProxyFactoryFactory { private final Class proxyClass; public BasicProxyFactoryImpl(Class superClass, Class[] interfaces) { - if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) { - throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" ); - } - - final javassist.util.proxy.ProxyFactory factory = new javassist.util.proxy.ProxyFactory(); - factory.setFilter( FINALIZE_FILTER ); - if ( superClass != null ) { - factory.setSuperclass( superClass ); - } - if ( interfaces != null && interfaces.length > 0 ) { - factory.setInterfaces( interfaces ); - } - proxyClass = factory.createClass(); + proxyClass = JavassistLazyInitializer.getProxyFactory( superClass, interfaces ); } public Object getProxy() { @@ -100,12 +87,6 @@ public class ProxyFactoryFactoryImpl implements ProxyFactoryFactory { } } - private static final MethodFilter FINALIZE_FILTER = new MethodFilter() { - public boolean isHandled(Method m) { - // skip finalize methods - return !( m.getParameterTypes().length == 0 && m.getName().equals( "finalize" ) ); - } - }; private static class PassThroughHandler implements MethodHandler { private HashMap data = new HashMap(); diff --git a/hibernate-core/src/main/java/org/hibernate/proxy/pojo/javassist/JavassistLazyInitializer.java b/hibernate-core/src/main/java/org/hibernate/proxy/pojo/javassist/JavassistLazyInitializer.java index a44c9ceb28..d2f53810fe 100644 --- a/hibernate-core/src/main/java/org/hibernate/proxy/pojo/javassist/JavassistLazyInitializer.java +++ b/hibernate-core/src/main/java/org/hibernate/proxy/pojo/javassist/JavassistLazyInitializer.java @@ -33,6 +33,7 @@ import javassist.util.proxy.ProxyFactory; import javassist.util.proxy.ProxyObject; import org.jboss.logging.Logger; +import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.internal.CoreMessageLogger; @@ -50,7 +51,8 @@ public class JavassistLazyInitializer extends BasicLazyInitializer implements Me private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, JavassistLazyInitializer.class.getName()); - private static final MethodFilter FINALIZE_FILTER = new MethodFilter() { + public static final MethodFilter FINALIZE_FILTER = new MethodFilter() { + @Override public boolean isHandled(Method m) { // skip finalize methods return !( m.getParameterTypes().length == 0 && m.getName().equals( "finalize" ) ); @@ -75,41 +77,22 @@ public class JavassistLazyInitializer extends BasicLazyInitializer implements Me } public static HibernateProxy getProxy( - final String entityName, - final Class persistentClass, - final Class[] interfaces, - final Method getIdentifierMethod, - final Method setIdentifierMethod, - CompositeType componentIdType, - final Serializable id, - final SessionImplementor session) throws HibernateException { - // note: interface is assumed to already contain HibernateProxy.class - try { - final JavassistLazyInitializer instance = new JavassistLazyInitializer( - entityName, - persistentClass, - interfaces, - id, - getIdentifierMethod, - setIdentifierMethod, - componentIdType, - session, - ReflectHelper.overridesEquals(persistentClass) - ); - ProxyFactory factory = new ProxyFactory(); - factory.setSuperclass( interfaces.length == 1 ? persistentClass : null ); - factory.setInterfaces( interfaces ); - factory.setFilter( FINALIZE_FILTER ); - Class cl = factory.createClass(); - final HibernateProxy proxy = ( HibernateProxy ) cl.newInstance(); - ( ( ProxyObject ) proxy ).setHandler( instance ); - instance.constructed = true; - return proxy; - } - catch ( Throwable t ) { - LOG.error(LOG.javassistEnhancementFailed(entityName), t); - throw new HibernateException(LOG.javassistEnhancementFailed(entityName), t); - } + final String entityName, final Class persistentClass, final Class[] interfaces, + final Method getIdentifierMethod, final Method setIdentifierMethod, CompositeType componentIdType, + final Serializable id, final SessionImplementor session) throws HibernateException { + + return getProxy( + getProxyFactory( persistentClass, interfaces ), + entityName, + persistentClass, + interfaces, + getIdentifierMethod, + setIdentifierMethod, + componentIdType, + id, + session, + ReflectHelper.overridesEquals( persistentClass ) + ); } public static HibernateProxy getProxy( @@ -127,7 +110,8 @@ public class JavassistLazyInitializer extends BasicLazyInitializer implements Me final JavassistLazyInitializer instance = new JavassistLazyInitializer( entityName, persistentClass, - interfaces, id, + interfaces, + id, getIdentifierMethod, setIdentifierMethod, componentIdType, @@ -151,20 +135,23 @@ public class JavassistLazyInitializer extends BasicLazyInitializer implements Me } public static Class getProxyFactory( - Class persistentClass, - Class[] interfaces) throws HibernateException { + Class superClass, Class[] interfaces) throws HibernateException { // note: interfaces is assumed to already contain HibernateProxy.class + if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) { + throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" ); + } try { ProxyFactory factory = new ProxyFactory(); - factory.setSuperclass( interfaces.length == 1 ? persistentClass : null ); + factory.setSuperclass( interfaces.length == 1 ? superClass : null ); factory.setInterfaces( interfaces ); factory.setFilter( FINALIZE_FILTER ); return factory.createClass(); } catch ( Throwable t ) { - LOG.error(LOG.javassistEnhancementFailed(persistentClass.getName()), t); - throw new HibernateException(LOG.javassistEnhancementFailed(persistentClass.getName()), t); + Class caculatedSuperClass = superClass != null ? superClass : interfaces[0]; + LOG.error( LOG.javassistEnhancementFailed( caculatedSuperClass.getName() ), t ); + throw new HibernateException( LOG.javassistEnhancementFailed( caculatedSuperClass.getName() ), t ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/proxy/pojo/javassist/JavassistProxyFactory.java b/hibernate-core/src/main/java/org/hibernate/proxy/pojo/javassist/JavassistProxyFactory.java index 19fa5932be..1d307a1321 100644 --- a/hibernate-core/src/main/java/org/hibernate/proxy/pojo/javassist/JavassistProxyFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/proxy/pojo/javassist/JavassistProxyFactory.java @@ -40,7 +40,6 @@ import org.hibernate.type.CompositeType; */ public class JavassistProxyFactory implements ProxyFactory, Serializable { - protected static final Class[] NO_CLASSES = new Class[0]; private Class persistentClass; private String entityName; private Class[] interfaces; @@ -60,7 +59,7 @@ public class JavassistProxyFactory implements ProxyFactory, Serializable { CompositeType componentIdType) throws HibernateException { this.entityName = entityName; this.persistentClass = persistentClass; - this.interfaces = interfaces.toArray(NO_CLASSES); + this.interfaces = interfaces.toArray( new Class[interfaces.size()] ); this.getIdentifierMethod = getIdentifierMethod; this.setIdentifierMethod = setIdentifierMethod; this.componentIdType = componentIdType; diff --git a/hibernate-core/src/main/java/org/hibernate/tuple/entity/PojoEntityTuplizer.java b/hibernate-core/src/main/java/org/hibernate/tuple/entity/PojoEntityTuplizer.java index 1945068df7..1456471e95 100644 --- a/hibernate-core/src/main/java/org/hibernate/tuple/entity/PojoEntityTuplizer.java +++ b/hibernate-core/src/main/java/org/hibernate/tuple/entity/PojoEntityTuplizer.java @@ -27,6 +27,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.HashSet; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; @@ -151,9 +152,6 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer { } } - /** - * {@inheritDoc} - */ @Override protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter idGetter, Setter idSetter) { // determine the id getter and setter methods from the proxy interface (if any) @@ -250,9 +248,6 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer { // return getFactory().getSettings().getBytecodeProvider().getProxyFactoryFactory().buildProxyFactory(); } - /** - * {@inheritDoc} - */ @Override protected Instantiator buildInstantiator(PersistentClass persistentClass) { if ( optimizer == null ) { @@ -263,18 +258,16 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer { } } - /** - * {@inheritDoc} - */ @Override protected ProxyFactory buildProxyFactory(EntityBinding entityBinding, Getter idGetter, Setter idSetter) { // determine the id getter and setter methods from the proxy interface (if any) // determine all interfaces needed by the resulting proxy - HashSet proxyInterfaces = new HashSet(); - proxyInterfaces.add( HibernateProxy.class ); + HashSet proxyInterfaces = new LinkedHashSet(); + Class mappedClass = entityBinding.getEntity().getClassReference(); - Class proxyInterface = entityBinding.getProxyInterfaceType().getValue(); + Class proxyInterface = entityBinding.getProxyInterfaceType() == null ? null : entityBinding.getProxyInterfaceType() + .getValue(); if ( proxyInterface!=null && !mappedClass.equals( proxyInterface ) ) { if ( ! proxyInterface.isInterface() ) { @@ -288,7 +281,7 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer { if ( mappedClass.isInterface() ) { proxyInterfaces.add( mappedClass ); } - + proxyInterfaces.add( HibernateProxy.class ); // we add the HibernateProxy.clas to the last of the set for ( EntityBinding subEntityBinding : entityBinding.getPostOrderSubEntityBindingClosure() ) { final Class subclassProxy = subEntityBinding.getProxyInterfaceType().getValue(); final Class subclassClass = subEntityBinding.getClassReference(); @@ -379,9 +372,6 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer { } } - /** - * {@inheritDoc} - */ @Override public Object[] getPropertyValues(Object entity) throws HibernateException { if ( shouldGetAllProperties( entity ) && optimizer != null && optimizer.getAccessOptimizer() != null ) { @@ -392,9 +382,6 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer { } } - /** - * {@inheritDoc} - */ @Override public Object[] getPropertyValuesToInsert(Object entity, Map mergeMap, SessionImplementor session) throws HibernateException { if ( shouldGetAllProperties( entity ) && optimizer != null && optimizer.getAccessOptimizer() != null ) { @@ -413,72 +400,47 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer { return optimizer.getAccessOptimizer().getPropertyValues( object ); } - /** - * {@inheritDoc} - */ + @Override public EntityMode getEntityMode() { return EntityMode.POJO; } - /** - * {@inheritDoc} - */ + @Override public Class getMappedClass() { return mappedClass; } - /** - * {@inheritDoc} - */ @Override public boolean isLifecycleImplementor() { return lifecycleImplementor; } - /** - * {@inheritDoc} - */ @Override protected Getter buildPropertyGetter(Property mappedProperty, PersistentClass mappedEntity) { return mappedProperty.getGetter( mappedEntity.getMappedClass() ); } - /** - * {@inheritDoc} - */ @Override protected Setter buildPropertySetter(Property mappedProperty, PersistentClass mappedEntity) { return mappedProperty.getSetter( mappedEntity.getMappedClass() ); } - /** - * {@inheritDoc} - */ @Override protected Getter buildPropertyGetter(AttributeBinding mappedProperty) { return PropertyFactory.getGetter( mappedProperty ); } - /** - * {@inheritDoc} - */ @Override protected Setter buildPropertySetter(AttributeBinding mappedProperty) { return PropertyFactory.getSetter( mappedProperty ); } - /** - * {@inheritDoc} - */ public Class getConcreteProxyClass() { return proxyInterface; } //TODO: need to make the majority of this functionality into a top-level support class for custom impl support - /** - * {@inheritDoc} - */ @Override public void afterInitialize(Object entity, boolean lazyPropertiesAreUnfetched, SessionImplementor session) { if ( isInstrumented() ) { @@ -490,9 +452,6 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer { } } - /** - * {@inheritDoc} - */ @Override public boolean hasUninitializedLazyProperties(Object entity) { if ( getEntityMetamodel().hasLazyProperties() ) { @@ -504,16 +463,12 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer { } } - /** - * {@inheritDoc} - */ + @Override public boolean isInstrumented() { return isInstrumented; } - /** - * {@inheritDoc} - */ + @Override public String determineConcreteSubclassEntityName(Object entityInstance, SessionFactoryImplementor factory) { final Class concreteEntityClass = entityInstance.getClass(); if ( concreteEntityClass == getMappedClass() ) { @@ -531,9 +486,7 @@ public class PojoEntityTuplizer extends AbstractEntityTuplizer { } } - /** - * {@inheritDoc} - */ + @Override public EntityNameResolver[] getEntityNameResolvers() { return null; }