1 package org.hibernate.tuple; 3 4 import java.io.IOException ; 5 import java.io.Serializable ; 6 import java.lang.reflect.Constructor ; 7 8 import net.sf.cglib.reflect.FastClass; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.hibernate.InstantiationException; 13 import org.hibernate.PropertyNotFoundException; 14 import org.hibernate.mapping.PersistentClass; 15 import org.hibernate.mapping.Component; 16 import org.hibernate.util.ReflectHelper; 17 18 21 public class PojoInstantiator implements Instantiator, Serializable { 22 23 private static final Log log = LogFactory.getLog(PojoInstantiator.class); 24 25 private transient Constructor constructor; 26 27 private final Class mappedClass; 28 private final transient FastClass fastClass; 29 private final boolean embeddedIdentifier; 30 private final Class proxyInterface; 31 32 public PojoInstantiator(Component component, FastClass fastClass) { 33 this.mappedClass = component.getComponentClass(); 34 this.fastClass = fastClass; 35 36 this.proxyInterface = null; 37 this.embeddedIdentifier = false; 38 39 try { 40 constructor = ReflectHelper.getDefaultConstructor(mappedClass); 41 } 42 catch ( PropertyNotFoundException pnfe ) { 43 log.info( 44 "no default (no-argument) constructor for class: " + 45 mappedClass.getName() + 46 " (class must be instantiated by Interceptor)" 47 ); 48 constructor = null; 49 } 50 } 51 52 public PojoInstantiator(PersistentClass persistentClass, FastClass fastClass) { 53 this.mappedClass = persistentClass.getMappedClass(); 54 this.proxyInterface = persistentClass.getProxyInterface(); 55 this.embeddedIdentifier = persistentClass.hasEmbeddedIdentifier(); 56 this.fastClass = fastClass; 57 58 try { 59 constructor = ReflectHelper.getDefaultConstructor(mappedClass); 60 } 61 catch ( PropertyNotFoundException pnfe ) { 62 log.info( 63 "no default (no-argument) constructor for class: " + 64 mappedClass.getName() + 65 " (class must be instantiated by Interceptor)" 66 ); 67 constructor = null; 68 } 69 } 70 71 private void readObject(java.io.ObjectInputStream stream) 72 throws ClassNotFoundException , IOException { 73 stream.defaultReadObject(); 74 constructor = ReflectHelper.getDefaultConstructor(mappedClass); 75 } 76 77 public Object instantiate() { 78 if ( ReflectHelper.isAbstractClass(mappedClass) ) { 79 throw new InstantiationException ( "Cannot instantiate abstract class or interface: ", mappedClass ); 80 } 81 else if ( fastClass != null ) { 82 try { 83 return fastClass.newInstance(); 84 } 85 catch ( Throwable t ) { 86 throw new InstantiationException ( "Could not instantiate entity with CGLIB: ", mappedClass, t ); 87 } 88 } 89 else if ( constructor == null ) { 90 throw new InstantiationException ( "No default constructor for entity: ", mappedClass ); 91 } 92 else { 93 try { 94 return constructor.newInstance( null ); 95 } 96 catch ( Exception e ) { 97 throw new InstantiationException ( "Could not instantiate entity: ", mappedClass, e ); 98 } 99 } 100 } 101 102 public Object instantiate(Serializable id) { 103 if ( embeddedIdentifier && id != null && id.getClass().equals(mappedClass) ) { 104 return id; 105 } 106 else { 107 return instantiate(); 108 } 109 } 110 111 public boolean isInstance(Object object) { 112 return mappedClass.isInstance(object) || 113 ( proxyInterface!=null && proxyInterface.isInstance(object) ); } 115 } | Popular Tags |