|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MethodSignature.java | 98% | 98.8% | 100% | 98.6% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
14 |
|
|
15 |
package org.apache.hivemind.service;
|
|
16 |
|
|
17 |
import java.lang.reflect.Method;
|
|
18 |
|
|
19 |
/**
|
|
20 |
* A representation of a {@link java.lang.reflect.Method}, identifying the name, return type,
|
|
21 |
* parameter types and exception types. Actual Method objects are tied to a particular class, and
|
|
22 |
* don't compare well with other otherwise identical Methods from other classes or interface;
|
|
23 |
* MethodSignatures are distinct from classes and compare well.
|
|
24 |
* <p>
|
|
25 |
* Because the intended purpose is to compare methods from interfaces (which are always public and
|
|
26 |
* abstract) we don't bother to actually track the modifiers. In addition, at this time,
|
|
27 |
* MethodSignature <em>does not distinguish between instance and static
|
|
28 |
* methods</em>.
|
|
29 |
*
|
|
30 |
* @author Howard Lewis Ship
|
|
31 |
*/
|
|
32 |
public class MethodSignature |
|
33 |
{ |
|
34 |
private int _hashCode = -1; |
|
35 |
|
|
36 |
private Class _returnType;
|
|
37 |
|
|
38 |
private String _name;
|
|
39 |
|
|
40 |
private Class[] _parameterTypes;
|
|
41 |
|
|
42 |
private Class[] _exceptionTypes;
|
|
43 |
|
|
44 | 8200 |
public MethodSignature(Class returnType, String name, Class[] parameterTypes,
|
45 |
Class[] exceptionTypes) |
|
46 |
{ |
|
47 | 8200 |
_returnType = returnType; |
48 | 8200 |
_name = name; |
49 | 8200 |
_parameterTypes = parameterTypes; |
50 | 8200 |
_exceptionTypes = exceptionTypes; |
51 |
} |
|
52 |
|
|
53 | 1738 |
public MethodSignature(Method m)
|
54 |
{ |
|
55 | 1738 |
this(m.getReturnType(), m.getName(), m.getParameterTypes(), m.getExceptionTypes());
|
56 |
} |
|
57 |
|
|
58 |
/**
|
|
59 |
* Returns the exceptions for this method. Caution: do not modify the returned array. May return
|
|
60 |
* null.
|
|
61 |
*/
|
|
62 | 8115 |
public Class[] getExceptionTypes()
|
63 |
{ |
|
64 | 8115 |
return _exceptionTypes;
|
65 |
} |
|
66 |
|
|
67 | 9791 |
public String getName()
|
68 |
{ |
|
69 | 9791 |
return _name;
|
70 |
} |
|
71 |
|
|
72 |
/**
|
|
73 |
* Returns the parameter types for this method. May return null. Caution: do not modify the
|
|
74 |
* returned array.
|
|
75 |
*/
|
|
76 | 8129 |
public Class[] getParameterTypes()
|
77 |
{ |
|
78 | 8129 |
return _parameterTypes;
|
79 |
} |
|
80 |
|
|
81 | 8119 |
public Class getReturnType()
|
82 |
{ |
|
83 | 8119 |
return _returnType;
|
84 |
} |
|
85 |
|
|
86 | 16204 |
public int hashCode() |
87 |
{ |
|
88 | 16204 |
if (_hashCode == -1)
|
89 |
{ |
|
90 |
|
|
91 | 8107 |
_hashCode = _returnType.hashCode(); |
92 |
|
|
93 | 8107 |
_hashCode = 31 * _hashCode + _name.hashCode(); |
94 |
|
|
95 | 8107 |
int count = count(_parameterTypes);
|
96 |
|
|
97 | 8107 |
for (int i = 0; i < count; i++) |
98 | 5110 |
_hashCode = 31 * _hashCode + _parameterTypes[i].hashCode(); |
99 |
|
|
100 | 8107 |
count = count(_exceptionTypes); |
101 |
|
|
102 | 8107 |
for (int i = 0; i < count; i++) |
103 | 22 |
_hashCode = 31 * _hashCode + _exceptionTypes[i].hashCode(); |
104 |
} |
|
105 |
|
|
106 | 16204 |
return _hashCode;
|
107 |
} |
|
108 |
|
|
109 | 22415 |
private static int count(Object[] array) |
110 |
{ |
|
111 | 22415 |
return array == null ? 0 : array.length; |
112 |
} |
|
113 |
|
|
114 |
/**
|
|
115 |
* Returns true if the other object is an instance of MethodSignature with identical values for
|
|
116 |
* return type, name, parameter types and exception types.
|
|
117 |
*/
|
|
118 | 20 |
public boolean equals(Object o) |
119 |
{ |
|
120 | 20 |
if (o == null || !(o instanceof MethodSignature)) |
121 | 2 |
return false; |
122 |
|
|
123 | 18 |
MethodSignature ms = (MethodSignature) o; |
124 |
|
|
125 | 18 |
if (_returnType != ms._returnType)
|
126 | 0 |
return false; |
127 |
|
|
128 | 18 |
if (!_name.equals(ms._name))
|
129 | 1 |
return false; |
130 |
|
|
131 | 17 |
if (mismatch(_parameterTypes, ms._parameterTypes))
|
132 | 1 |
return false; |
133 |
|
|
134 | 16 |
return !mismatch(_exceptionTypes, ms._exceptionTypes);
|
135 |
} |
|
136 |
|
|
137 | 42 |
private boolean mismatch(Class[] a1, Class[] a2) |
138 |
{ |
|
139 | 42 |
int a1Count = count(a1);
|
140 | 42 |
int a2Count = count(a2);
|
141 |
|
|
142 | 42 |
if (a1Count != a2Count)
|
143 | 1 |
return true; |
144 |
|
|
145 |
// Hm. What if order is important (for exceptions)? We're really saying here that they
|
|
146 |
// were derived from the name Method.
|
|
147 |
|
|
148 | 41 |
for (int i = 0; i < a1Count; i++) |
149 |
{ |
|
150 | 4 |
if (a1[i] != a2[i])
|
151 | 1 |
return true; |
152 |
} |
|
153 |
|
|
154 | 40 |
return false; |
155 |
} |
|
156 |
|
|
157 | 9 |
public String toString()
|
158 |
{ |
|
159 | 9 |
StringBuffer buffer = new StringBuffer();
|
160 |
|
|
161 | 9 |
buffer.append(ClassFabUtils.getJavaClassName(_returnType)); |
162 | 9 |
buffer.append(" ");
|
163 | 9 |
buffer.append(_name); |
164 | 9 |
buffer.append("(");
|
165 |
|
|
166 | 9 |
for (int i = 0; i < count(_parameterTypes); i++) |
167 |
{ |
|
168 | 4 |
if (i > 0)
|
169 | 3 |
buffer.append(", ");
|
170 |
|
|
171 | 4 |
buffer.append(ClassFabUtils.getJavaClassName(_parameterTypes[i])); |
172 |
} |
|
173 |
|
|
174 | 9 |
buffer.append(")");
|
175 |
|
|
176 | 9 |
for (int i = 0; i < count(_exceptionTypes); i++) |
177 |
{ |
|
178 | 2 |
if (i == 0)
|
179 | 1 |
buffer.append(" throws ");
|
180 |
else
|
|
181 | 1 |
buffer.append(", ");
|
182 |
|
|
183 | 2 |
buffer.append(_exceptionTypes[i].getName()); |
184 |
} |
|
185 |
|
|
186 | 9 |
return buffer.toString();
|
187 |
} |
|
188 |
|
|
189 |
/**
|
|
190 |
* Returns a string consisting of the name of the method and its parameter values. This is
|
|
191 |
* similar to {@link #toString()}, but omits the return type and information about thrown
|
|
192 |
* exceptions. A unique id is used by {@link MethodIterator}to identify overlapping methods
|
|
193 |
* (methods with the same name but different thrown exceptions).
|
|
194 |
*
|
|
195 |
* @since 1.1
|
|
196 |
*/
|
|
197 | 1677 |
public String getUniqueId()
|
198 |
{ |
|
199 | 1677 |
StringBuffer buffer = new StringBuffer(_name);
|
200 | 1677 |
buffer.append("(");
|
201 |
|
|
202 | 1677 |
for (int i = 0; i < count(_parameterTypes); i++) |
203 |
{ |
|
204 | 4400 |
if (i > 0)
|
205 | 3113 |
buffer.append(",");
|
206 |
|
|
207 | 4400 |
buffer.append(ClassFabUtils.getJavaClassName(_parameterTypes[i])); |
208 |
} |
|
209 |
|
|
210 | 1677 |
buffer.append(")");
|
211 |
|
|
212 | 1677 |
return buffer.toString();
|
213 |
} |
|
214 |
|
|
215 |
/**
|
|
216 |
* Returns true if this signature has the same return type, name and parameters types as the
|
|
217 |
* method signature passed in, and this signatures exceptions "trump" (are the same as, or
|
|
218 |
* super-implementations of, all exceptions thrown by the other method signature).
|
|
219 |
*
|
|
220 |
* @since 1.1
|
|
221 |
*/
|
|
222 |
|
|
223 | 11 |
public boolean isOverridingSignatureOf(MethodSignature ms) |
224 |
{ |
|
225 | 11 |
if (_returnType != ms._returnType)
|
226 | 1 |
return false; |
227 |
|
|
228 | 10 |
if (!_name.equals(ms._name))
|
229 | 1 |
return false; |
230 |
|
|
231 | 9 |
if (mismatch(_parameterTypes, ms._parameterTypes))
|
232 | 1 |
return false; |
233 |
|
|
234 | 8 |
return exceptionsEncompass(ms._exceptionTypes);
|
235 |
} |
|
236 |
|
|
237 |
/**
|
|
238 |
* The nuts and bolts of checking that another method signature's exceptions are a subset of
|
|
239 |
* this signature's.
|
|
240 |
*
|
|
241 |
* @since 1.1
|
|
242 |
*/
|
|
243 |
|
|
244 | 8 |
private boolean exceptionsEncompass(Class[] otherExceptions) |
245 |
{ |
|
246 | 8 |
int ourCount = count(_exceptionTypes);
|
247 | 8 |
int otherCount = count(otherExceptions);
|
248 |
|
|
249 |
// If we have no exceptions, then ours encompass theirs only if they
|
|
250 |
// have no exceptions, either.
|
|
251 |
|
|
252 | 8 |
if (ourCount == 0)
|
253 | 2 |
return otherCount == 0;
|
254 |
|
|
255 | 6 |
boolean[] matched = new boolean[otherCount]; |
256 | 6 |
int unmatched = otherCount;
|
257 |
|
|
258 | 6 |
for (int i = 0; i < ourCount && unmatched > 0; i++) |
259 |
{ |
|
260 | 6 |
for (int j = 0; j < otherCount; j++) |
261 |
{ |
|
262 |
// Ignore exceptions that have already been matched
|
|
263 |
|
|
264 | 10 |
if (matched[j])
|
265 | 2 |
continue;
|
266 |
|
|
267 |
// When one of our exceptions is a super-class of one of their exceptions,
|
|
268 |
// then their exceptions is matched.
|
|
269 |
|
|
270 | 8 |
if (_exceptionTypes[i].isAssignableFrom(otherExceptions[j]))
|
271 |
{ |
|
272 | 3 |
matched[j] = true;
|
273 | 3 |
unmatched--; |
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
|
|
278 | 6 |
return unmatched == 0;
|
279 |
} |
|
280 |
} |
|