1 package org.hibernate.property; 3 4 import java.lang.reflect.Field ; 5 import java.lang.reflect.Method ; 6 import java.util.Map ; 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 19 public class DirectPropertyAccessor implements PropertyAccessor { 20 21 public static final class DirectGetter implements Getter { 22 private final transient Field field; 23 private final Class clazz; 24 private final String name; 25 DirectGetter(Field field, Class clazz, String name) { 26 this.field = field; 27 this.clazz = clazz; 28 this.name = name; 29 } 30 public Object get(Object target) throws HibernateException { 31 try { 32 return field.get(target); 33 } 34 catch (Exception e) { 35 throw new PropertyAccessException(e, "could not get a field value by reflection", false, clazz, name); 36 } 37 } 38 39 public Object getForInsert(Object target, Map mergeMap, SessionImplementor session) { 40 return get( target ); 41 } 42 43 public Method getMethod() { 44 return null; 45 } 46 public String getMethodName() { 47 return null; 48 } 49 public Class getReturnType() { 50 return field.getType(); 51 } 52 53 Object readResolve() { 54 return new DirectGetter( getField(clazz, name), clazz, name ); 55 } 56 57 public String toString() { 58 return "DirectGetter(" + clazz.getName() + '.' + name + ')'; 59 } 60 } 61 62 public static final class DirectSetter implements Setter { 63 private final transient Field field; 64 private final Class clazz; 65 private final String name; 66 DirectSetter(Field field, Class clazz, String name) { 67 this.field = field; 68 this.clazz = clazz; 69 this.name = name; 70 } 71 public Method getMethod() { 72 return null; 73 } 74 public String getMethodName() { 75 return null; 76 } 77 public void set(Object target, Object value, SessionFactoryImplementor factory) throws HibernateException { 78 try { 79 field.set(target, value); 80 } 81 catch (Exception e) { 82 throw new PropertyAccessException(e, "could not set a field value by reflection", true, clazz, name); 83 } 84 } 85 86 public String toString() { 87 return "DirectSetter(" + clazz.getName() + '.' + name + ')'; 88 } 89 90 Object readResolve() { 91 return new DirectSetter( getField(clazz, name), clazz, name ); 92 } 93 } 94 95 private static Field getField(Class clazz, String name) throws PropertyNotFoundException { 96 if ( clazz==null || clazz==Object .class ) { 97 throw new PropertyNotFoundException("field not found: " + name); } 99 Field field; 100 try { 101 field = clazz.getDeclaredField(name); 102 } 103 catch (NoSuchFieldException 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 theClass, String propertyName) 111 throws PropertyNotFoundException { 112 return new DirectGetter( getField(theClass, propertyName), theClass, propertyName ); 113 } 114 115 public Setter getSetter(Class theClass, String propertyName) 116 throws PropertyNotFoundException { 117 return new DirectSetter( getField(theClass, propertyName), theClass, propertyName ); 118 } 119 120 } 121 | Popular Tags |