1 19 20 package org.apache.cayenne.reflect; 21 22 import java.lang.reflect.Field ; 23 24 import org.apache.cayenne.CayenneRuntimeException; 25 import org.apache.cayenne.util.Util; 26 27 33 public class FieldAccessor implements Accessor { 34 35 protected String propertyName; 36 protected Field field; 37 protected Object nullValue; 38 39 public FieldAccessor(Class objectClass, String propertyName, Class propertyType) { 40 if (objectClass == null) { 42 throw new IllegalArgumentException ("Null objectClass"); 43 } 44 45 if (propertyName == null) { 46 throw new IllegalArgumentException ("Null propertyName"); 47 } 48 49 this.propertyName = propertyName; 50 this.field = prepareField(objectClass, propertyName, propertyType); 51 this.nullValue = PropertyUtils.defaultNullValueForType(field.getType()); 52 } 53 54 public String getName() { 55 return propertyName; 56 } 57 58 public Object getValue(Object object) throws PropertyException { 59 try { 60 return field.get(object); 61 } 62 catch (Throwable th) { 63 throw new PropertyException( 64 "Error reading field: " + field.getName(), 65 this, 66 object, 67 th); 68 } 69 } 70 71 74 public void setValue(Object object, Object newValue) throws PropertyException { 75 if (newValue == null) { 77 newValue = this.nullValue; 78 } 79 80 try { 81 field.set(object, newValue); 82 } 83 catch (Throwable th) { 84 throw new PropertyException( 85 "Error writing field: " + field.getName(), 86 this, 87 object, 88 th); 89 } 90 } 91 92 96 protected Field prepareField(Class beanClass, String propertyName, Class propertyType) { 97 Field field; 98 99 try { 101 field = lookupFieldInHierarchy(beanClass, propertyName); 102 } 103 catch (SecurityException e) { 104 throw new CayenneRuntimeException("Error accessing field '" 105 + propertyName 106 + "' in class: " 107 + beanClass.getName(), e); 108 } 109 catch (NoSuchFieldException e) { 110 throw new CayenneRuntimeException("No field '" 111 + propertyName 112 + "' is defined in class: " 113 + beanClass.getName(), e); 114 } 115 116 if (!Util.isAccessible(field)) { 118 field.setAccessible(true); 119 } 120 121 if (propertyType != null) { 122 123 if (!propertyType.isAssignableFrom(field.getType())) { 125 126 if (!PropertyUtils.normalizeType(propertyType).isAssignableFrom( 128 PropertyUtils.normalizeType(field.getType()))) { 129 throw new CayenneRuntimeException("Expected property type '" 130 + propertyType.getName() 131 + "', got '" 132 + field.getType().getName() 133 + "'. Property: " 134 + beanClass.getName() 135 + "." 136 + propertyName); 137 } 138 } 139 } 140 141 return field; 142 } 143 144 147 protected Field lookupFieldInHierarchy(Class beanClass, String fieldName) 148 throws SecurityException , NoSuchFieldException { 149 150 153 try { 154 return beanClass.getDeclaredField(fieldName); 155 } 156 catch (NoSuchFieldException e) { 157 158 Class superClass = beanClass.getSuperclass(); 159 if (superClass == null || superClass.getName().equals(Object .class.getName())) { 160 throw e; 161 } 162 163 return lookupFieldInHierarchy(superClass, fieldName); 164 } 165 } 166 167 } 168 | Popular Tags |