1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.enhance; |
16 |
|
|
17 |
|
import java.lang.reflect.Method; |
18 |
|
import java.lang.reflect.Modifier; |
19 |
|
import java.util.HashMap; |
20 |
|
import java.util.HashSet; |
21 |
|
import java.util.Iterator; |
22 |
|
import java.util.List; |
23 |
|
import java.util.Map; |
24 |
|
import java.util.Set; |
25 |
|
|
26 |
|
import org.apache.hivemind.ErrorLog; |
27 |
|
import org.apache.hivemind.Location; |
28 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
5 |
public class EnhancedClassValidatorImpl implements EnhancedClassValidator |
38 |
|
{ |
39 |
|
|
40 |
|
private ErrorLog _errorLog; |
41 |
|
|
42 |
|
private ClassInspector _inspector; |
43 |
|
|
44 |
|
public void validate(Class baseClass, Class enhancedClass, IComponentSpecification specification) |
45 |
|
{ |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
5 |
Set implementedMethods = new HashSet(); |
52 |
|
|
53 |
|
|
54 |
5 |
Map interfaceMethods = new HashMap(); |
55 |
|
|
56 |
5 |
Location location = specification.getLocation(); |
57 |
|
|
58 |
5 |
Class current = enhancedClass; |
59 |
|
|
60 |
|
while(true) |
61 |
|
{ |
62 |
9 |
addInterfaceMethods(current, interfaceMethods); |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
9 |
Method[] methods = current.getDeclaredMethods(); |
79 |
|
|
80 |
35 |
for(int i = 0; i < methods.length; i++) |
81 |
|
{ |
82 |
26 |
Method m = methods[i]; |
83 |
|
|
84 |
26 |
MethodSignature s = _inspector.getMethodSignature(current, m); |
85 |
|
|
86 |
26 |
boolean isAbstract = Modifier.isAbstract(m.getModifiers()); |
87 |
|
|
88 |
26 |
if (isAbstract) |
89 |
|
{ |
90 |
5 |
if (interfaceMethods.containsKey(s)) |
91 |
0 |
continue; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
5 |
if (implementedMethods.contains(s)) |
98 |
4 |
continue; |
99 |
|
|
100 |
1 |
_errorLog.error(EnhanceMessages.noImplForAbstractMethod(m, current, baseClass, enhancedClass), location, null); |
101 |
|
} |
102 |
|
|
103 |
22 |
implementedMethods.add(s); |
104 |
|
} |
105 |
|
|
106 |
9 |
current = current.getSuperclass(); |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
10 |
if (current == null || current == Object.class) |
114 |
4 |
break; |
115 |
4 |
} |
116 |
|
|
117 |
5 |
Iterator i = interfaceMethods.entrySet().iterator(); |
118 |
6 |
while(i.hasNext()) |
119 |
|
{ |
120 |
1 |
Map.Entry entry = (Map.Entry) i.next(); |
121 |
|
|
122 |
1 |
MethodSignature sig = (MethodSignature) entry.getKey(); |
123 |
|
|
124 |
1 |
if (implementedMethods.contains(sig)) |
125 |
0 |
continue; |
126 |
|
|
127 |
1 |
Method method = (Method) entry.getValue(); |
128 |
|
|
129 |
1 |
_errorLog.error(EnhanceMessages.unimplementedInterfaceMethod(method, baseClass, enhancedClass), location, null); |
130 |
1 |
} |
131 |
|
|
132 |
5 |
} |
133 |
|
|
134 |
|
private void addInterfaceMethods(Class current, Map interfaceMethods) |
135 |
|
{ |
136 |
9 |
Class[] interfaces = current.getInterfaces(); |
137 |
|
|
138 |
10 |
for(int i = 0; i < interfaces.length; i++) |
139 |
1 |
addMethodsFromInterface(interfaces[i], interfaceMethods); |
140 |
9 |
} |
141 |
|
|
142 |
|
private void addMethodsFromInterface(Class interfaceClass, Map interfaceMethods) |
143 |
|
{ |
144 |
1 |
Method[] methods = interfaceClass.getMethods(); |
145 |
|
|
146 |
2 |
for(int i = 0; i < methods.length; i++) |
147 |
|
{ |
148 |
1 |
MethodSignature sig = _inspector.getMethodSignature(interfaceClass, methods[i]); |
149 |
|
|
150 |
1 |
if (interfaceMethods.containsKey(sig)) |
151 |
0 |
continue; |
152 |
|
|
153 |
1 |
interfaceMethods.put(sig, methods[i]); |
154 |
|
} |
155 |
1 |
} |
156 |
|
|
157 |
|
public void setErrorLog(ErrorLog errorLog) |
158 |
|
{ |
159 |
2 |
_errorLog = errorLog; |
160 |
2 |
} |
161 |
|
|
162 |
|
public void setClassInspector(ClassInspector inspector) |
163 |
|
{ |
164 |
5 |
_inspector = inspector; |
165 |
5 |
} |
166 |
|
|
167 |
|
public void setClassInspectors(List inspectors) |
168 |
|
{ |
169 |
0 |
_inspector = (ClassInspector)inspectors.get(0); |
170 |
0 |
} |
171 |
|
} |