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.lang.reflect.Modifier;
20
21
22
23
24
25
26 public class AccessorMethodParser {
27
28
29
30
31
32 protected int requiredParameterCount() {
33 return -1;
34 }
35
36
37
38
39
40 protected String requiredPrefix() {
41 return null;
42 }
43
44
45
46
47
48
49
50
51 protected boolean testReturnType(Class javaClass) {
52 return true;
53 }
54
55
56
57
58
59
60
61
62 protected boolean testParameterType(int index, Class parameterType) {
63 return true;
64 }
65
66
67
68
69
70
71 protected String testAndRemoveSuffix(String methodName) {
72 return methodName;
73 }
74
75
76
77
78
79 protected boolean testFirstCharacterOfPropertyName(char ch) {
80 return Character.isUpperCase(ch);
81 }
82
83
84
85
86
87 protected Class getValueType(Method method) {
88 return null;
89 }
90
91
92
93
94
95
96 protected Class getParameterType(Method method) {
97 return null;
98 }
99
100
101
102
103
104
105
106
107 public AccessorMethodParseResults parse(Method method) {
108 int modifiers = method.getModifiers();
109
110
111 if (!Modifier.isPublic(modifiers) || Modifier.isStatic(modifiers)) {
112 return null;
113 }
114
115 Class returnType = method.getReturnType();
116 if (returnType == null) {
117 returnType = Void.TYPE;
118 }
119 if (!testReturnType(returnType)) {
120 return null;
121 }
122
123 int reqParamCount = requiredParameterCount();
124 if (reqParamCount != -1) {
125 Class paramTypes[] = method.getParameterTypes();
126 if (paramTypes.length != reqParamCount) {
127 return null;
128 }
129
130 for (int i = 0; i < paramTypes.length; i++) {
131 if (!testParameterType(i, paramTypes[i])) {
132 return null;
133 }
134 }
135 }
136
137 String propertyName = getPropertyName(method);
138 if (propertyName == null) {
139 return null;
140 }
141
142 return new AccessorMethodParseResults(
143 method,
144 propertyName,
145 getValueType(method),
146 getParameterType(method));
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161 protected String getPropertyName(Method method) {
162 String name = method.getName();
163 name = testAndRemoveSuffix(name);
164 if (name == null) {
165 return null;
166 }
167
168 String prefix = requiredPrefix();
169 if (prefix == null) {
170 return name;
171 }
172
173 int prefixLength = prefix.length();
174
175 if (name.length() <= prefixLength) {
176 return null;
177 }
178
179 if (!name.startsWith(prefix)) {
180 return null;
181 }
182
183 if (!testFirstCharacterOfPropertyName(name.charAt(prefixLength))) {
184 return null;
185 }
186
187 return decapitalize(name.substring(prefixLength));
188 }
189
190
191
192
193
194
195
196
197
198 protected String decapitalize(String string) {
199 char firstChar = string.charAt(0);
200 if (!Character.isUpperCase(firstChar)) {
201 return string;
202 }
203
204 int len = string.length();
205 if (len == 1) {
206 return String.valueOf(Character.toLowerCase(firstChar));
207 }
208 else if (Character.isLowerCase(string.charAt(1))) {
209 char buffer[] = new char[len];
210 buffer[0] = Character.toLowerCase(firstChar);
211 string.getChars(1, len, buffer, 1);
212 return new String(buffer);
213 }
214
215 return string;
216 }
217 }