1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils;
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
46
47
48
49 public class DefaultBeanIntrospector implements BeanIntrospector {
50
51 public static final BeanIntrospector INSTANCE = new DefaultBeanIntrospector();
52
53
54 private static final Class<?>[] EMPTY_CLASS_PARAMETERS = new Class[0];
55
56
57 private static final Class<?>[] LIST_CLASS_PARAMETER = new Class[] { java.util.List.class };
58
59
60 private final Log log = LogFactory.getLog(getClass());
61
62
63
64
65 private DefaultBeanIntrospector() {
66 }
67
68
69
70
71
72
73
74
75
76 public void introspect(final IntrospectionContext icontext) {
77 BeanInfo beanInfo = null;
78 try {
79 beanInfo = Introspector.getBeanInfo(icontext.getTargetClass());
80 } catch (final IntrospectionException e) {
81
82 log.error(
83 "Error when inspecting class " + icontext.getTargetClass(),
84 e);
85 return;
86 }
87
88 PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
89 if (descriptors == null) {
90 descriptors = new PropertyDescriptor[0];
91 }
92
93 handleIndexedPropertyDescriptors(icontext.getTargetClass(),
94 descriptors);
95 icontext.addPropertyDescriptors(descriptors);
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 private void handleIndexedPropertyDescriptors(final Class<?> beanClass,
123 final PropertyDescriptor[] descriptors) {
124 for (final PropertyDescriptor pd : descriptors) {
125 if (pd instanceof IndexedPropertyDescriptor) {
126 final IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) pd;
127 final String propName = descriptor.getName().substring(0, 1)
128 .toUpperCase()
129 + descriptor.getName().substring(1);
130
131 if (descriptor.getReadMethod() == null) {
132 final String methodName = descriptor.getIndexedReadMethod() != null ? descriptor
133 .getIndexedReadMethod().getName() : "get"
134 + propName;
135 final Method readMethod = MethodUtils
136 .getMatchingAccessibleMethod(beanClass, methodName,
137 EMPTY_CLASS_PARAMETERS);
138 if (readMethod != null) {
139 try {
140 descriptor.setReadMethod(readMethod);
141 } catch (final Exception e) {
142 log.error(
143 "Error setting indexed property read method",
144 e);
145 }
146 }
147 }
148 if (descriptor.getWriteMethod() == null) {
149 final String methodName = descriptor.getIndexedWriteMethod() != null ? descriptor
150 .getIndexedWriteMethod().getName() : "set"
151 + propName;
152 Method writeMethod = MethodUtils
153 .getMatchingAccessibleMethod(beanClass, methodName,
154 LIST_CLASS_PARAMETER);
155 if (writeMethod == null) {
156 for (final Method m : beanClass.getMethods()) {
157 if (m.getName().equals(methodName)) {
158 final Class<?>[] parameterTypes = m.getParameterTypes();
159 if (parameterTypes.length == 1
160 && List.class
161 .isAssignableFrom(parameterTypes[0])) {
162 writeMethod = m;
163 break;
164 }
165 }
166 }
167 }
168 if (writeMethod != null) {
169 try {
170 descriptor.setWriteMethod(writeMethod);
171 } catch (final Exception e) {
172 log.error(
173 "Error setting indexed property write method",
174 e);
175 }
176 }
177 }
178 }
179 }
180 }
181 }