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.service.ClassFabUtils; |
17 |
|
|
18 |
|
import java.lang.reflect.Method; |
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
public class MethodSignatureImpl implements MethodSignature |
25 |
|
{ |
26 |
58 |
protected int _hashCode = -1; |
27 |
|
|
28 |
|
protected Class _returnType; |
29 |
|
|
30 |
|
protected String _name; |
31 |
|
|
32 |
|
protected Class[] _parameterTypes; |
33 |
|
|
34 |
|
protected Class[] _exceptionTypes; |
35 |
|
|
36 |
|
public MethodSignatureImpl(Class returnType, String name, |
37 |
|
Class[] parameterTypes, Class[] exceptionTypes) |
38 |
58 |
{ |
39 |
58 |
_returnType = returnType; |
40 |
58 |
_name = name; |
41 |
58 |
_parameterTypes = parameterTypes; |
42 |
58 |
_exceptionTypes = exceptionTypes; |
43 |
58 |
} |
44 |
|
|
45 |
|
public MethodSignatureImpl(Method m) |
46 |
|
{ |
47 |
19 |
this(m.getReturnType(), m.getName(), m.getParameterTypes(), m.getExceptionTypes()); |
48 |
19 |
} |
49 |
|
|
50 |
|
public Class[] getExceptionTypes() |
51 |
|
{ |
52 |
10 |
return _exceptionTypes; |
53 |
|
} |
54 |
|
|
55 |
|
public String getName() |
56 |
|
{ |
57 |
10 |
return _name; |
58 |
|
} |
59 |
|
|
60 |
|
public Class[] getParameterTypes() |
61 |
|
{ |
62 |
18 |
return _parameterTypes; |
63 |
|
} |
64 |
|
|
65 |
|
public Class getReturnType() |
66 |
|
{ |
67 |
29 |
return _returnType; |
68 |
|
} |
69 |
|
|
70 |
|
public int hashCode() |
71 |
|
{ |
72 |
46 |
if (_hashCode == -1) |
73 |
|
{ |
74 |
38 |
_hashCode = _returnType.hashCode(); |
75 |
|
|
76 |
38 |
_hashCode = 31 * _hashCode + _name.hashCode(); |
77 |
|
|
78 |
38 |
int count = count(_parameterTypes); |
79 |
|
|
80 |
50 |
for (int i = 0; i < count; i++) |
81 |
12 |
_hashCode = 31 * _hashCode + _parameterTypes[i].hashCode(); |
82 |
|
|
83 |
38 |
count = count(_exceptionTypes); |
84 |
|
|
85 |
46 |
for (int i = 0; i < count; i++) |
86 |
8 |
_hashCode = 31 * _hashCode + _exceptionTypes[i].hashCode(); |
87 |
|
} |
88 |
|
|
89 |
46 |
return _hashCode; |
90 |
|
} |
91 |
|
|
92 |
|
protected static int count(Object[] array) |
93 |
|
{ |
94 |
96 |
return array == null ? 0 : array.length; |
95 |
|
} |
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
public boolean equals(Object o) |
102 |
|
{ |
103 |
6 |
if (o == null || !(o instanceof MethodSignatureImpl)) |
104 |
0 |
return false; |
105 |
|
|
106 |
6 |
MethodSignatureImpl ms = (MethodSignatureImpl) o; |
107 |
|
|
108 |
6 |
if (_returnType != ms._returnType) |
109 |
2 |
return false; |
110 |
|
|
111 |
4 |
if (!_name.equals(ms._name)) |
112 |
0 |
return false; |
113 |
|
|
114 |
4 |
if (mismatch(_parameterTypes, ms._parameterTypes)) |
115 |
0 |
return false; |
116 |
|
|
117 |
4 |
return !mismatch(_exceptionTypes, ms._exceptionTypes); |
118 |
|
} |
119 |
|
|
120 |
|
protected boolean mismatch(Class[] a1, Class[] a2) |
121 |
|
{ |
122 |
9 |
int a1Count = count(a1); |
123 |
9 |
int a2Count = count(a2); |
124 |
|
|
125 |
9 |
if (a1Count != a2Count) |
126 |
0 |
return true; |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
10 |
for (int i = 0; i < a1Count; i++) |
132 |
|
{ |
133 |
1 |
if (!a1[i].isAssignableFrom(a2[i])) |
134 |
0 |
return true; |
135 |
|
} |
136 |
|
|
137 |
9 |
return false; |
138 |
|
} |
139 |
|
|
140 |
|
public String toString() |
141 |
|
{ |
142 |
0 |
StringBuffer buffer = new StringBuffer(); |
143 |
|
|
144 |
0 |
buffer.append(ClassFabUtils.getJavaClassName(_returnType)); |
145 |
0 |
buffer.append(" "); |
146 |
0 |
buffer.append(_name); |
147 |
0 |
buffer.append("("); |
148 |
|
|
149 |
0 |
for (int i = 0; i < count(_parameterTypes); i++) |
150 |
|
{ |
151 |
0 |
if (i > 0) |
152 |
0 |
buffer.append(", "); |
153 |
|
|
154 |
0 |
buffer.append(ClassFabUtils.getJavaClassName(_parameterTypes[i])); |
155 |
|
} |
156 |
|
|
157 |
0 |
buffer.append(")"); |
158 |
|
|
159 |
0 |
for (int i = 0; i < count(_exceptionTypes); i++) |
160 |
|
{ |
161 |
0 |
if (i == 0) |
162 |
0 |
buffer.append(" throws "); |
163 |
|
else |
164 |
0 |
buffer.append(", "); |
165 |
|
|
166 |
0 |
buffer.append(_exceptionTypes[i].getName()); |
167 |
|
} |
168 |
|
|
169 |
0 |
return buffer.toString(); |
170 |
|
} |
171 |
|
|
172 |
|
public String getUniqueId() |
173 |
|
{ |
174 |
0 |
StringBuffer buffer = new StringBuffer(_name); |
175 |
0 |
buffer.append("("); |
176 |
|
|
177 |
0 |
for (int i = 0; i < count(_parameterTypes); i++) |
178 |
|
{ |
179 |
0 |
if (i > 0) |
180 |
0 |
buffer.append(","); |
181 |
|
|
182 |
0 |
buffer.append(ClassFabUtils.getJavaClassName(_parameterTypes[i])); |
183 |
|
} |
184 |
|
|
185 |
0 |
buffer.append(")"); |
186 |
|
|
187 |
0 |
return buffer.toString(); |
188 |
|
} |
189 |
|
|
190 |
|
public boolean isGeneric() |
191 |
|
{ |
192 |
0 |
return false; |
193 |
|
} |
194 |
|
|
195 |
|
public boolean isOverridingSignatureOf(MethodSignature ms) |
196 |
|
{ |
197 |
1 |
if (!(ms instanceof MethodSignatureImpl)) |
198 |
0 |
return false; |
199 |
|
|
200 |
1 |
MethodSignatureImpl sig = (MethodSignatureImpl)ms; |
201 |
|
|
202 |
1 |
if (!sig._returnType.isAssignableFrom(_returnType)) |
203 |
0 |
return false; |
204 |
|
|
205 |
1 |
if (!_name.equals(sig._name)) |
206 |
0 |
return false; |
207 |
|
|
208 |
1 |
if (mismatch(_parameterTypes, sig._parameterTypes)) |
209 |
0 |
return false; |
210 |
|
|
211 |
1 |
return exceptionsEncompass(sig._exceptionTypes); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
protected boolean exceptionsEncompass(Class[] otherExceptions) |
220 |
|
{ |
221 |
1 |
int ourCount = count(_exceptionTypes); |
222 |
1 |
int otherCount = count(otherExceptions); |
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
1 |
if (ourCount == 0) |
228 |
1 |
return otherCount == 0; |
229 |
|
|
230 |
0 |
boolean[] matched = new boolean[otherCount]; |
231 |
0 |
int unmatched = otherCount; |
232 |
|
|
233 |
0 |
for (int i = 0; i < ourCount && unmatched > 0; i++) |
234 |
|
{ |
235 |
0 |
for (int j = 0; j < otherCount; j++) |
236 |
|
{ |
237 |
|
|
238 |
|
|
239 |
0 |
if (matched[j]) |
240 |
0 |
continue; |
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
0 |
if (_exceptionTypes[i].isAssignableFrom(otherExceptions[j])) |
246 |
|
{ |
247 |
0 |
matched[j] = true; |
248 |
0 |
unmatched--; |
249 |
|
} |
250 |
|
} |
251 |
|
} |
252 |
|
|
253 |
0 |
return unmatched == 0; |
254 |
|
} |
255 |
|
} |