KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: PojoComponentTuplizer.java,v 1.7 2005/07/11 17:31:50 steveebersole Exp $
2
package org.hibernate.tuple;
3
4 import java.lang.reflect.Method JavaDoc;
5
6 import net.sf.cglib.beans.BulkBean;
7 import net.sf.cglib.reflect.FastClass;
8
9 import org.hibernate.HibernateException;
10 import org.hibernate.PropertyAccessException;
11 import org.hibernate.cfg.Environment;
12 import org.hibernate.engine.SessionFactoryImplementor;
13 import org.hibernate.mapping.Component;
14 import org.hibernate.mapping.Property;
15 import org.hibernate.property.Getter;
16 import org.hibernate.property.PropertyAccessor;
17 import org.hibernate.property.PropertyAccessorFactory;
18 import org.hibernate.property.Setter;
19 import org.hibernate.util.ReflectHelper;
20
21 /**
22  * @author Gavin King
23  */

24 public class PojoComponentTuplizer extends AbstractComponentTuplizer {
25     
26     private final Class JavaDoc componentClass;
27     private transient BulkBean optimizer;
28     private transient FastClass fastClass;
29     private final Getter parentGetter;
30     private final Setter parentSetter;
31
32     public PojoComponentTuplizer(Component component) {
33         super( component );
34
35         this.componentClass = component.getComponentClass();
36
37         String JavaDoc[] getterNames = new String JavaDoc[propertySpan];
38         String JavaDoc[] setterNames = new String JavaDoc[propertySpan];
39         Class JavaDoc[] propTypes = new Class JavaDoc[propertySpan];
40         for ( int i = 0; i < propertySpan; i++ ) {
41             getterNames[i] = getters[i].getMethodName();
42             setterNames[i] = setters[i].getMethodName();
43             propTypes[i] = getters[i].getReturnType();
44         }
45
46         final String JavaDoc parentPropertyName = component.getParentProperty();
47         if ( parentPropertyName == null ) {
48             parentSetter = null;
49             parentGetter = null;
50         }
51         else {
52             PropertyAccessor pa = PropertyAccessorFactory.getPropertyAccessor( null );
53             parentSetter = pa.getSetter( componentClass, parentPropertyName );
54             parentGetter = pa.getGetter( componentClass, parentPropertyName );
55         }
56
57         if ( hasCustomAccessors || !Environment.useReflectionOptimizer() ) {
58             fastClass = null;
59             optimizer = null;
60         }
61         else {
62             fastClass = ReflectHelper.getFastClass( componentClass );
63             optimizer = ReflectHelper.getBulkBean( componentClass, getterNames, setterNames, propTypes, fastClass );
64             if (optimizer==null) fastClass = null;
65         }
66
67     }
68
69     public Class JavaDoc getMappedClass() {
70         return componentClass;
71     }
72     
73     public Object JavaDoc[] getPropertyValues(Object JavaDoc component) throws HibernateException {
74         if ( optimizer != null ) {
75             try {
76                 return optimizer.getPropertyValues( component );
77             }
78             catch ( Throwable JavaDoc t ) {
79                 throw new PropertyAccessException( t,
80                         ReflectHelper.PROPERTY_GET_EXCEPTION,
81                         false,
82                         componentClass,
83                         ReflectHelper.getPropertyName( t, optimizer )
84                     );
85             }
86         }
87         else {
88             return super.getPropertyValues(component);
89         }
90     }
91
92     public void setPropertyValues(Object JavaDoc component, Object JavaDoc[] values)
93             throws HibernateException {
94
95         if ( optimizer != null ) {
96             try {
97                 optimizer.setPropertyValues( component, values );
98                 return;
99             }
100             catch ( Throwable JavaDoc t ) {
101                 throw new PropertyAccessException( t,
102                         ReflectHelper.PROPERTY_SET_EXCEPTION,
103                         true,
104                         componentClass,
105                         ReflectHelper.getPropertyName( t, optimizer )
106                     );
107             }
108         }
109         else {
110             super.setPropertyValues(component, values);
111         }
112
113     }
114     
115     public Object JavaDoc getParent(Object JavaDoc component) {
116         return parentGetter.get( component );
117     }
118     
119     public boolean hasParentProperty() {
120         return parentGetter!=null;
121     }
122     
123     public boolean isMethodOf(Method JavaDoc method) {
124         for ( int i=0; i<propertySpan; i++ ) {
125             final Method JavaDoc getterMethod = getters[i].getMethod();
126             if ( getterMethod!=null && getterMethod.equals(method) ) return true;
127         }
128         return false;
129     }
130     
131     public void setParent(Object JavaDoc component, Object JavaDoc parent, SessionFactoryImplementor factory) {
132         parentSetter.set(component, parent, factory);
133     }
134     
135     protected Instantiator buildInstantiator(Component component) {
136         return new PojoInstantiator( component, fastClass );
137     }
138
139     protected Getter buildGetter(Component component, Property prop) {
140         return prop.getGetter( component.getComponentClass() );
141     }
142
143     protected Setter buildSetter(Component component, Property prop) {
144         return prop.getSetter( component.getComponentClass() );
145     }
146
147 }
148
Popular Tags