KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > property > DirectPropertyAccessor


1 //$Id: DirectPropertyAccessor.java,v 1.9 2005/07/16 22:20:47 oneovthafew Exp $
2
package org.hibernate.property;
3
4 import java.lang.reflect.Field JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.hibernate.HibernateException;
9 import org.hibernate.PropertyAccessException;
10 import org.hibernate.PropertyNotFoundException;
11 import org.hibernate.engine.SessionFactoryImplementor;
12 import org.hibernate.engine.SessionImplementor;
13 import org.hibernate.util.ReflectHelper;
14
15 /**
16  * Accesses fields directly.
17  * @author Gavin King
18  */

19 public class DirectPropertyAccessor implements PropertyAccessor {
20
21     public static final class DirectGetter implements Getter {
22         private final transient Field JavaDoc field;
23         private final Class JavaDoc clazz;
24         private final String JavaDoc name;
25         DirectGetter(Field JavaDoc field, Class JavaDoc clazz, String JavaDoc name) {
26             this.field = field;
27             this.clazz = clazz;
28             this.name = name;
29         }
30         public Object JavaDoc get(Object JavaDoc target) throws HibernateException {
31             try {
32                 return field.get(target);
33             }
34             catch (Exception JavaDoc e) {
35                 throw new PropertyAccessException(e, "could not get a field value by reflection", false, clazz, name);
36             }
37         }
38
39         public Object JavaDoc getForInsert(Object JavaDoc target, Map JavaDoc mergeMap, SessionImplementor session) {
40             return get( target );
41         }
42
43         public Method JavaDoc getMethod() {
44             return null;
45         }
46         public String JavaDoc getMethodName() {
47             return null;
48         }
49         public Class JavaDoc getReturnType() {
50             return field.getType();
51         }
52
53         Object JavaDoc readResolve() {
54             return new DirectGetter( getField(clazz, name), clazz, name );
55         }
56         
57         public String JavaDoc toString() {
58             return "DirectGetter(" + clazz.getName() + '.' + name + ')';
59         }
60     }
61
62     public static final class DirectSetter implements Setter {
63         private final transient Field JavaDoc field;
64         private final Class JavaDoc clazz;
65         private final String JavaDoc name;
66         DirectSetter(Field JavaDoc field, Class JavaDoc clazz, String JavaDoc name) {
67             this.field = field;
68             this.clazz = clazz;
69             this.name = name;
70         }
71         public Method JavaDoc getMethod() {
72             return null;
73         }
74         public String JavaDoc getMethodName() {
75             return null;
76         }
77         public void set(Object JavaDoc target, Object JavaDoc value, SessionFactoryImplementor factory) throws HibernateException {
78             try {
79                 field.set(target, value);
80             }
81             catch (Exception JavaDoc e) {
82                 throw new PropertyAccessException(e, "could not set a field value by reflection", true, clazz, name);
83             }
84         }
85
86         public String JavaDoc toString() {
87             return "DirectSetter(" + clazz.getName() + '.' + name + ')';
88         }
89         
90         Object JavaDoc readResolve() {
91             return new DirectSetter( getField(clazz, name), clazz, name );
92         }
93     }
94
95     private static Field JavaDoc getField(Class JavaDoc clazz, String JavaDoc name) throws PropertyNotFoundException {
96         if ( clazz==null || clazz==Object JavaDoc.class ) {
97             throw new PropertyNotFoundException("field not found: " + name); // TODO: we should report what class we started searching for the property - right now it will always end on Object.class.
98
}
99         Field JavaDoc field;
100         try {
101             field = clazz.getDeclaredField(name);
102         }
103         catch (NoSuchFieldException JavaDoc nsfe) {
104             field = getField( clazz.getSuperclass(), name );
105         }
106         if ( !ReflectHelper.isPublic(clazz, field) ) field.setAccessible(true);
107         return field;
108     }
109
110     public Getter getGetter(Class JavaDoc theClass, String JavaDoc propertyName)
111         throws PropertyNotFoundException {
112         return new DirectGetter( getField(theClass, propertyName), theClass, propertyName );
113     }
114
115     public Setter getSetter(Class JavaDoc theClass, String JavaDoc propertyName)
116         throws PropertyNotFoundException {
117         return new DirectSetter( getField(theClass, propertyName), theClass, propertyName );
118     }
119
120 }
121
Popular Tags