1 16 17 package org.springframework.beans; 18 19 import java.beans.PropertyChangeEvent ; 20 import java.lang.reflect.Field ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import org.springframework.util.Assert; 25 import org.springframework.util.ReflectionUtils; 26 27 44 public class DirectFieldAccessor extends AbstractPropertyAccessor { 45 46 private final Object target; 47 48 private final Map fieldMap = new HashMap (); 49 50 private final TypeConverterDelegate typeConverterDelegate; 51 52 53 57 public DirectFieldAccessor(Object target) { 58 Assert.notNull(target, "Target object must not be null"); 59 this.target = target; 60 ReflectionUtils.doWithFields(this.target.getClass(), new ReflectionUtils.FieldCallback() { 61 public void doWith(Field field) { 62 fieldMap.put(field.getName(), field); 63 } 64 }); 65 this.typeConverterDelegate = new TypeConverterDelegate(this, target); 66 setExtractOldValueForEditor(true); 67 } 68 69 70 public boolean isReadableProperty(String propertyName) throws BeansException { 71 return this.fieldMap.containsKey(propertyName); 72 } 73 74 public boolean isWritableProperty(String propertyName) throws BeansException { 75 return this.fieldMap.containsKey(propertyName); 76 } 77 78 public Class getPropertyType(String propertyName) throws BeansException { 79 Field field = (Field ) this.fieldMap.get(propertyName); 80 if (field != null) { 81 return field.getType(); 82 } 83 return null; 84 } 85 86 public Object getPropertyValue(String propertyName) throws BeansException { 87 Field field = (Field ) this.fieldMap.get(propertyName); 88 if (field == null) { 89 throw new NotReadablePropertyException( 90 this.target.getClass(), propertyName, "Field '" + propertyName + "' does not exist"); 91 } 92 try { 93 ReflectionUtils.makeAccessible(field); 94 return field.get(this.target); 95 } 96 catch (IllegalAccessException ex) { 97 throw new InvalidPropertyException(this.target.getClass(), propertyName, "Field is not accessible", ex); 98 } 99 } 100 101 public void setPropertyValue(String propertyName, Object newValue) throws BeansException { 102 Field field = (Field ) this.fieldMap.get(propertyName); 103 if (field == null) { 104 throw new NotWritablePropertyException( 105 this.target.getClass(), propertyName, "Field '" + propertyName + "' does not exist"); 106 } 107 Object oldValue = null; 108 try { 109 ReflectionUtils.makeAccessible(field); 110 oldValue = field.get(this.target); 111 Object convertedValue = 112 this.typeConverterDelegate.convertIfNecessary(propertyName, oldValue, newValue, field.getType()); 113 field.set(this.target, convertedValue); 114 } 115 catch (IllegalAccessException ex) { 116 throw new InvalidPropertyException(this.target.getClass(), propertyName, "Field is not accessible", ex); 117 } 118 catch (IllegalArgumentException ex) { 119 PropertyChangeEvent pce = new PropertyChangeEvent (this.target, propertyName, oldValue, newValue); 120 throw new TypeMismatchException(pce, field.getType(), ex); 121 } 122 } 123 124 } 125 | Popular Tags |