From d78f153f0e5763aea4871c237a857f62f5137a69 Mon Sep 17 00:00:00 2001 From: Zed Date: Thu, 1 Nov 2018 18:21:28 +0800 Subject: [PATCH] fix --- .../org/hibernate/tuple/PojoInstantiator.java | 143 +++++++++--------- .../tuple/entity/DefaultEntityRegister.java | 41 +++++ 2 files changed, 110 insertions(+), 74 deletions(-) create mode 100644 fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java index cc25b3390..fc51fb364 100755 --- a/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java +++ b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/PojoInstantiator.java @@ -6,10 +6,6 @@ */ package com.fr.third.org.hibernate.tuple; -import java.io.IOException; -import java.io.Serializable; -import java.lang.reflect.Constructor; - import com.fr.third.org.hibernate.InstantiationException; import com.fr.third.org.hibernate.PropertyNotFoundException; import com.fr.third.org.hibernate.bytecode.spi.ReflectionOptimizer; @@ -17,95 +13,94 @@ import com.fr.third.org.hibernate.internal.CoreLogging; import com.fr.third.org.hibernate.internal.CoreMessageLogger; import com.fr.third.org.hibernate.internal.util.ReflectHelper; import com.fr.third.org.hibernate.mapping.Component; +import com.fr.third.org.hibernate.tuple.entity.DefaultEntityRegister; + +import java.io.IOException; +import java.io.Serializable; +import java.lang.reflect.Constructor; /** * Defines a POJO-based instantiator for use from the tuplizers. */ public class PojoInstantiator implements Instantiator, Serializable { - private static final CoreMessageLogger LOG = CoreLogging.messageLogger( PojoInstantiator.class.getName() ); + private static final CoreMessageLogger LOG = CoreLogging.messageLogger(PojoInstantiator.class.getName()); - private transient Constructor constructor; + private transient Constructor constructor; - private final Class mappedClass; - private final transient ReflectionOptimizer.InstantiationOptimizer optimizer; - private final boolean embeddedIdentifier; - private final boolean isAbstract; + private final Class mappedClass; + private final transient ReflectionOptimizer.InstantiationOptimizer optimizer; + private final boolean embeddedIdentifier; + private final boolean isAbstract; - public PojoInstantiator( - Class mappedClass, - ReflectionOptimizer.InstantiationOptimizer optimizer, - boolean embeddedIdentifier) { - this.mappedClass = mappedClass; - this.optimizer = optimizer; - this.embeddedIdentifier = embeddedIdentifier; - this.isAbstract = ReflectHelper.isAbstractClass( mappedClass ); + public PojoInstantiator( + Class mappedClass, + ReflectionOptimizer.InstantiationOptimizer optimizer, + boolean embeddedIdentifier) { + this.mappedClass = mappedClass; + this.optimizer = optimizer; + this.embeddedIdentifier = embeddedIdentifier; + this.isAbstract = ReflectHelper.isAbstractClass(mappedClass); - try { - constructor = ReflectHelper.getDefaultConstructor(mappedClass); - } - catch ( PropertyNotFoundException pnfe ) { - LOG.noDefaultConstructor( mappedClass.getName() ); - constructor = null; - } - } + try { + constructor = ReflectHelper.getDefaultConstructor(mappedClass); + } catch (PropertyNotFoundException pnfe) { + LOG.noDefaultConstructor(mappedClass.getName()); + constructor = null; + } + } - public PojoInstantiator(Component component, ReflectionOptimizer.InstantiationOptimizer optimizer) { - this.mappedClass = component.getComponentClass(); - this.isAbstract = ReflectHelper.isAbstractClass( mappedClass ); - this.optimizer = optimizer; + public PojoInstantiator(Component component, ReflectionOptimizer.InstantiationOptimizer optimizer) { + this.mappedClass = component.getComponentClass(); + this.isAbstract = ReflectHelper.isAbstractClass(mappedClass); + this.optimizer = optimizer; - this.embeddedIdentifier = false; + this.embeddedIdentifier = false; - try { - constructor = ReflectHelper.getDefaultConstructor(mappedClass); - } - catch ( PropertyNotFoundException pnfe ) { - LOG.noDefaultConstructor(mappedClass.getName()); - constructor = null; - } - } + try { + constructor = ReflectHelper.getDefaultConstructor(mappedClass); + } catch (PropertyNotFoundException pnfe) { + LOG.noDefaultConstructor(mappedClass.getName()); + constructor = null; + } + } - private void readObject(java.io.ObjectInputStream stream) throws ClassNotFoundException, IOException { - stream.defaultReadObject(); - constructor = ReflectHelper.getDefaultConstructor( mappedClass ); - } + private void readObject(java.io.ObjectInputStream stream) throws ClassNotFoundException, IOException { + stream.defaultReadObject(); + constructor = ReflectHelper.getDefaultConstructor(mappedClass); + } - public Object instantiate() { - if ( isAbstract ) { + public Object instantiate() { + if (isAbstract) { try { - return Class.forName("com.fr.schedule.base.entity.output.DefaultOutputEntity").newInstance(); + return DefaultEntityRegister.getInstance().getDefaultEntityClass().newInstance(); } catch (Exception e) { throw new InstantiationException("class not found: ", mappedClass); } - } - else if ( optimizer != null ) { - return optimizer.newInstance(); - } - else if ( constructor == null ) { - throw new InstantiationException( "No default constructor for entity: ", mappedClass ); - } - else { - try { - return applyInterception( constructor.newInstance( (Object[]) null ) ); - } - catch ( Exception e ) { - throw new InstantiationException( "Could not instantiate entity: ", mappedClass, e ); - } - } - } + } else if (optimizer != null) { + return optimizer.newInstance(); + } else if (constructor == null) { + throw new InstantiationException("No default constructor for entity: ", mappedClass); + } else { + try { + return applyInterception(constructor.newInstance((Object[]) null)); + } catch (Exception e) { + throw new InstantiationException("Could not instantiate entity: ", mappedClass, e); + } + } + } - protected Object applyInterception(Object entity) { - return entity; - } + protected Object applyInterception(Object entity) { + return entity; + } - public Object instantiate(Serializable id) { - final boolean useEmbeddedIdentifierInstanceAsEntity = embeddedIdentifier && - id != null && - id.getClass().equals(mappedClass); - return useEmbeddedIdentifierInstanceAsEntity ? id : instantiate(); - } + public Object instantiate(Serializable id) { + final boolean useEmbeddedIdentifierInstanceAsEntity = embeddedIdentifier && + id != null && + id.getClass().equals(mappedClass); + return useEmbeddedIdentifierInstanceAsEntity ? id : instantiate(); + } - public boolean isInstance(Object object) { - return mappedClass.isInstance( object ); - } + public boolean isInstance(Object object) { + return mappedClass.isInstance(object); + } } diff --git a/fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java new file mode 100644 index 000000000..fe3932ea7 --- /dev/null +++ b/fine-hibernate/src/com/fr/third/org/hibernate/tuple/entity/DefaultEntityRegister.java @@ -0,0 +1,41 @@ +package com.fr.third.org.hibernate.tuple.entity; + +/** + * Created by Zed on 2018/11/1. + */ +public class DefaultEntityRegister { + + private static volatile DefaultEntityRegister instance; + private Class defaultEntityClass = null; + + public static DefaultEntityRegister getInstance() { + if (instance == null) { + synchronized (DefaultEntityRegister.class) { + if (instance == null) { + instance = new DefaultEntityRegister(); + } + } + } + return instance; + } + + private DefaultEntityRegister() { + + } + + public void registerDefaultClass(Class defaultEntityClass) { + this.defaultEntityClass = defaultEntityClass; + } + + public void resetDefaultClass() { + this.defaultEntityClass = null; + } + + public Class getDefaultEntityClass() { + return defaultEntityClass; + } + + public void setDefaultEntityClass(Class defaultEntityClass) { + this.defaultEntityClass = defaultEntityClass; + } +}