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.List; |
20 | |
|
21 | |
import org.apache.commons.clazz.Clazz; |
22 | |
import org.apache.commons.clazz.reflect.ReflectedClazz; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class ReflectedListPropertyParseResults |
32 | |
extends ReflectedPropertyParseResults |
33 | |
{ |
34 | |
private AccessorMethodParseResults getMethodParseResults; |
35 | |
private AccessorMethodParseResults setMethodParseResults; |
36 | |
private AccessorMethodParseResults addMethodParseResults; |
37 | |
private AccessorMethodParseResults addIndexedMethodParseResults; |
38 | |
private AccessorMethodParseResults removeMethodParseResults; |
39 | |
private AccessorMethodParseResults removeIndexedMethodParseResults; |
40 | |
|
41 | |
private AccessorMethodParseResults sizeMethodParseResults; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
public ReflectedListPropertyParseResults( |
48 | |
ReflectedClazz clazz, String propertyName) |
49 | |
{ |
50 | 0 | super(clazz, propertyName); |
51 | 0 | } |
52 | |
|
53 | |
protected String getPropertyCategory() { |
54 | 0 | return "list"; |
55 | |
} |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public boolean isList() { |
62 | 0 | if (readMethodParseResults != null |
63 | |
&& isList(readMethodParseResults.getType())) { |
64 | 0 | return true; |
65 | |
} |
66 | 0 | if (writeMethodParseResults != null |
67 | |
&& isList(writeMethodParseResults.getType())) { |
68 | 0 | return true; |
69 | |
} |
70 | 0 | return false; |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
protected boolean isList(Class javaClass) { |
78 | 0 | return javaClass != null |
79 | 0 | && (List.class.isAssignableFrom(javaClass) || javaClass.isArray()); |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public Class getContentType() { |
87 | 0 | if (getMethodParseResults != null) { |
88 | 0 | return getMethodParseResults.getType(); |
89 | |
} |
90 | 0 | if (setMethodParseResults != null) { |
91 | 0 | return setMethodParseResults.getType(); |
92 | |
} |
93 | 0 | if (addMethodParseResults != null) { |
94 | 0 | return addMethodParseResults.getType(); |
95 | |
} |
96 | 0 | if (addIndexedMethodParseResults != null) { |
97 | 0 | return addIndexedMethodParseResults.getType(); |
98 | |
} |
99 | 0 | if (removeMethodParseResults != null) { |
100 | 0 | return removeMethodParseResults.getType(); |
101 | |
} |
102 | 0 | if (readMethodParseResults != null) { |
103 | 0 | Class type = readMethodParseResults.getType(); |
104 | 0 | if (type.isArray()) { |
105 | 0 | return type.getComponentType(); |
106 | |
} |
107 | |
} |
108 | 0 | if (writeMethodParseResults != null) { |
109 | 0 | Class type = writeMethodParseResults.getType(); |
110 | 0 | if (type.isArray()) { |
111 | 0 | return type.getComponentType(); |
112 | |
} |
113 | |
} |
114 | 0 | return null; |
115 | |
} |
116 | |
|
117 | |
public void setGetMethodParseResults( |
118 | |
AccessorMethodParseResults getMethodParseResults) |
119 | |
{ |
120 | 0 | checkForExtraneousAccessor( |
121 | |
this.getMethodParseResults, |
122 | |
getMethodParseResults); |
123 | 0 | this.getMethodParseResults = getMethodParseResults; |
124 | 0 | } |
125 | |
|
126 | |
public Method getGetMethod() { |
127 | 0 | if (getMethodParseResults == null) { |
128 | 0 | return null; |
129 | |
} |
130 | 0 | return getMethodParseResults.getMethod(); |
131 | |
} |
132 | |
|
133 | |
public void setSetMethodParseResults( |
134 | |
AccessorMethodParseResults setMethodParseResults) |
135 | |
{ |
136 | 0 | checkForExtraneousAccessor( |
137 | |
this.setMethodParseResults, |
138 | |
setMethodParseResults); |
139 | 0 | this.setMethodParseResults = setMethodParseResults; |
140 | 0 | } |
141 | |
|
142 | |
public Method getSetMethod() { |
143 | 0 | if (setMethodParseResults == null) { |
144 | 0 | return null; |
145 | |
} |
146 | 0 | return setMethodParseResults.getMethod(); |
147 | |
} |
148 | |
|
149 | |
public void setAddMethodParseResults( |
150 | |
AccessorMethodParseResults addMethodParseResults) |
151 | |
{ |
152 | 0 | checkForExtraneousAccessor( |
153 | |
this.addMethodParseResults, |
154 | |
addMethodParseResults); |
155 | 0 | this.addMethodParseResults = addMethodParseResults; |
156 | 0 | } |
157 | |
|
158 | |
public Method getAddMethod() { |
159 | 0 | if (addMethodParseResults == null) { |
160 | 0 | return null; |
161 | |
} |
162 | 0 | return addMethodParseResults.getMethod(); |
163 | |
} |
164 | |
|
165 | |
public void setAddIndexedMethodParseResults( |
166 | |
AccessorMethodParseResults addIndexedMethodParseResults) |
167 | |
{ |
168 | 0 | checkForExtraneousAccessor( |
169 | |
this.addIndexedMethodParseResults, |
170 | |
addIndexedMethodParseResults); |
171 | 0 | this.addIndexedMethodParseResults = addIndexedMethodParseResults; |
172 | 0 | } |
173 | |
|
174 | |
public Method getAddIndexedMethod() { |
175 | 0 | if (addIndexedMethodParseResults == null) { |
176 | 0 | return null; |
177 | |
} |
178 | 0 | return addIndexedMethodParseResults.getMethod(); |
179 | |
} |
180 | |
|
181 | |
public void setRemoveMethodParseResults( |
182 | |
AccessorMethodParseResults removeMethodParseResults) |
183 | |
{ |
184 | 0 | checkForExtraneousAccessor( |
185 | |
this.removeMethodParseResults, |
186 | |
removeMethodParseResults); |
187 | 0 | this.removeMethodParseResults = removeMethodParseResults; |
188 | 0 | } |
189 | |
|
190 | |
public Method getRemoveMethod() { |
191 | 0 | if (removeMethodParseResults == null) { |
192 | 0 | return null; |
193 | |
} |
194 | 0 | return removeMethodParseResults.getMethod(); |
195 | |
} |
196 | |
|
197 | |
public void setRemoveIndexedMethodParseResults( |
198 | |
AccessorMethodParseResults removeIndexedMethodParseResults) |
199 | |
{ |
200 | 0 | checkForExtraneousAccessor( |
201 | |
this.removeIndexedMethodParseResults, |
202 | |
removeIndexedMethodParseResults); |
203 | 0 | this.removeIndexedMethodParseResults = removeIndexedMethodParseResults; |
204 | 0 | } |
205 | |
|
206 | |
public Method getRemoveIndexedMethod() { |
207 | 0 | if (removeIndexedMethodParseResults == null) { |
208 | 0 | return null; |
209 | |
} |
210 | 0 | return removeIndexedMethodParseResults.getMethod(); |
211 | |
} |
212 | |
|
213 | |
|
214 | |
public void setSizeMethodParseResults( |
215 | |
AccessorMethodParseResults sizeMethodParseResults) |
216 | |
{ |
217 | 0 | checkForExtraneousAccessor( |
218 | |
this.sizeMethodParseResults, |
219 | |
sizeMethodParseResults); |
220 | 0 | this.sizeMethodParseResults = sizeMethodParseResults; |
221 | 0 | } |
222 | |
|
223 | |
public Method getSizeMethod() { |
224 | 0 | if (sizeMethodParseResults == null) { |
225 | 0 | return null; |
226 | |
} |
227 | 0 | return sizeMethodParseResults.getMethod(); |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
public void merge(ReflectedListPropertyParseResults other) { |
239 | 0 | super.merge(other); |
240 | 0 | if (other.readMethodParseResults != null) { |
241 | 0 | setReadMethodParseResults(other.readMethodParseResults); |
242 | |
} |
243 | 0 | if (other.writeMethodParseResults != null) { |
244 | 0 | setWriteMethodParseResults(other.writeMethodParseResults); |
245 | |
} |
246 | 0 | if (other.getMethodParseResults != null) { |
247 | 0 | setGetMethodParseResults(other.getMethodParseResults); |
248 | |
} |
249 | 0 | if (other.setMethodParseResults != null) { |
250 | 0 | setSetMethodParseResults(other.setMethodParseResults); |
251 | |
} |
252 | 0 | if (other.addMethodParseResults != null) { |
253 | 0 | setAddMethodParseResults(other.addMethodParseResults); |
254 | |
} |
255 | 0 | if (other.addIndexedMethodParseResults != null) { |
256 | 0 | setAddIndexedMethodParseResults(other.addIndexedMethodParseResults); |
257 | |
} |
258 | 0 | if (other.removeMethodParseResults != null) { |
259 | 0 | setRemoveMethodParseResults(other.removeMethodParseResults); |
260 | |
} |
261 | 0 | if (other.sizeMethodParseResults != null) { |
262 | 0 | setSizeMethodParseResults(other.sizeMethodParseResults); |
263 | |
} |
264 | 0 | } |
265 | |
|
266 | |
protected void appendDescription(StringBuffer buffer) { |
267 | 0 | super.appendDescription(buffer); |
268 | 0 | Class contentType = getContentType(); |
269 | 0 | if (contentType != null) { |
270 | 0 | buffer.append("\n [content type] "); |
271 | 0 | buffer.append(Clazz.getCanonicalClassName(contentType)); |
272 | |
} |
273 | 0 | } |
274 | |
|
275 | |
protected void appendMethodDescriptions(StringBuffer buffer) { |
276 | 0 | super.appendMethodDescriptions(buffer); |
277 | 0 | if (getMethodParseResults != null) { |
278 | 0 | buffer.append("\n [get~(int)] "); |
279 | 0 | buffer.append(getMethodParseResults.getMethod()); |
280 | |
} |
281 | 0 | if (setMethodParseResults != null) { |
282 | 0 | buffer.append("\n [set~(int,v)] "); |
283 | 0 | buffer.append(setMethodParseResults.getMethod()); |
284 | |
} |
285 | 0 | if (addMethodParseResults != null) { |
286 | 0 | buffer.append("\n [add~(v)] "); |
287 | 0 | buffer.append(addMethodParseResults.getMethod()); |
288 | |
} |
289 | 0 | if (addIndexedMethodParseResults != null) { |
290 | 0 | buffer.append("\n [add~(int,v)] "); |
291 | 0 | buffer.append(addIndexedMethodParseResults.getMethod()); |
292 | |
} |
293 | 0 | if (removeMethodParseResults != null) { |
294 | 0 | buffer.append("\n [remove~(v)] "); |
295 | 0 | buffer.append(removeMethodParseResults.getMethod()); |
296 | |
} |
297 | 0 | if (removeIndexedMethodParseResults != null) { |
298 | 0 | buffer.append("\n [remove~(i)] "); |
299 | 0 | buffer.append(removeIndexedMethodParseResults.getMethod()); |
300 | |
} |
301 | 0 | if (sizeMethodParseResults != null) { |
302 | 0 | buffer.append("\n [get~Count()] "); |
303 | 0 | buffer.append(sizeMethodParseResults.getMethod()); |
304 | |
} |
305 | 0 | } |
306 | |
|
307 | |
public boolean checkConsistency() { |
308 | 0 | if (!super.checkConsistency()) { |
309 | 0 | return false; |
310 | |
} |
311 | |
|
312 | 0 | if (readMethodParseResults == null && getMethodParseResults == null) { |
313 | 0 | return false; |
314 | |
} |
315 | |
|
316 | 0 | if (readMethodParseResults != null) { |
317 | 0 | Class type = readMethodParseResults.getType(); |
318 | 0 | if (writeMethodParseResults != null) { |
319 | 0 | if (!type.equals(writeMethodParseResults.getType())) { |
320 | 0 | return false; |
321 | |
} |
322 | |
} |
323 | |
} |
324 | |
|
325 | 0 | Class contentType = null; |
326 | |
|
327 | 0 | if (getMethodParseResults != null) { |
328 | 0 | contentType = getMethodParseResults.getType(); |
329 | |
} |
330 | |
|
331 | 0 | if (setMethodParseResults != null) { |
332 | 0 | if (contentType == null) { |
333 | 0 | contentType = setMethodParseResults.getType(); |
334 | |
} |
335 | |
else { |
336 | 0 | if (!contentType.equals(setMethodParseResults.getType())) { |
337 | 0 | return false; |
338 | |
} |
339 | |
} |
340 | |
} |
341 | |
|
342 | 0 | if (addMethodParseResults != null) { |
343 | 0 | if (contentType == null) { |
344 | 0 | contentType = addMethodParseResults.getType(); |
345 | |
} |
346 | |
else { |
347 | 0 | if (!contentType.equals(addMethodParseResults.getType())) { |
348 | 0 | return false; |
349 | |
} |
350 | |
} |
351 | |
} |
352 | |
|
353 | 0 | if (addIndexedMethodParseResults != null) { |
354 | 0 | if (contentType == null) { |
355 | 0 | contentType = addIndexedMethodParseResults.getType(); |
356 | |
} |
357 | |
else { |
358 | 0 | if (!contentType.equals( |
359 | |
addIndexedMethodParseResults.getType())) { |
360 | 0 | return false; |
361 | |
} |
362 | |
} |
363 | |
} |
364 | |
|
365 | 0 | if (removeMethodParseResults != null) { |
366 | 0 | if (contentType != null) { |
367 | 0 | if (!contentType.equals(removeMethodParseResults.getType())) { |
368 | 0 | return false; |
369 | |
} |
370 | |
} |
371 | |
} |
372 | |
|
373 | 0 | return true; |
374 | |
} |
375 | |
|
376 | |
protected boolean appendInconsistencyDescriptions(StringBuffer buffer) { |
377 | 0 | if (!super.appendInconsistencyDescriptions(buffer)) { |
378 | 0 | return false; |
379 | |
} |
380 | |
|
381 | 0 | if (readMethodParseResults == null && getMethodParseResults == null) { |
382 | 0 | buffer.append( |
383 | |
"\n - Does not have either get() or get(int) method"); |
384 | 0 | return true; |
385 | |
} |
386 | |
|
387 | 0 | if (readMethodParseResults != null) { |
388 | 0 | Class type = readMethodParseResults.getType(); |
389 | 0 | if (writeMethodParseResults != null) { |
390 | 0 | if (!type.equals(writeMethodParseResults.getType())) { |
391 | 0 | buffer.append( |
392 | |
"\n - Get() and set(v) types do not match"); |
393 | |
} |
394 | |
} |
395 | |
} |
396 | |
|
397 | 0 | Class contentType = null; |
398 | |
|
399 | 0 | if (getMethodParseResults != null) { |
400 | 0 | contentType = getMethodParseResults.getType(); |
401 | |
} |
402 | |
|
403 | 0 | if (setMethodParseResults != null) { |
404 | 0 | if (contentType == null) { |
405 | 0 | contentType = setMethodParseResults.getType(); |
406 | |
} |
407 | |
else { |
408 | 0 | if (!contentType.equals(setMethodParseResults.getType())) { |
409 | 0 | buffer.append( |
410 | |
"\n - Content type mismatch between " |
411 | |
+ "get(int) and set(int,v)"); |
412 | |
} |
413 | |
} |
414 | |
} |
415 | |
|
416 | 0 | if (addMethodParseResults != null) { |
417 | 0 | if (contentType == null) { |
418 | 0 | contentType = addMethodParseResults.getType(); |
419 | |
} |
420 | |
else { |
421 | 0 | if (!contentType.equals(addMethodParseResults.getType())) { |
422 | 0 | buffer.append( |
423 | |
"\n - Content type mismatch between " |
424 | |
+ "get(int) and add(v)"); |
425 | |
} |
426 | |
} |
427 | |
} |
428 | |
|
429 | 0 | if (addIndexedMethodParseResults != null) { |
430 | 0 | if (contentType == null) { |
431 | 0 | contentType = addIndexedMethodParseResults.getType(); |
432 | |
} |
433 | |
else { |
434 | 0 | if (!contentType.equals( |
435 | |
addIndexedMethodParseResults.getType())) { |
436 | 0 | buffer.append( |
437 | |
"\n - Content type mismatch between " |
438 | |
+ "get(int) and add(int,v)"); |
439 | |
} |
440 | |
} |
441 | |
} |
442 | |
|
443 | 0 | if (removeMethodParseResults != null) { |
444 | 0 | if (contentType != null) { |
445 | 0 | Class removeType = removeMethodParseResults.getType(); |
446 | 0 | if (removeType != null && !contentType.equals(removeType)) { |
447 | 0 | buffer.append( |
448 | |
"\n - Content type mismatch between " |
449 | |
+ "get(int) and remove(v)"); |
450 | |
} |
451 | |
} |
452 | |
} |
453 | 0 | return true; |
454 | |
} |
455 | |
} |