1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils2;
18
19 import java.beans.BeanInfo;
20 import java.beans.IndexedPropertyDescriptor;
21 import java.beans.IntrospectionException;
22 import java.beans.Introspector;
23 import java.beans.PropertyDescriptor;
24 import java.lang.reflect.Method;
25 import java.util.List;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class DefaultBeanIntrospector implements BeanIntrospector {
46
47
48 public static final BeanIntrospector INSTANCE = new DefaultBeanIntrospector();
49
50
51 private static final Class<?>[] LIST_CLASS_PARAMETER = new Class[] { java.util.List.class };
52
53
54 private final Log log = LogFactory.getLog(getClass());
55
56
57
58
59 private DefaultBeanIntrospector() {
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 private void handleIndexedPropertyDescriptors(final Class<?> beanClass, final PropertyDescriptor[] descriptors) {
84 for (final PropertyDescriptor pd : descriptors) {
85 if (pd instanceof IndexedPropertyDescriptor) {
86 final IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) pd;
87 final String propName = descriptor.getName().substring(0, 1).toUpperCase() + descriptor.getName().substring(1);
88
89 if (descriptor.getReadMethod() == null) {
90 final String methodName = descriptor.getIndexedReadMethod() != null ? descriptor.getIndexedReadMethod().getName() : "get" + propName;
91 final Method readMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, BeanUtils.EMPTY_CLASS_ARRAY);
92 if (readMethod != null) {
93 try {
94 descriptor.setReadMethod(readMethod);
95 } catch (final Exception e) {
96 log.error("Error setting indexed property read method", e);
97 }
98 }
99 }
100 if (descriptor.getWriteMethod() == null) {
101 final String methodName = descriptor.getIndexedWriteMethod() != null ? descriptor.getIndexedWriteMethod().getName() : "set" + propName;
102 Method writeMethod = MethodUtils.getMatchingAccessibleMethod(beanClass, methodName, LIST_CLASS_PARAMETER);
103 if (writeMethod == null) {
104 for (final Method m : beanClass.getMethods()) {
105 if (m.getName().equals(methodName)) {
106 final Class<?>[] parameterTypes = m.getParameterTypes();
107 if (parameterTypes.length == 1 && List.class.isAssignableFrom(parameterTypes[0])) {
108 writeMethod = m;
109 break;
110 }
111 }
112 }
113 }
114 if (writeMethod != null) {
115 try {
116 descriptor.setWriteMethod(writeMethod);
117 } catch (final Exception e) {
118 log.error("Error setting indexed property write method", e);
119 }
120 }
121 }
122 }
123 }
124 }
125
126
127
128
129
130
131
132 @Override
133 public void introspect(final IntrospectionContext icontext) {
134 BeanInfo beanInfo = null;
135 try {
136 beanInfo = Introspector.getBeanInfo(icontext.getTargetClass());
137 } catch (final IntrospectionException e) {
138
139 log.error("Error when inspecting class " + icontext.getTargetClass(), e);
140 return;
141 }
142
143 PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
144 if (descriptors == null) {
145 descriptors = PropertyDescriptors.EMPTY_ARRAY;
146 }
147
148 handleIndexedPropertyDescriptors(icontext.getTargetClass(), descriptors);
149 icontext.addPropertyDescriptors(descriptors);
150 }
151 }