001: //$Id: PojoInstantiator.java 9210 2006-02-03 22:15:19Z steveebersole $
002: package org.hibernate.tuple;
003:
004: import java.io.IOException;
005: import java.io.Serializable;
006: import java.lang.reflect.Constructor;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.hibernate.InstantiationException;
011: import org.hibernate.PropertyNotFoundException;
012: import org.hibernate.bytecode.ReflectionOptimizer;
013: import org.hibernate.mapping.PersistentClass;
014: import org.hibernate.mapping.Component;
015: import org.hibernate.util.ReflectHelper;
016:
017: /**
018: * Defines a POJO-based instantiator for use from the tuplizers.
019: */
020: public class PojoInstantiator implements Instantiator, Serializable {
021:
022: private static final Log log = LogFactory
023: .getLog(PojoInstantiator.class);
024:
025: private transient Constructor constructor;
026:
027: private final Class mappedClass;
028: private final transient ReflectionOptimizer.InstantiationOptimizer optimizer;
029: private final boolean embeddedIdentifier;
030: private final Class proxyInterface;
031:
032: public PojoInstantiator(Component component,
033: ReflectionOptimizer.InstantiationOptimizer optimizer) {
034: this .mappedClass = component.getComponentClass();
035: this .optimizer = optimizer;
036:
037: this .proxyInterface = null;
038: this .embeddedIdentifier = false;
039:
040: try {
041: constructor = ReflectHelper
042: .getDefaultConstructor(mappedClass);
043: } catch (PropertyNotFoundException pnfe) {
044: log.info("no default (no-argument) constructor for class: "
045: + mappedClass.getName()
046: + " (class must be instantiated by Interceptor)");
047: constructor = null;
048: }
049: }
050:
051: public PojoInstantiator(PersistentClass persistentClass,
052: ReflectionOptimizer.InstantiationOptimizer optimizer) {
053: this .mappedClass = persistentClass.getMappedClass();
054: this .proxyInterface = persistentClass.getProxyInterface();
055: this .embeddedIdentifier = persistentClass
056: .hasEmbeddedIdentifier();
057: this .optimizer = optimizer;
058:
059: try {
060: constructor = ReflectHelper
061: .getDefaultConstructor(mappedClass);
062: } catch (PropertyNotFoundException pnfe) {
063: log.info("no default (no-argument) constructor for class: "
064: + mappedClass.getName()
065: + " (class must be instantiated by Interceptor)");
066: constructor = null;
067: }
068: }
069:
070: private void readObject(java.io.ObjectInputStream stream)
071: throws ClassNotFoundException, IOException {
072: stream.defaultReadObject();
073: constructor = ReflectHelper.getDefaultConstructor(mappedClass);
074: }
075:
076: public Object instantiate() {
077: if (ReflectHelper.isAbstractClass(mappedClass)) {
078: throw new InstantiationException(
079: "Cannot instantiate abstract class or interface: ",
080: mappedClass);
081: } else if (optimizer != null) {
082: return optimizer.newInstance();
083: } else if (constructor == null) {
084: throw new InstantiationException(
085: "No default constructor for entity: ", mappedClass);
086: } else {
087: try {
088: return constructor.newInstance(null);
089: } catch (Exception e) {
090: throw new InstantiationException(
091: "Could not instantiate entity: ", mappedClass,
092: e);
093: }
094: }
095: }
096:
097: public Object instantiate(Serializable id) {
098: final boolean useEmbeddedIdentifierInstanceAsEntity = embeddedIdentifier
099: && id != null && id.getClass().equals(mappedClass);
100: return useEmbeddedIdentifierInstanceAsEntity ? id
101: : instantiate();
102: }
103:
104: public boolean isInstance(Object object) {
105: return mappedClass.isInstance(object)
106: || (proxyInterface != null && proxyInterface
107: .isInstance(object)); //this one needed only for guessEntityMode()
108: }
109: }
|