1 4 package com.tc.aspectwerkz.reflect; 5 6 import java.lang.reflect.Constructor ; 7 import java.lang.reflect.Field ; 8 import java.lang.reflect.Modifier ; 9 import java.lang.reflect.Method ; 10 import java.util.List ; 11 import java.util.Iterator ; 12 13 14 import com.tc.aspectwerkz.exception.WrappedRuntimeException; 15 import com.tc.aspectwerkz.transform.TransformationConstants; 16 17 22 public class ReflectHelper { 23 24 private final static Method OBJECT_EQUALS; 25 private final static Method OBJECT_HASH_CODE; 26 private final static Method OBJECT_GET_CLASS; 27 private final static Method OBJECT_TO_STRING; 28 private final static Method OBJECT_CLONE; 29 private final static Method OBJECT_WAIT_1; 30 private final static Method OBJECT_WAIT_2; 31 private final static Method OBJECT_WAIT_3; 32 private final static Method OBJECT_NOTIFY; 33 private final static Method OBJECT_NOTIFY_ALL; 34 private final static Method OBJECT_FINALIZE; 35 36 static { 37 Class clazz = Object .class; 38 try { 39 OBJECT_EQUALS = clazz.getDeclaredMethod("equals", new Class []{clazz}); 40 OBJECT_HASH_CODE = clazz.getDeclaredMethod("hashCode", new Class []{}); 41 OBJECT_GET_CLASS = clazz.getDeclaredMethod("getClass", new Class []{}); 42 OBJECT_CLONE = clazz.getDeclaredMethod("clone", new Class []{}); 43 OBJECT_TO_STRING = clazz.getDeclaredMethod("toString", new Class []{}); 44 OBJECT_WAIT_1 = clazz.getDeclaredMethod("wait", new Class []{}); 45 OBJECT_WAIT_2 = clazz.getDeclaredMethod("wait", new Class []{long.class}); 46 OBJECT_WAIT_3 = clazz.getDeclaredMethod("wait", new Class []{long.class, int.class}); 47 OBJECT_NOTIFY = clazz.getDeclaredMethod("notify", new Class []{}); 48 OBJECT_NOTIFY_ALL = clazz.getDeclaredMethod("notifyAll", new Class []{}); 49 OBJECT_FINALIZE = clazz.getDeclaredMethod("finalize", new Class []{}); 50 } catch (NoSuchMethodException e) { 51 throw new WrappedRuntimeException(e); 52 } 53 } 54 55 88 124 131 private static boolean isDeclaredByInterface(Method method, List interfaceDeclaredMethods) { 132 boolean match = false; 133 for (Iterator iterator = interfaceDeclaredMethods.iterator(); iterator.hasNext();) { 134 Method methodIt = (Method ) iterator.next(); 135 if (method.getName().equals(methodIt.getName())) { 136 if (method.getParameterTypes().length == methodIt.getParameterTypes().length) { 137 boolean matchArgs = true; 138 for (int i = 0; i < method.getParameterTypes().length; i++) { 139 Class parameterType = method.getParameterTypes()[i]; 144 if (parameterType.getName().equals(methodIt.getParameterTypes()[i].getName())) { 145 ; 146 } else { 147 matchArgs = false; 148 break; 149 } 150 } 151 if (matchArgs) { 152 match = true; 153 break; 154 } 155 } 156 } 157 } 158 return match; 159 } 160 161 167 private static boolean isUserDefinedMethod(final Method method) { 168 if (!method.equals(OBJECT_EQUALS) 169 && !method.equals(OBJECT_HASH_CODE) 170 && !method.equals(OBJECT_GET_CLASS) 171 && !method.equals(OBJECT_TO_STRING) 172 && !method.equals(OBJECT_CLONE) 173 && !method.equals(OBJECT_WAIT_1) 174 && !method.equals(OBJECT_WAIT_2) 175 && !method.equals(OBJECT_WAIT_3) 176 && !method.equals(OBJECT_NOTIFY) 177 && !method.equals(OBJECT_NOTIFY_ALL) 178 && !method.getName().startsWith(TransformationConstants.SYNTHETIC_MEMBER_PREFIX) 179 && !method.getName().startsWith(TransformationConstants.ORIGINAL_METHOD_PREFIX) 180 && !method.getName().startsWith(TransformationConstants.ASPECTWERKZ_PREFIX)) { 181 return true; 182 } else { 183 return false; 184 } 185 } 186 187 193 public static int getModifiersAsInt(final String [] modifiers) { 194 int accessFlags = 0; 195 for (int i = 0; i < modifiers.length; i++) { 196 if (modifiers[i].equals("abstract")) { 197 accessFlags |= Modifier.ABSTRACT; 198 } else if (modifiers[i].equals("final")) { 199 accessFlags |= Modifier.FINAL; 200 } else if (modifiers[i].equals("interface")) { 201 accessFlags |= Modifier.INTERFACE; 202 } else if (modifiers[i].equals("native")) { 203 accessFlags |= Modifier.NATIVE; 204 } else if (modifiers[i].equals("private")) { 205 accessFlags |= Modifier.PRIVATE; 206 } else if (modifiers[i].equals("protected")) { 207 accessFlags |= Modifier.PROTECTED; 208 } else if (modifiers[i].equals("public")) { 209 accessFlags |= Modifier.PUBLIC; 210 } else if (modifiers[i].equals("static")) { 211 accessFlags |= Modifier.STATIC; 212 } else if (modifiers[i].equals("strict")) { 213 accessFlags |= Modifier.STRICT; 214 } else if (modifiers[i].equals("synchronized")) { 215 accessFlags |= Modifier.SYNCHRONIZED; 216 } else if (modifiers[i].equals("transient")) { 217 accessFlags |= Modifier.TRANSIENT; 218 } else if (modifiers[i].equals("volatile")) { 219 accessFlags |= Modifier.VOLATILE; 220 } 221 } 222 return accessFlags; 223 } 224 225 231 public static int calculateHash(final Class klass) { 232 return klass.getName().hashCode(); 233 } 234 235 241 public static int calculateHash(final Method method) { 242 int hash = 17; 243 hash = (37 * hash) + method.getName().hashCode(); 244 for (int i = 0; i < method.getParameterTypes().length; i++) { 245 Class type = method.getParameterTypes()[i]; 246 hash = (37 * hash) + type.getName().hashCode(); 247 } 248 return hash; 249 } 250 251 257 public static int calculateHash(final Constructor constructor) { 258 int hash = 17; 259 hash = (37 * hash) + TransformationConstants.INIT_METHOD_NAME.hashCode(); 260 for (int i = 0; i < constructor.getParameterTypes().length; i++) { 261 Class type = constructor.getParameterTypes()[i]; 262 hash = (37 * hash) + type.getName().replace('/', '.').hashCode(); 263 } 264 return hash; 265 } 266 267 273 public static int calculateHash(final Field field) { 274 int hash = 17; 275 hash = (37 * hash) + field.getName().hashCode(); 276 Class type = field.getType(); 277 hash = (37 * hash) + type.getName().hashCode(); 278 return hash; 279 } 280 281 287 public static Class getPrimitiveClass(final String className) { 288 if (className.equals("void")) { 289 return void.class; 290 } else if (className.equals("long")) { 291 return long.class; 292 } else if (className.equals("int")) { 293 return int.class; 294 } else if (className.equals("short")) { 295 return short.class; 296 } else if (className.equals("double")) { 297 return double.class; 298 } else if (className.equals("float")) { 299 return float.class; 300 } else if (className.equals("byte")) { 301 return byte.class; 302 } else if (className.equals("boolean")) { 303 return boolean.class; 304 } else if (className.equals("char")) { 305 return char.class; 306 } else { 307 return null; 308 } 309 } 310 311 317 public static String getClassSignature(Class cl) { 318 StringBuffer sbuf = new StringBuffer (); 319 while (cl.isArray()) { 320 sbuf.append('['); 321 cl = cl.getComponentType(); 322 } 323 if (cl.isPrimitive()) { 324 if (cl == Integer.TYPE) { 325 sbuf.append('I'); 326 } else if (cl == Byte.TYPE) { 327 sbuf.append('B'); 328 } else if (cl == Long.TYPE) { 329 sbuf.append('J'); 330 } else if (cl == Float.TYPE) { 331 sbuf.append('F'); 332 } else if (cl == Double.TYPE) { 333 sbuf.append('D'); 334 } else if (cl == Short.TYPE) { 335 sbuf.append('S'); 336 } else if (cl == Character.TYPE) { 337 sbuf.append('C'); 338 } else if (cl == Boolean.TYPE) { 339 sbuf.append('Z'); 340 } else if (cl == Void.TYPE) { 341 sbuf.append('V'); 342 } else { 343 throw new InternalError (); 344 } 345 } else { 346 sbuf.append('L' + cl.getName().replace('.', '/') + ';'); 347 } 348 return sbuf.toString(); 349 } 350 351 357 public static String getConstructorSignature(final Constructor constructor) { 358 return getMethodSignature(constructor.getParameterTypes(), Void.TYPE); 359 } 360 361 367 public static String getFieldSignature(final Field field) { 368 return getClassSignature(field.getType()); 369 } 370 371 377 public static String getMethodSignature(final Method method) { 378 return getMethodSignature(method.getParameterTypes(), method.getReturnType()); 379 } 380 381 388 private static String getMethodSignature(Class [] paramTypes, Class retType) { 389 StringBuffer sbuf = new StringBuffer (); 390 sbuf.append('('); 391 for (int i = 0; i < paramTypes.length; i++) { 392 sbuf.append(getClassSignature(paramTypes[i])); 393 } 394 sbuf.append(')'); 395 sbuf.append(getClassSignature(retType)); 396 return sbuf.toString(); 397 } 398 } | Popular Tags |