|
|||||||||||||||||||
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 | 80% | 88.9% | 100% | 87.4% |
|
1 |
// Copyright 2004 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,
|
|
21 |
* return type, parameter types and exception types. Actual Method objects are tied to
|
|
22 |
* a particular class, and don't compare well with other otherwise identical Methods from
|
|
23 |
* other classes or interface; MethodSignatures are distinct from classes and compare well.
|
|
24 |
*
|
|
25 |
* <p>
|
|
26 |
* Because the intended purpose is to compare methods from interfaces (which are always
|
|
27 |
* public and abstract) we don't bother to actually track the modifiers. In addition,
|
|
28 |
* at this time, MethodSignature <em>does not distinguish between instance and static
|
|
29 |
* methods</em>.
|
|
30 |
*
|
|
31 |
* @author Howard Lewis Ship
|
|
32 |
*/
|
|
33 |
public class MethodSignature |
|
34 |
{ |
|
35 |
private int _hashCode = -1; |
|
36 |
private Class _returnType;
|
|
37 |
private String _name;
|
|
38 |
private Class[] _parameterTypes;
|
|
39 |
private Class[] _exceptionTypes;
|
|
40 |
|
|
41 | 2460 |
public MethodSignature(
|
42 |
Class returnType, |
|
43 |
String name, |
|
44 |
Class[] parameterTypes, |
|
45 |
Class[] exceptionTypes) |
|
46 |
{ |
|
47 | 2460 |
_returnType = returnType; |
48 | 2460 |
_name = name; |
49 | 2460 |
_parameterTypes = parameterTypes; |
50 | 2460 |
_exceptionTypes = exceptionTypes; |
51 |
} |
|
52 |
|
|
53 | 717 |
public MethodSignature(Method m)
|
54 |
{ |
|
55 | 717 |
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.
|
|
60 |
* May return null.
|
|
61 |
*/
|
|
62 | 2410 |
public Class[] getExceptionTypes()
|
63 |
{ |
|
64 | 2410 |
return _exceptionTypes;
|
65 |
} |
|
66 |
|
|
67 | 2438 |
public String getName()
|
68 |
{ |
|
69 | 2438 |
return _name;
|
70 |
} |
|
71 |
|
|
72 |
/**
|
|
73 |
* Returns the parameter types for this method. May return null. Caution: do
|
|
74 |
* not modify the returned array.
|
|
75 |
*/
|
|
76 | 2429 |
public Class[] getParameterTypes()
|
77 |
{ |
|
78 | 2429 |
return _parameterTypes;
|
79 |
} |
|
80 |
|
|
81 | 2411 |
public Class getReturnType()
|
82 |
{ |
|
83 | 2411 |
return _returnType;
|
84 |
} |
|
85 |
|
|
86 | 4804 |
public int hashCode() |
87 |
{ |
|
88 | 4804 |
if (_hashCode == -1)
|
89 |
{ |
|
90 |
|
|
91 | 2407 |
_hashCode = _returnType.hashCode(); |
92 |
|
|
93 | 2407 |
_hashCode = 31 * _hashCode + _name.hashCode(); |
94 |
|
|
95 | 2407 |
int count = count(_parameterTypes);
|
96 |
|
|
97 | 2407 |
for (int i = 0; i < count; i++) |
98 | 1167 |
_hashCode = 31 * _hashCode + _parameterTypes[i].hashCode(); |
99 |
|
|
100 | 2407 |
count = count(_exceptionTypes); |
101 |
|
|
102 | 2407 |
for (int i = 0; i < count; i++) |
103 | 12 |
_hashCode = 31 * _hashCode + _exceptionTypes[i].hashCode(); |
104 |
} |
|
105 |
|
|
106 | 4804 |
return _hashCode;
|
107 |
} |
|
108 |
|
|
109 | 4854 |
private static int count(Object[] array) |
110 |
{ |
|
111 | 4854 |
return array == null ? 0 : array.length; |
112 |
} |
|
113 |
|
|
114 |
/**
|
|
115 |
* Returns true if the other object is an instance of MethodSignature with
|
|
116 |
* identical values for return type, name, parameter types and exception types.
|
|
117 |
*/
|
|
118 | 6 |
public boolean equals(Object o) |
119 |
{ |
|
120 | 6 |
if (o == null || !(o instanceof MethodSignature)) |
121 | 0 |
return false; |
122 |
|
|
123 | 6 |
MethodSignature ms = (MethodSignature) o; |
124 |
|
|
125 | 6 |
if (_returnType != ms._returnType)
|
126 | 0 |
return false; |
127 |
|
|
128 | 6 |
if (!_name.equals(ms._name))
|
129 | 0 |
return false; |
130 |
|
|
131 | 6 |
if (mismatch(_parameterTypes, ms._parameterTypes))
|
132 | 0 |
return false; |
133 |
|
|
134 | 6 |
return !mismatch(_exceptionTypes, ms._exceptionTypes);
|
135 |
} |
|
136 |
|
|
137 | 12 |
private boolean mismatch(Class[] a1, Class[] a2) |
138 |
{ |
|
139 | 12 |
int a1Count = count(a1);
|
140 | 12 |
int a2Count = count(a2);
|
141 |
|
|
142 | 12 |
if (a1Count != a2Count)
|
143 | 0 |
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 | 12 |
for (int i = 0; i < a1Count; i++) |
149 |
{ |
|
150 | 2 |
if (a1[i] != a2[i])
|
151 | 0 |
return true; |
152 |
} |
|
153 |
|
|
154 | 12 |
return false; |
155 |
} |
|
156 |
|
|
157 | 5 |
public String toString()
|
158 |
{ |
|
159 | 5 |
StringBuffer buffer = new StringBuffer();
|
160 |
|
|
161 | 5 |
buffer.append(ClassFabUtils.getJavaClassName(_returnType)); |
162 | 5 |
buffer.append(" ");
|
163 | 5 |
buffer.append(_name); |
164 | 5 |
buffer.append("(");
|
165 |
|
|
166 | 5 |
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 | 5 |
buffer.append(")");
|
175 |
|
|
176 | 5 |
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 | 5 |
return buffer.toString();
|
187 |
} |
|
188 |
|
|
189 |
} |
|
190 |
|
|