Skip to content

Commit c09227a

Browse files
author
Keith Donald
committed
removed dependency on java.beans
1 parent 94d690f commit c09227a

File tree

6 files changed

+246
-172
lines changed

6 files changed

+246
-172
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/BeanWrapperImpl.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.core.convert.ConversionException;
4444
import org.springframework.core.convert.ConverterNotFoundException;
4545
import org.springframework.core.convert.TypeDescriptor;
46+
import org.springframework.core.convert.TypeDescriptor.Property;
4647
import org.springframework.util.Assert;
4748
import org.springframework.util.ObjectUtils;
4849
import org.springframework.util.StringUtils;
@@ -369,11 +370,11 @@ public TypeDescriptor getPropertyTypeDescriptor(String propertyName) throws Bean
369370
if (pd != null) {
370371
if (tokens.keys != null) {
371372
if (pd.getReadMethod() != null || pd.getWriteMethod() != null) {
372-
return TypeDescriptor.nested(nestedBw.getWrappedClass(), pd, tokens.keys.length);
373+
return TypeDescriptor.nested(property(pd), tokens.keys.length);
373374
}
374375
} else {
375376
if (pd.getReadMethod() != null || pd.getWriteMethod() != null) {
376-
return new TypeDescriptor(nestedBw.getWrappedClass(), pd);
377+
return new TypeDescriptor(property(pd));
377378
}
378379
}
379380
}
@@ -493,7 +494,7 @@ private Object convertForProperty(String propertyName, Object oldValue, Object n
493494
throws TypeMismatchException {
494495
GenericTypeAwarePropertyDescriptor gpd = (GenericTypeAwarePropertyDescriptor) pd;
495496
Class<?> beanClass = gpd.getBeanClass();
496-
return convertIfNecessary(propertyName, oldValue, newValue, pd.getPropertyType(), new TypeDescriptor(beanClass, pd));
497+
return convertIfNecessary(propertyName, oldValue, newValue, pd.getPropertyType(), new TypeDescriptor(property(pd)));
497498
}
498499

499500

@@ -948,7 +949,7 @@ private void setPropertyValue(PropertyTokenHolder tokens, PropertyValue pv) thro
948949
if (isExtractOldValueForEditor() && arrayIndex < Array.getLength(propValue)) {
949950
oldValue = Array.get(propValue, arrayIndex);
950951
}
951-
Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType, TypeDescriptor.nested(getWrappedClass(), pd, tokens.keys.length));
952+
Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType, TypeDescriptor.nested(property(pd), tokens.keys.length));
952953
// TODO review this grow algorithm along side the null gap algorithm for setting lists below ... the two are inconsistent
953954
propValue = growArrayIfNecessary(propValue, arrayIndex, actualName);
954955
Array.set(propValue, arrayIndex, convertedValue);
@@ -968,7 +969,7 @@ else if (propValue instanceof List) {
968969
if (isExtractOldValueForEditor() && index < list.size()) {
969970
oldValue = list.get(index);
970971
}
971-
Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType, TypeDescriptor.nested(getWrappedClass(), pd, tokens.keys.length));
972+
Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType, TypeDescriptor.nested(property(pd), tokens.keys.length));
972973
if (index < list.size()) {
973974
list.set(index, convertedValue);
974975
}
@@ -1005,7 +1006,7 @@ else if (propValue instanceof Map) {
10051006
// Pass full property name and old value in here, since we want full
10061007
// conversion ability for map values.
10071008
Object convertedMapValue = convertIfNecessary(
1008-
propertyName, oldValue, pv.getValue(), mapValueType, TypeDescriptor.nested(getWrappedClass(), pd, tokens.keys.length));
1009+
propertyName, oldValue, pv.getValue(), mapValueType, TypeDescriptor.nested(property(pd), tokens.keys.length));
10091010
map.put(convertedMapKey, convertedMapValue);
10101011
}
10111012
else {
@@ -1167,5 +1168,10 @@ private static class PropertyTokenHolder {
11671168

11681169
public String[] keys;
11691170
}
1171+
1172+
private Property property(PropertyDescriptor pd) {
1173+
GenericTypeAwarePropertyDescriptor typeAware = (GenericTypeAwarePropertyDescriptor) pd;
1174+
return new Property(typeAware.getBeanClass(), typeAware.getReadMethod(), typeAware.getWriteMethod());
1175+
}
11701176

11711177
}

