1 22 package org.jboss.aop.util; 23 24 import java.util.ArrayList ; 25 26 import javassist.CtClass; 27 import javassist.CtMethod; 28 import javassist.NotFoundException; 29 30 35 public class JavassistUtils 36 { 37 public static CtClass[] EMPTY_CTCLASS_ARRAY = new CtClass[0]; 38 39 public static CtMethod[] getMethodsWithName(CtClass clazz, String name) 40 { 41 CtMethod[] methods = clazz.getMethods(); 42 return getMethodsWithName(methods, name); 43 } 44 45 public static CtMethod[] getDeclaredMethodsWithName(CtClass clazz, String name) 46 { 47 CtMethod[] methods = clazz.getDeclaredMethods(); 48 return getMethodsWithName(methods, name); 49 } 50 51 private static CtMethod[] getMethodsWithName(CtMethod[] methods, String name) 52 { 53 ArrayList foundMethods = new ArrayList (); 54 for (int i = 0 ; i < methods.length ; i++) 55 { 56 if (methods[i].getName().equals(name)) 57 { 58 foundMethods.add(methods[i]); 59 } 60 } 61 return (CtMethod[])foundMethods.toArray(new CtMethod[foundMethods.size()]); 62 } 63 64 public static boolean isSubclassOrImplements(CtClass clazz, CtClass lookingFor) throws NotFoundException 65 { 66 if (clazz == null) return false; 67 if (clazz.equals(lookingFor)) return true; 68 if (clazz.getName().equals("java.lang.Object")) return false; 69 70 if (clazz.isPrimitive()) return false; 71 72 CtClass[] interfaces = clazz.getInterfaces(); 73 for (int i = 0 ; i < interfaces.length ; i++) 74 { 75 if (isSubclassOrImplements(interfaces[i], lookingFor)) return true; 76 } 77 78 if (isSubclassOrImplements(clazz.getSuperclass(), lookingFor)) return true; 79 80 return false; 81 } 82 83 public static String getNullOrZeroInitialiser(CtClass clazz) 84 { 85 if (clazz.isPrimitive()) 86 { 87 if (clazz.equals(CtClass.booleanType)) return "false"; 88 else if (clazz.equals(CtClass.charType)) return "'\\0'"; 89 else if (clazz.equals(CtClass.byteType)) return "(byte)0"; 90 else if (clazz.equals(CtClass.shortType)) return "(short)0"; 91 else if (clazz.equals(CtClass.intType)) return "(int)0"; 92 else if (clazz.equals(CtClass.longType)) return "0L"; 93 else if (clazz.equals(CtClass.floatType)) return "0.0f"; 94 else if (clazz.equals(CtClass.doubleType)) return "0.0d"; 95 } 96 else 97 { 98 return "null"; 99 } 100 return null; 101 } 102 } 103 | Popular Tags |