1 15 package org.apache.hivemind.util; 16 17 import java.beans.PropertyEditor ; 18 import java.beans.PropertyEditorManager ; 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.Method ; 21 22 import org.apache.hivemind.ApplicationRuntimeException; 23 24 29 public class PropertyAdaptor 30 { 31 private String _propertyName; 32 33 private Class _propertyType; 34 35 private Method _readMethod; 36 37 private Method _writeMethod; 38 39 PropertyAdaptor(String propertyName, Class propertyType, Method readMethod, Method writeMethod) 40 { 41 _propertyName = propertyName; 42 _propertyType = propertyType; 43 _readMethod = readMethod; 44 _writeMethod = writeMethod; 45 } 46 47 51 public String getReadMethodName() 52 { 53 return _readMethod == null ? null : _readMethod.getName(); 54 } 55 56 60 public String getWriteMethodName() 61 { 62 return _writeMethod == null ? null : _writeMethod.getName(); 63 } 64 65 public String getPropertyName() 66 { 67 return _propertyName; 68 } 69 70 public Class getPropertyType() 71 { 72 return _propertyType; 73 } 74 75 83 public void write(Object target, Object value) 84 { 85 if (_writeMethod == null) 86 throw new ApplicationRuntimeException(UtilMessages.noPropertyWriter( 87 _propertyName, 88 target), target, null, null); 89 90 try 91 { 92 _writeMethod.invoke(target, new Object [] 93 { value }); 94 95 } 96 catch (Exception ex) 97 { 98 throw new ApplicationRuntimeException(UtilMessages.writeFailure( 99 _propertyName, 100 target, 101 ex), target, null, ex); 102 } 103 } 104 105 public void smartWrite(Object target, String value) 106 { 107 Object convertedValue = convertValueForAssignment(target, value); 108 109 write(target, convertedValue); 110 } 111 112 113 private Object convertValueForAssignment(Object target, String value) 114 { 115 if (value == null || _propertyType.isInstance(value)) 116 return value; 117 118 PropertyEditor e = PropertyEditorManager.findEditor(_propertyType); 119 120 if (e == null) 121 { 122 Object convertedValue = instantiateViaStringConstructor(target, value); 123 124 if (convertedValue != null) 125 return convertedValue; 126 127 throw new ApplicationRuntimeException(UtilMessages.noPropertyEditor( 128 _propertyName, 129 target.getClass())); 130 } 131 132 try 133 { 134 e.setAsText(value); 135 136 return e.getValue(); 137 } 138 catch (Exception ex) 139 { 140 throw new ApplicationRuntimeException(UtilMessages.unableToConvert( 141 value, 142 _propertyType, 143 _propertyName, 144 target, 145 ex), null, ex); 146 } 147 } 148 149 153 154 private Object instantiateViaStringConstructor(Object target, String value) 155 { 156 try 157 { 158 Constructor c = _propertyType.getConstructor(new Class [] 159 { String .class }); 160 161 return c.newInstance(new Object [] 162 { value }); 163 } 164 catch (Exception ex) 165 { 166 return null; 167 } 168 } 169 170 173 public boolean isWritable() 174 { 175 return _writeMethod != null; 176 } 177 178 184 public Object read(Object target) 185 { 186 if (_readMethod == null) 187 throw new ApplicationRuntimeException(UtilMessages.noReader(_propertyName, target), 188 target, null, null); 189 190 try 191 { 192 return _readMethod.invoke(target, (Object []) null); 193 194 } 195 catch (Exception ex) 196 { 197 throw new ApplicationRuntimeException(UtilMessages.readFailure( 198 _propertyName, 199 target, 200 ex), target, null, ex); 201 } 202 } 203 204 207 208 public boolean isReadable() 209 { 210 return _readMethod != null; 211 } 212 213 } | Popular Tags |