1 15 package org.apache.hivemind.service; 16 17 import java.lang.reflect.Method ; 18 19 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 public MethodSignature(Class returnType, String name, Class [] parameterTypes, 45 Class [] exceptionTypes) 46 { 47 _returnType = returnType; 48 _name = name; 49 _parameterTypes = parameterTypes; 50 _exceptionTypes = exceptionTypes; 51 } 52 53 public MethodSignature(Method m) 54 { 55 this(m.getReturnType(), m.getName(), m.getParameterTypes(), m.getExceptionTypes()); 56 } 57 58 62 public Class [] getExceptionTypes() 63 { 64 return _exceptionTypes; 65 } 66 67 public String getName() 68 { 69 return _name; 70 } 71 72 76 public Class [] getParameterTypes() 77 { 78 return _parameterTypes; 79 } 80 81 public Class getReturnType() 82 { 83 return _returnType; 84 } 85 86 public int hashCode() 87 { 88 if (_hashCode == -1) 89 { 90 91 _hashCode = _returnType.hashCode(); 92 93 _hashCode = 31 * _hashCode + _name.hashCode(); 94 95 int count = count(_parameterTypes); 96 97 for (int i = 0; i < count; i++) 98 _hashCode = 31 * _hashCode + _parameterTypes[i].hashCode(); 99 100 count = count(_exceptionTypes); 101 102 for (int i = 0; i < count; i++) 103 _hashCode = 31 * _hashCode + _exceptionTypes[i].hashCode(); 104 } 105 106 return _hashCode; 107 } 108 109 private static int count(Object [] array) 110 { 111 return array == null ? 0 : array.length; 112 } 113 114 118 public boolean equals(Object o) 119 { 120 if (o == null || !(o instanceof MethodSignature)) 121 return false; 122 123 MethodSignature ms = (MethodSignature) o; 124 125 if (_returnType != ms._returnType) 126 return false; 127 128 if (!_name.equals(ms._name)) 129 return false; 130 131 if (mismatch(_parameterTypes, ms._parameterTypes)) 132 return false; 133 134 return !mismatch(_exceptionTypes, ms._exceptionTypes); 135 } 136 137 private boolean mismatch(Class [] a1, Class [] a2) 138 { 139 int a1Count = count(a1); 140 int a2Count = count(a2); 141 142 if (a1Count != a2Count) 143 return true; 144 145 148 for (int i = 0; i < a1Count; i++) 149 { 150 if (a1[i] != a2[i]) 151 return true; 152 } 153 154 return false; 155 } 156 157 public String toString() 158 { 159 StringBuffer buffer = new StringBuffer (); 160 161 buffer.append(ClassFabUtils.getJavaClassName(_returnType)); 162 buffer.append(" "); 163 buffer.append(_name); 164 buffer.append("("); 165 166 for (int i = 0; i < count(_parameterTypes); i++) 167 { 168 if (i > 0) 169 buffer.append(", "); 170 171 buffer.append(ClassFabUtils.getJavaClassName(_parameterTypes[i])); 172 } 173 174 buffer.append(")"); 175 176 for (int i = 0; i < count(_exceptionTypes); i++) 177 { 178 if (i == 0) 179 buffer.append(" throws "); 180 else 181 buffer.append(", "); 182 183 buffer.append(_exceptionTypes[i].getName()); 184 } 185 186 return buffer.toString(); 187 } 188 189 197 public String getUniqueId() 198 { 199 StringBuffer buffer = new StringBuffer (_name); 200 buffer.append("("); 201 202 for (int i = 0; i < count(_parameterTypes); i++) 203 { 204 if (i > 0) 205 buffer.append(","); 206 207 buffer.append(ClassFabUtils.getJavaClassName(_parameterTypes[i])); 208 } 209 210 buffer.append(")"); 211 212 return buffer.toString(); 213 } 214 215 222 223 public boolean isOverridingSignatureOf(MethodSignature ms) 224 { 225 if (_returnType != ms._returnType) 226 return false; 227 228 if (!_name.equals(ms._name)) 229 return false; 230 231 if (mismatch(_parameterTypes, ms._parameterTypes)) 232 return false; 233 234 return exceptionsEncompass(ms._exceptionTypes); 235 } 236 237 243 244 private boolean exceptionsEncompass(Class [] otherExceptions) 245 { 246 int ourCount = count(_exceptionTypes); 247 int otherCount = count(otherExceptions); 248 249 252 if (ourCount == 0) 253 return otherCount == 0; 254 255 boolean[] matched = new boolean[otherCount]; 256 int unmatched = otherCount; 257 258 for (int i = 0; i < ourCount && unmatched > 0; i++) 259 { 260 for (int j = 0; j < otherCount; j++) 261 { 262 264 if (matched[j]) 265 continue; 266 267 270 if (_exceptionTypes[i].isAssignableFrom(otherExceptions[j])) 271 { 272 matched[j] = true; 273 unmatched--; 274 } 275 } 276 } 277 278 return unmatched == 0; 279 } 280 } | Popular Tags |