1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package org.apache.tapestry.enhance; |
15 | |
|
16 | |
import org.apache.hivemind.ApplicationRuntimeException; |
17 | |
|
18 | |
import java.beans.Introspector; |
19 | |
import java.lang.reflect.Method; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class GenericsClassInspectorImpl implements ClassInspector |
27 | |
{ |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public MethodSignature getMethodSignature(Class implementing, Method m) |
33 | |
{ |
34 | 0 | return new GenericsMethodSignatureImpl(implementing, m); |
35 | |
} |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public MethodSignature getPropertyAccessor(Class type, String propertyName) |
41 | |
{ |
42 | |
try { |
43 | 0 | Method[] props = type.getMethods(); |
44 | 0 | Method match = null; |
45 | |
|
46 | 0 | for (int i=0; i < props.length; i++) { |
47 | |
|
48 | 0 | String propName = getPropertyName(props[i]); |
49 | |
|
50 | 0 | if (!propertyName.equals(propName)) { |
51 | 0 | continue; |
52 | |
} |
53 | |
|
54 | |
|
55 | 0 | if (match == null) |
56 | 0 | match = props[i]; |
57 | |
|
58 | |
|
59 | 0 | if (props[i].getReturnType() == void.class) { |
60 | 0 | continue; |
61 | |
} |
62 | |
|
63 | 0 | return new GenericsMethodSignatureImpl(type, props[i]); |
64 | |
} |
65 | |
|
66 | 0 | if (match != null) |
67 | 0 | return new GenericsMethodSignatureImpl(type, match); |
68 | |
|
69 | 0 | } catch (Throwable t) { |
70 | |
|
71 | 0 | throw new ApplicationRuntimeException("Error reading property " + propertyName + " from base class : " + type, t); |
72 | 0 | } |
73 | |
|
74 | 0 | return null; |
75 | |
} |
76 | |
|
77 | |
static String getPropertyName(Method m) |
78 | |
{ |
79 | 0 | String name = m.getName(); |
80 | |
|
81 | 0 | if (name.startsWith("get")) |
82 | 0 | name = name.substring(3); |
83 | 0 | else if (name.startsWith("set")) |
84 | 0 | name = name.substring(3); |
85 | 0 | else if (name.startsWith("is")) |
86 | 0 | name = name.substring(2); |
87 | |
|
88 | 0 | return Introspector.decapitalize(name); |
89 | |
} |
90 | |
} |