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 | 0 | 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 | 0 | Set implementedMethods = new HashSet(); |
52 | |
|
53 | |
|
54 | 0 | Map interfaceMethods = new HashMap(); |
55 | |
|
56 | 0 | Location location = specification.getLocation(); |
57 | |
|
58 | 0 | Class current = enhancedClass; |
59 | |
|
60 | |
while(true) |
61 | |
{ |
62 | 0 | addInterfaceMethods(current, interfaceMethods); |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 0 | Method[] methods = current.getDeclaredMethods(); |
79 | |
|
80 | 0 | for(int i = 0; i < methods.length; i++) |
81 | |
{ |
82 | 0 | Method m = methods[i]; |
83 | |
|
84 | 0 | MethodSignature s = _inspector.getMethodSignature(current, m); |
85 | |
|
86 | 0 | boolean isAbstract = Modifier.isAbstract(m.getModifiers()); |
87 | |
|
88 | 0 | if (isAbstract) |
89 | |
{ |
90 | 0 | if (interfaceMethods.containsKey(s)) |
91 | 0 | continue; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | 0 | if (implementedMethods.contains(s)) |
98 | 0 | continue; |
99 | |
|
100 | 0 | _errorLog.error(EnhanceMessages.noImplForAbstractMethod(m, current, baseClass, enhancedClass), location, null); |
101 | |
} |
102 | |
|
103 | 0 | implementedMethods.add(s); |
104 | |
} |
105 | |
|
106 | 0 | current = current.getSuperclass(); |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | 0 | if (current == null || current == Object.class) |
114 | 0 | break; |
115 | 0 | } |
116 | |
|
117 | 0 | Iterator i = interfaceMethods.entrySet().iterator(); |
118 | 0 | while(i.hasNext()) |
119 | |
{ |
120 | 0 | Map.Entry entry = (Map.Entry) i.next(); |
121 | |
|
122 | 0 | MethodSignature sig = (MethodSignature) entry.getKey(); |
123 | |
|
124 | 0 | if (implementedMethods.contains(sig)) |
125 | 0 | continue; |
126 | |
|
127 | 0 | Method method = (Method) entry.getValue(); |
128 | |
|
129 | 0 | _errorLog.error(EnhanceMessages.unimplementedInterfaceMethod(method, baseClass, enhancedClass), location, null); |
130 | 0 | } |
131 | |
|
132 | 0 | } |
133 | |
|
134 | |
private void addInterfaceMethods(Class current, Map interfaceMethods) |
135 | |
{ |
136 | 0 | Class[] interfaces = current.getInterfaces(); |
137 | |
|
138 | 0 | for(int i = 0; i < interfaces.length; i++) |
139 | 0 | addMethodsFromInterface(interfaces[i], interfaceMethods); |
140 | 0 | } |
141 | |
|
142 | |
private void addMethodsFromInterface(Class interfaceClass, Map interfaceMethods) |
143 | |
{ |
144 | 0 | Method[] methods = interfaceClass.getMethods(); |
145 | |
|
146 | 0 | for(int i = 0; i < methods.length; i++) |
147 | |
{ |
148 | 0 | MethodSignature sig = _inspector.getMethodSignature(interfaceClass, methods[i]); |
149 | |
|
150 | 0 | if (interfaceMethods.containsKey(sig)) |
151 | 0 | continue; |
152 | |
|
153 | 0 | interfaceMethods.put(sig, methods[i]); |
154 | |
} |
155 | 0 | } |
156 | |
|
157 | |
public void setErrorLog(ErrorLog errorLog) |
158 | |
{ |
159 | 0 | _errorLog = errorLog; |
160 | 0 | } |
161 | |
|
162 | |
public void setClassInspector(ClassInspector inspector) |
163 | |
{ |
164 | 0 | _inspector = inspector; |
165 | 0 | } |
166 | |
|
167 | |
public void setClassInspectors(List inspectors) |
168 | |
{ |
169 | 0 | _inspector = (ClassInspector)inspectors.get(0); |
170 | 0 | } |
171 | |
} |