1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.apache.commons.clazz.reflect.common; |
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import org.apache.commons.clazz.reflect.ReflectedClazz; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class ReflectedScalarPropertyIntrospector |
30 | |
extends ReflectedPropertyIntrospectorSupport |
31 | |
{ |
32 | 0 | protected static final AccessorMethodParser READ_METHOD_PARSER = |
33 | |
new JBReadAccessorMethodParser(); |
34 | |
|
35 | 0 | protected static final AccessorMethodParser WRITE_METHOD_PARSER = |
36 | |
new WriteAccessorMethodParser(); |
37 | |
|
38 | |
public void introspectProperties( |
39 | |
ReflectedClazz clazz, |
40 | |
Class javaClass, |
41 | |
Map parseResultMap) |
42 | |
{ |
43 | 0 | Method methods[] = javaClass.getMethods(); |
44 | |
ReflectedScalarPropertyParseResults parseResults; |
45 | |
AccessorMethodParseResults results; |
46 | 0 | for (int i = 0; i < methods.length; i++) { |
47 | 0 | Method method = methods[i]; |
48 | 0 | if (method.getDeclaringClass().equals(Object.class)) { |
49 | 0 | continue; |
50 | |
} |
51 | |
|
52 | 0 | results = getReadAccessorMethodParser().parse(method); |
53 | 0 | if (results != null) { |
54 | 0 | parseResults = |
55 | |
getParseResults( |
56 | |
clazz, |
57 | |
parseResultMap, |
58 | |
results.getPropertyName()); |
59 | 0 | parseResults.setReadMethodParseResults(results); |
60 | 0 | continue; |
61 | |
} |
62 | |
|
63 | 0 | results = getWriteAccessorMethodParser().parse(method); |
64 | 0 | if (results != null) { |
65 | 0 | parseResults = |
66 | |
getParseResults( |
67 | |
clazz, |
68 | |
parseResultMap, |
69 | |
results.getPropertyName()); |
70 | 0 | parseResults.setWriteMethodParseResults(results); |
71 | 0 | continue; |
72 | |
} |
73 | |
} |
74 | 0 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
protected AccessorMethodParser getReadAccessorMethodParser() { |
80 | 0 | return READ_METHOD_PARSER; |
81 | |
} |
82 | |
|
83 | |
protected AccessorMethodParser getWriteAccessorMethodParser() { |
84 | 0 | return WRITE_METHOD_PARSER; |
85 | |
} |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
protected ReflectedScalarPropertyParseResults getParseResults( |
92 | |
ReflectedClazz clazz, |
93 | |
Map parseResultMap, |
94 | |
String propertyName) |
95 | |
{ |
96 | 0 | ReflectedScalarPropertyParseResults parseResults = |
97 | |
(ReflectedScalarPropertyParseResults) parseResultMap.get( |
98 | |
propertyName); |
99 | 0 | if (parseResults == null) { |
100 | 0 | parseResults = |
101 | |
new ReflectedScalarPropertyParseResults(clazz, propertyName); |
102 | 0 | parseResultMap.put(propertyName, parseResults); |
103 | |
} |
104 | 0 | return parseResults; |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
protected ReflectedAccessorPairProperty createProperty( |
111 | |
ReflectedClazz clazz, |
112 | |
ReflectedPropertyParseResults parseResults) |
113 | |
{ |
114 | 0 | ReflectedScalarProperty property = |
115 | |
new ReflectedScalarProperty(clazz, parseResults.getPropertyName()); |
116 | |
|
117 | 0 | property.setType(parseResults.getPropertyType()); |
118 | 0 | property.setReadMethod(parseResults.getReadMethod()); |
119 | 0 | property.setWriteMethod(parseResults.getWriteMethod()); |
120 | 0 | return property; |
121 | |
} |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | 0 | public static class ReadAccessorMethodParser extends AccessorMethodParser { |
132 | |
protected boolean testReturnType(Class returnType) { |
133 | 0 | return !returnType.equals(Void.TYPE); |
134 | |
} |
135 | |
protected int requiredParameterCount() { |
136 | 0 | return 0; |
137 | |
} |
138 | |
protected String requiredPrefix() { |
139 | 0 | return "get"; |
140 | |
} |
141 | |
protected Class getValueType(Method method) { |
142 | 0 | return method.getReturnType(); |
143 | |
} |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | 0 | public static class JBReadAccessorMethodParser |
156 | |
extends ReadAccessorMethodParser |
157 | |
{ |
158 | |
protected String getPropertyName(Method method) { |
159 | 0 | String name = method.getName(); |
160 | 0 | String propertyName = null; |
161 | 0 | if (name.startsWith("get")) { |
162 | 0 | if (name.length() <= 3) { |
163 | 0 | return null; |
164 | |
} |
165 | 0 | if (!testFirstCharacterOfPropertyName(name.charAt(3))) { |
166 | 0 | return null; |
167 | |
} |
168 | 0 | propertyName = decapitalize(name.substring(3)); |
169 | |
} |
170 | 0 | else if (method.getReturnType().equals(Boolean.TYPE) |
171 | |
&& name.startsWith("is")) { |
172 | 0 | if (name.length() <= 2) { |
173 | 0 | return null; |
174 | |
} |
175 | 0 | if (!testFirstCharacterOfPropertyName(name.charAt(2))) { |
176 | 0 | return null; |
177 | |
} |
178 | 0 | propertyName = decapitalize(name.substring(2)); |
179 | |
} |
180 | 0 | return propertyName; |
181 | |
} |
182 | |
}; |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | 0 | public static class WriteAccessorMethodParser extends AccessorMethodParser { |
193 | |
protected int requiredParameterCount() { |
194 | 0 | return 1; |
195 | |
} |
196 | |
protected String requiredPrefix() { |
197 | 0 | return "set"; |
198 | |
} |
199 | |
protected boolean testReturnType(Class returnType) { |
200 | 0 | return returnType.equals(Void.TYPE); |
201 | |
} |
202 | |
protected Class getValueType(Method method) { |
203 | 0 | return method.getParameterTypes()[0]; |
204 | |
} |
205 | |
} |
206 | |
|
207 | |
} |