KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: AbstractComponentTuplizer.java,v 1.3 2005/02/19 12:58:24 oneovthafew Exp $
2
package org.hibernate.tuple;
3
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 import org.hibernate.HibernateException;
8 import org.hibernate.engine.SessionFactoryImplementor;
9 import org.hibernate.mapping.Component;
10 import org.hibernate.mapping.Property;
11 import org.hibernate.property.Getter;
12 import org.hibernate.property.Setter;
13
14 /**
15  * @author Gavin King
16  */

17 public abstract class AbstractComponentTuplizer implements ComponentTuplizer {
18     
19     protected final Getter[] getters;
20     protected final Setter[] setters;
21     protected final int propertySpan;
22     protected final Instantiator instantiator;
23     protected final boolean hasCustomAccessors;
24     
25     protected abstract Instantiator buildInstantiator(Component component);
26     protected abstract Getter buildGetter(Component component, Property prop);
27     protected abstract Setter buildSetter(Component component, Property prop);
28     
29     protected AbstractComponentTuplizer(Component component) {
30         
31         propertySpan = component.getPropertySpan();
32         
33         getters = new Getter[propertySpan];
34         setters = new Setter[propertySpan];
35         Iterator JavaDoc iter = component.getPropertyIterator();
36         boolean foundCustomAccessor=false;
37         int i=0;
38         while ( iter.hasNext() ) {
39             Property prop = (Property) iter.next();
40             getters[i] = buildGetter(component, prop);
41             setters[i] = buildSetter(component, prop);
42             if ( !prop.isBasicPropertyAccessor() ) foundCustomAccessor = true;
43             i++;
44         }
45         hasCustomAccessors = foundCustomAccessor;
46         
47         String JavaDoc[] getterNames = new String JavaDoc[propertySpan];
48         String JavaDoc[] setterNames = new String JavaDoc[propertySpan];
49         Class JavaDoc[] propTypes = new Class JavaDoc[propertySpan];
50         for ( int j = 0; j < propertySpan; j++ ) {
51             getterNames[j] = getters[j].getMethodName();
52             setterNames[j] = setters[j].getMethodName();
53             propTypes[j] = getters[j].getReturnType();
54         }
55         instantiator = buildInstantiator(component);
56     }
57     
58
59     public Object JavaDoc getPropertyValue(Object JavaDoc component, int i)
60     throws HibernateException {
61         return getters[i].get( component );
62     }
63     
64     public Object JavaDoc[] getPropertyValues(Object JavaDoc component) throws HibernateException {
65         Object JavaDoc[] values = new Object JavaDoc[propertySpan];
66         for ( int i = 0; i < propertySpan; i++ ) {
67             values[i] = getPropertyValue( component, i );
68         }
69         return values;
70     }
71     
72     public boolean isInstance(Object JavaDoc object) {
73         return instantiator.isInstance(object);
74     }
75     
76     public void setPropertyValues(Object JavaDoc component, Object JavaDoc[] values)
77     throws HibernateException {
78     
79         for ( int i = 0; i < propertySpan; i++ ) {
80             setters[i].set( component, values[i], null );
81         }
82     
83     }
84     
85     /**
86     * This method does not populate the component parent
87     */

88     public Object JavaDoc instantiate() throws HibernateException {
89         return instantiator.instantiate();
90     }
91     
92     public Object JavaDoc getParent(Object JavaDoc component) {
93         return null;
94     }
95     
96     public boolean hasParentProperty() {
97         return false;
98     }
99     
100     public boolean isMethodOf(Method JavaDoc method) {
101         return false;
102     }
103     
104     public void setParent(Object JavaDoc component, Object JavaDoc parent, SessionFactoryImplementor factory) {
105         throw new UnsupportedOperationException JavaDoc();
106     }
107
108 }
109
Popular Tags