org.springframework.core/src/main/java/org/springframework/core/convert/AbstractDescriptor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ abstract class AbstractDescriptor {
2323

2424
private final Class<?> type;
2525

26-
public AbstractDescriptor(Class<?> type) {
26+
protected AbstractDescriptor(Class<?> type) {
2727
if (type == null) {
2828
throw new IllegalArgumentException("type cannot be null");
2929
}
@@ -93,15 +93,15 @@ public AbstractDescriptor nested() {
9393
// internal helpers
9494

9595
private boolean isCollection() {
96-
return getType() != null && Collection.class.isAssignableFrom(getType());
96+
return Collection.class.isAssignableFrom(getType());
9797
}
9898

9999
private boolean isArray() {
100-
return getType() != null && getType().isArray();
100+
return getType().isArray();
101101
}
102102

103103
private boolean isMap() {
104-
return getType() != null && Map.class.isAssignableFrom(getType());
104+
return Map.class.isAssignableFrom(getType());
105105
}
106106

107107
}

org.springframework.core/src/main/java/org/springframework/core/convert/BeanPropertyDescriptor.java

Lines changed: 8 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,25 @@
1515
*/
1616
package org.springframework.core.convert;
1717

18-
import java.beans.PropertyDescriptor;
1918
import java.lang.annotation.Annotation;
20-
import java.lang.reflect.Field;
21-
import java.lang.reflect.Method;
22-
import java.util.LinkedHashMap;
23-
import java.util.Map;
2419

2520
import org.springframework.core.GenericCollectionTypeResolver;
26-
import org.springframework.core.GenericTypeResolver;
2721
import org.springframework.core.MethodParameter;
28-
import org.springframework.util.ReflectionUtils;
29-
import org.springframework.util.StringUtils;
22+
import org.springframework.core.convert.TypeDescriptor.Property;
3023

3124
class BeanPropertyDescriptor extends AbstractDescriptor {
3225

33-
private final Class<?> beanClass;
34-
35-
private final PropertyDescriptor property;
26+
private final Property property;
3627

3728
private final MethodParameter methodParameter;
3829

3930
private final Annotation[] annotations;
4031

41-
public BeanPropertyDescriptor(Class<?> beanClass, PropertyDescriptor property) {
42-
super(property.getPropertyType());
43-
this.beanClass = beanClass;
32+
public BeanPropertyDescriptor(Property property) {
33+
super(property.getType());
4434
this.property = property;
45-
this.methodParameter = resolveMethodParameter();
46-
this.annotations = resolveAnnotations();
35+
this.methodParameter = property.getMethodParameter();
36+
this.annotations = property.getAnnotations();
4737
}
4838

4939
@Override
@@ -71,79 +61,13 @@ protected AbstractDescriptor nested(Class<?> type, int typeIndex) {
7161
MethodParameter methodParameter = new MethodParameter(this.methodParameter);
7262
methodParameter.increaseNestingLevel();
7363
methodParameter.setTypeIndexForCurrentLevel(typeIndex);
74-
return new BeanPropertyDescriptor(type, beanClass, property, methodParameter, annotations);
64+
return new BeanPropertyDescriptor(type, property, methodParameter, annotations);
7565
}
7666

7767
// internal
7868

79-
private MethodParameter resolveMethodParameter() {
80-
MethodParameter parameter = parameterForPropertyMethod();
81-
// needed to resolve generic property types that parameterized by sub-classes e.g. T getFoo();
82-
GenericTypeResolver.resolveParameterType(parameter, beanClass);
83-
return parameter;
84-
}
85-
86-
private MethodParameter parameterForPropertyMethod() {
87-
if (property.getReadMethod() != null) {
88-
return new MethodParameter(property.getReadMethod(), -1);
89-
} else if (property.getWriteMethod() != null) {
90-
return new MethodParameter(property.getWriteMethod(), 0);
91-
} else {
92-
throw new IllegalArgumentException("Property is neither readable or writeable");
93-
}
94-
}
95-
96-
private Annotation[] resolveAnnotations() {
97-
Map<Class<?>, Annotation> annMap = new LinkedHashMap<Class<?>, Annotation>();
98-
Method readMethod = this.property.getReadMethod();
99-
if (readMethod != null) {
100-
for (Annotation ann : readMethod.getAnnotations()) {
101-
annMap.put(ann.annotationType(), ann);
102-
}
103-
}
104-
Method writeMethod = this.property.getWriteMethod();
105-
if (writeMethod != null) {
106-
for (Annotation ann : writeMethod.getAnnotations()) {
107-
annMap.put(ann.annotationType(), ann);
108-
}
109-
}
110-
Field field = getField();
111-
if (field != null) {
112-
for (Annotation ann : field.getAnnotations()) {
113-
annMap.put(ann.annotationType(), ann);
114-
}
115-
}
116-
return annMap.values().toArray(new Annotation[annMap.size()]);
117-
}
118-
119-
private Field getField() {
120-
String name = this.property.getName();
121-
if (!StringUtils.hasLength(name)) {
122-
return null;
123-
}
124-
Class<?> declaringClass = declaringClass();
125-
Field field = ReflectionUtils.findField(declaringClass, name);
126-
if (field == null) {
127-
// Same lenient fallback checking as in CachedIntrospectionResults...
128-
field = ReflectionUtils.findField(declaringClass, name.substring(0, 1).toLowerCase() + name.substring(1));
129-
if (field == null) {
130-
field = ReflectionUtils.findField(declaringClass, name.substring(0, 1).toUpperCase() + name.substring(1));
131-
}
132-
}
133-
return field;
134-
}
135-
136-
private Class<?> declaringClass() {
137-
if (this.property.getReadMethod() != null) {
138-
return this.property.getReadMethod().getDeclaringClass();
139-
} else {
140-
return this.property.getWriteMethod().getDeclaringClass();
141-
}
142-
}
143-
144-
private BeanPropertyDescriptor(Class<?> type, Class<?> beanClass, java.beans.PropertyDescriptor propertyDescriptor, MethodParameter methodParameter, Annotation[] annotations) {
69+
private BeanPropertyDescriptor(Class<?> type, Property propertyDescriptor, MethodParameter methodParameter, Annotation[] annotations) {
14570
super(type);
146-
this.beanClass = beanClass;
14771
this.property = propertyDescriptor;
14872
this.methodParameter = methodParameter;
14973
this.annotations = annotations;

0 commit comments

Comments
 (0)