1 22 package org.jboss.aop.util; 23 24 import javassist.ClassPool; 25 import javassist.CtClass; 26 import javassist.CtConstructor; 27 import javassist.CtField; 28 import javassist.CtMethod; 29 import javassist.NotFoundException; 30 31 import org.jboss.aop.AspectManager; 32 33 import java.lang.reflect.Constructor ; 34 import java.lang.reflect.Field ; 35 import java.lang.reflect.Method ; 36 37 44 public class ReflectToJavassist 45 { 46 public static CtClass classToJavassist(Class clazz) throws NotFoundException 47 { 48 ClassPool pool = AspectManager.instance().findClassPool(clazz.getClassLoader()); 49 CtClass ct = pool.get(clazz.getName()); 50 return ct; 51 } 52 53 public static CtField fieldToJavassist(Field field) throws NotFoundException 54 { 55 Class clazz = field.getDeclaringClass(); 56 return classToJavassist(field.getDeclaringClass()).getField(field.getName()); 57 } 58 public static CtConstructor constructorToJavassist(Constructor con) throws NotFoundException 59 { 60 Class clazz = con.getDeclaringClass(); 61 Class [] params = con.getParameterTypes(); 62 String [] strParams = new String [params.length]; 63 for (int i = 0; i < params.length; i++) 64 { 65 strParams[i] = simpleType(params[i]); 66 } 67 CtClass ct = classToJavassist(clazz); 68 69 CtConstructor[] methods = ct.getDeclaredConstructors(); 70 for (int i = 0; i < methods.length; i++) 71 { 72 CtClass[] ctParams = methods[i].getParameterTypes(); 73 if (ctParams.length == params.length) 74 { 75 boolean found = true; 76 for (int j = 0; j < ctParams.length; j++) 77 { 78 if (!ctParams[j].getName().equals(strParams[j])) 79 { 80 found = false; 81 break; 82 } 83 } 84 if (found) return methods[i]; 85 } 86 } 87 return null; 88 } 89 90 protected static String simpleType(Class type) 91 { 92 Class ret = type; 93 if (ret.isArray()) 94 { 95 Class arr = ret; 96 String array = ""; 97 while (arr.isArray()) 98 { 99 array += "[]"; 100 arr = arr.getComponentType(); 101 } 102 return arr.getName() + array; 103 } 104 return ret.getName(); 105 } 106 107 public static CtMethod methodToJavassist(Method method) throws NotFoundException 108 { 109 Class clazz = method.getDeclaringClass(); 110 Class [] params = method.getParameterTypes(); 111 String [] strParams = new String [params.length]; 112 for (int i = 0; i < params.length; i++) 113 { 114 strParams[i] = simpleType(params[i]); 115 } 116 CtClass ct = classToJavassist(clazz); 117 118 while (!ct.getName().equals(clazz.getName())) 119 { 120 ct = ct.getSuperclass(); 121 } 122 CtMethod[] methods = ct.getDeclaredMethods(); 123 for (int i = 0; i < methods.length; i++) 125 { 126 if (methods[i].getName().equals(method.getName())) 127 { 128 CtClass[] ctParams = methods[i].getParameterTypes(); 130 if (ctParams.length == params.length) 131 { 132 boolean found = true; 133 for (int j = 0; j < ctParams.length; j++) 134 { 135 if (!ctParams[j].getName().equals(strParams[j])) 136 { 137 found = false; 139 break; 140 } 141 } 142 if (found) return methods[i]; 143 } 144 } 145 } 146 return null; 148 } 149 150 } 151 | Popular Tags |