KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > tuple > PojoInstantiator


1 //$Id: PojoInstantiator.java,v 1.4 2005/07/11 17:31:50 steveebersole Exp $
2
package org.hibernate.tuple;
3
4 import java.io.IOException JavaDoc;
5 import java.io.Serializable JavaDoc;
6 import java.lang.reflect.Constructor JavaDoc;
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 /**
19  * Defines a POJO-based instantiator for use from the tuplizers.
20  */

21 public class PojoInstantiator implements Instantiator, Serializable JavaDoc {
22
23     private static final Log log = LogFactory.getLog(PojoInstantiator.class);
24
25     private transient Constructor JavaDoc constructor;
26
27     private final Class JavaDoc mappedClass;
28     private final transient FastClass fastClass;
29     private final boolean embeddedIdentifier;
30     private final Class JavaDoc 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 JavaDoc stream)
72     throws ClassNotFoundException JavaDoc, IOException JavaDoc {
73         stream.defaultReadObject();
74         constructor = ReflectHelper.getDefaultConstructor(mappedClass);
75     }
76
77     public Object JavaDoc instantiate() {
78         if ( ReflectHelper.isAbstractClass(mappedClass) ) {
79             throw new InstantiationException JavaDoc( "Cannot instantiate abstract class or interface: ", mappedClass );
80         }
81         else if ( fastClass != null ) {
82             try {
83                 return fastClass.newInstance();
84             }
85             catch ( Throwable JavaDoc t ) {
86                 throw new InstantiationException JavaDoc( "Could not instantiate entity with CGLIB: ", mappedClass, t );
87             }
88         }
89         else if ( constructor == null ) {
90             throw new InstantiationException JavaDoc( "No default constructor for entity: ", mappedClass );
91         }
92         else {
93             try {
94                 return constructor.newInstance( null );
95             }
96             catch ( Exception JavaDoc e ) {
97                 throw new InstantiationException JavaDoc( "Could not instantiate entity: ", mappedClass, e );
98             }
99         }
100     }
101     
102     public Object JavaDoc instantiate(Serializable JavaDoc 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 JavaDoc object) {
112         return mappedClass.isInstance(object) ||
113                 ( proxyInterface!=null && proxyInterface.isInstance(object) ); //this one needed only for guessEntityMode()
114
}
115 }
Popular Tags