Changeset 974 for AnalyzerBeans/trunk/src/main/java/org/eobjects/analyzer/descriptors/AbstractPropertyDescriptor.java
- Timestamp:
- 08/14/10 11:12:43 (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
AnalyzerBeans/trunk/src/main/java/org/eobjects/analyzer/descriptors/AbstractPropertyDescriptor.java
r967 r974 3 3 import java.lang.annotation.Annotation; 4 4 import java.lang.reflect.Field; 5 import java.lang.reflect.Method;6 5 import java.lang.reflect.Type; 7 6 import java.util.Set; … … 11 10 import org.eobjects.analyzer.util.SchemaNavigator; 12 11 13 public class AbstractPropertyDescriptor implements PropertyDescriptor { 12 public class AbstractPropertyDescriptor implements PropertyDescriptor, 13 Comparable<AbstractPropertyDescriptor> { 14 14 15 private final Method _method;16 15 private final Field _field; 17 16 private final Class<?> _baseType; 18 17 private final Type _genericType; 19 18 20 public AbstractPropertyDescriptor(Method setterMethod) { 21 _field = null; 22 _method = setterMethod; 23 _method.setAccessible(true); 24 Class<?>[] parameterTypes = setterMethod.getParameterTypes(); 25 if (parameterTypes.length != 1) { 26 throw new DescriptorException("The method " + setterMethod 27 + " defines " + parameterTypes.length 28 + " parameters, a single parameter is required"); 19 public AbstractPropertyDescriptor(Field field) { 20 if (field == null) { 21 throw new IllegalArgumentException("field cannot be null"); 29 22 } 30 _baseType = parameterTypes[0];31 _genericType = _method.getGenericParameterTypes()[0];32 init();33 }34 35 public AbstractPropertyDescriptor(Field field) {36 _method = null;37 23 _field = field; 38 24 _field.setAccessible(true); … … 52 38 @Override 53 39 public String getName() { 54 return (_method == null ? _field.getName() : _method.getName());40 return _field.getName(); 55 41 } 56 42 57 43 @Override 58 public void assignValue(Object bean, Object value)44 public void setValue(Object bean, Object value) 59 45 throws IllegalArgumentException { 60 46 try { 61 if (_method != null) { 62 _method.invoke(bean, value); 63 } else { 64 _field.set(bean, value); 65 } 47 _field.set(bean, value); 66 48 } catch (Exception e) { 67 49 throw new IllegalStateException("Could not assign value '" + value 68 + "' to " + (_method == null ? _field : _method), e); 50 + "' to " + _field, e); 51 } 52 } 53 54 @Override 55 public Object getValue(Object bean) throws IllegalArgumentException { 56 if (bean == null) { 57 throw new IllegalArgumentException("bean cannot be null"); 58 } 59 try { 60 return _field.get(bean); 61 } catch (Exception e) { 62 throw new IllegalArgumentException("Could not retrieve property '" 63 + getName() + "' from bean: " + bean); 69 64 } 70 65 } … … 72 67 @Override 73 68 public Set<Annotation> getAnnotations() { 74 if (_field == null) { 75 return CollectionUtils.set(_method.getAnnotations()); 76 } else { 77 return CollectionUtils.set(_field.getAnnotations()); 78 } 69 return CollectionUtils.set(_field.getAnnotations()); 79 70 } 80 71 81 72 @Override 82 73 public <A extends Annotation> A getAnnotation(Class<A> annotationClass) { 83 if (_field == null) { 84 return _method.getAnnotation(annotationClass); 85 } else { 86 return _field.getAnnotation(annotationClass); 87 } 74 return _field.getAnnotation(annotationClass); 88 75 } 89 76 … … 116 103 int result = 1; 117 104 result = prime * result + ((_field == null) ? 0 : _field.hashCode()); 118 result = prime * result + ((_method == null) ? 0 : _method.hashCode());119 105 return result; 120 106 } … … 134 120 } else if (!_field.equals(other._field)) 135 121 return false; 136 if (_method == null) {137 if (other._method != null)138 return false;139 } else if (!_method.equals(other._method))140 return false;141 122 return true; 142 123 } … … 144 125 @Override 145 126 public String toString() { 146 if (_field == null) { 147 return getClass().getSimpleName() + "[method=" + _method.getName() 148 + ",baseType=" + _baseType + "]"; 149 } else { 150 return getClass().getSimpleName() + "[field=" + _field.getName() 151 + ",baseType=" + _baseType + "]"; 127 return getClass().getSimpleName() + "[field=" + _field.getName() 128 + ",baseType=" + _baseType + "]"; 129 } 130 131 @Override 132 public int compareTo(AbstractPropertyDescriptor o) { 133 Field otherField = o._field; 134 if (_field == otherField) { 135 return 0; 152 136 } 137 return _field.toString().compareTo(otherField.toString()); 153 138 } 154 139 }
Note: See TracChangeset
for help on using the changeset viewer.
