Ignore:
Timestamp:
08/14/10 11:12:43 (22 months ago)
Author:
kasper
Message:

limited possibility of @Configured and @Provided to fields in order to make it easy to also get value of properties through PropertyDescriptor?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • AnalyzerBeans/trunk/src/main/java/org/eobjects/analyzer/descriptors/AbstractPropertyDescriptor.java

    r967 r974  
    33import java.lang.annotation.Annotation; 
    44import java.lang.reflect.Field; 
    5 import java.lang.reflect.Method; 
    65import java.lang.reflect.Type; 
    76import java.util.Set; 
     
    1110import org.eobjects.analyzer.util.SchemaNavigator; 
    1211 
    13 public class AbstractPropertyDescriptor implements PropertyDescriptor { 
     12public class AbstractPropertyDescriptor implements PropertyDescriptor, 
     13                Comparable<AbstractPropertyDescriptor> { 
    1414 
    15         private final Method _method; 
    1615        private final Field _field; 
    1716        private final Class<?> _baseType; 
    1817        private final Type _genericType; 
    1918 
    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"); 
    2922                } 
    30                 _baseType = parameterTypes[0]; 
    31                 _genericType = _method.getGenericParameterTypes()[0]; 
    32                 init(); 
    33         } 
    34  
    35         public AbstractPropertyDescriptor(Field field) { 
    36                 _method = null; 
    3723                _field = field; 
    3824                _field.setAccessible(true); 
     
    5238        @Override 
    5339        public String getName() { 
    54                 return (_method == null ? _field.getName() : _method.getName()); 
     40                return _field.getName(); 
    5541        } 
    5642 
    5743        @Override 
    58         public void assignValue(Object bean, Object value) 
     44        public void setValue(Object bean, Object value) 
    5945                        throws IllegalArgumentException { 
    6046                try { 
    61                         if (_method != null) { 
    62                                 _method.invoke(bean, value); 
    63                         } else { 
    64                                 _field.set(bean, value); 
    65                         } 
     47                        _field.set(bean, value); 
    6648                } catch (Exception e) { 
    6749                        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); 
    6964                } 
    7065        } 
     
    7267        @Override 
    7368        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()); 
    7970        } 
    8071 
    8172        @Override 
    8273        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); 
    8875        } 
    8976 
     
    116103                int result = 1; 
    117104                result = prime * result + ((_field == null) ? 0 : _field.hashCode()); 
    118                 result = prime * result + ((_method == null) ? 0 : _method.hashCode()); 
    119105                return result; 
    120106        } 
     
    134120                } else if (!_field.equals(other._field)) 
    135121                        return false; 
    136                 if (_method == null) { 
    137                         if (other._method != null) 
    138                                 return false; 
    139                 } else if (!_method.equals(other._method)) 
    140                         return false; 
    141122                return true; 
    142123        } 
     
    144125        @Override 
    145126        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; 
    152136                } 
     137                return _field.toString().compareTo(otherField.toString()); 
    153138        } 
    154139} 
Note: See TracChangeset for help on using the changeset viewer.