1 22 package org.jboss.aop.util; 23 24 import org.jboss.aop.AspectManager; 25 26 import javassist.CtBehavior; 27 import javassist.CtClass; 28 import javassist.CtMethod; 29 import javassist.NotFoundException; 30 31 37 public class JavassistToReflect 38 { 39 public static String getParameterClassArrayString(String arrayName, CtBehavior behavior)throws NotFoundException 40 { 41 CtClass[] params = behavior.getParameterTypes(); 42 return getParameterClassArrayString(params, arrayName); 43 } 44 45 public static String getParameterClassArrayString(CtClass[] params, String arrayName) throws NotFoundException 46 { 47 StringBuffer arr = new StringBuffer (" new java.lang.Class[" + params.length + "];"); 48 49 for (int i = 0 ; i < params.length ; i++) 50 { 51 arr.append(arrayName + "["+ i + "] = "); 52 53 if (params[i].isArray()) 54 { 55 arr.append(getClassObjectString(params[i], true)); 56 } 57 else 58 { 59 arr.append(getClassObjectString(params[i])); 60 } 61 arr.append(";"); 62 } 63 return arr.toString(); 64 } 65 66 public static String getClassObjectString(CtClass clazz) 67 { 68 return getClassObjectString(clazz, false); 72 } 73 74 public static String getClassObjectString(CtClass clazz, boolean isArray) 75 { 76 80 if (!isArray) 81 { 82 if (clazz.isPrimitive()) 83 { 84 return getClassName(clazz); 85 } 86 else 87 { 88 return "java.lang.Class.forName(\"" + getClassName(clazz) + "\")"; 89 } 90 } 91 else 92 { 93 if (AspectManager.debugClasses) 95 { 96 return "new " + getArrayClassName(clazz) + "[0].getClass()"; 97 } 98 else 99 { 100 return getArrayClassName(clazz) + "[].class"; 101 } 102 } 103 } 104 105 public static String getArrayClassName(CtClass clazz) 106 { 107 String name = clazz.getName(); 108 int ind = name.indexOf("["); 109 return name.substring(0, ind); 110 } 111 112 public static String getClassName(CtClass clazz) 113 { 114 if (!clazz.isPrimitive()) 115 { 116 return clazz.getName(); 117 } 118 else 119 { 120 if (clazz == CtClass.booleanType) return "java.lang.Boolean.TYPE"; 121 else if (clazz == CtClass.byteType) return "java.lang.Byte.TYPE"; 122 else if (clazz == CtClass.charType) return "java.lang.Character.TYPE"; 123 else if (clazz == CtClass.doubleType)return "java.lang.Double.TYPE"; 124 else if (clazz == CtClass.floatType) return "java.lang.Float.TYPE"; 125 else if (clazz == CtClass.intType) return "java.lang.Integer.TYPE"; 126 else if (clazz == CtClass.longType) return "java.lang.Long.TYPE"; 127 else if (clazz == CtClass.shortType) return "java.lang.Short.TYPE"; 128 else throw new RuntimeException ("Unsupported type " + clazz.getName()); 129 } 130 } 131 132 public static String castInvocationValueToTypeString(CtClass type) 133 { 134 return castInvocationValueToTypeString(type, "value"); 135 } 136 137 public static String castInvocationValueToTypeString(CtClass type, String valueName) 138 { 139 String cast = null; 140 if (type.isPrimitive()) 141 { 142 if (type.equals(CtClass.booleanType)) 143 { 144 cast = "((Boolean)" + valueName +").booleanValue();"; 145 } 146 else if (type.equals(CtClass.byteType)) 147 { 148 cast = "((Byte)" + valueName +").byteValue();"; 149 } 150 else if (type.equals(CtClass.charType)) 151 { 152 cast = "((Character)" + valueName +").charValue();"; 153 } 154 else if (type.equals(CtClass.doubleType)) 155 { 156 cast = "((Double)" + valueName +").doubleValue();"; 157 } 158 else if (type.equals(CtClass.floatType)) 159 { 160 cast = "((Float)" + valueName +").floatValue();"; 161 } 162 else if (type.equals(CtClass.intType)) 163 { 164 cast = "((Integer)" + valueName +").intValue();"; 165 } 166 else if (type.equals(CtClass.longType)) 167 { 168 cast = "((Long)" + valueName +").longValue();"; 169 } 170 else if (type.equals(CtClass.shortType)) 171 { 172 cast = "((Short)" + valueName +").shortValue();"; 173 } 174 } 175 else if (type.isArray()) 176 { 177 cast = "(" + type.getName() + ")" + valueName +";"; 178 } 179 else 180 { 181 cast = "(" + type.getName() + ")" + valueName +";"; 182 } 183 184 return cast; 185 } 186 187 } 188 | Popular Tags |