1 22 package org.jboss.aop.instrument; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.lang.ref.WeakReference ; 27 import java.net.URI ; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.security.AccessController ; 31 import java.security.PrivilegedActionException ; 32 import java.security.PrivilegedExceptionAction ; 33 34 import org.jboss.aop.AspectManager; 35 import org.jboss.aop.classpool.AOPClassPool; 36 import org.jboss.aop.standalone.Compiler; 37 38 import javassist.CannotCompileException; 39 import javassist.ClassPool; 40 import javassist.CtClass; 41 import javassist.CtField; 42 import javassist.NotFoundException; 43 44 50 public class TransformerCommon { 51 52 final static URL [] NO_URLS = new URL [0]; 53 final static CtClass[] EMPTY_CTCLASS_ARRAY = new CtClass[0]; 54 final static String WEAK_REFERENCE = WeakReference .class.getName(); 55 56 public static boolean isCompileTime() 57 { 58 return Compiler.loader != null; 59 } 60 61 public static void compileOrLoadClass(CtClass classForPackage, CtClass newClass) 62 { 63 compileOrLoadClass(classForPackage, newClass, isCompileTime()); 64 } 65 66 72 public static void compileOrLoadClass(CtClass classForPackage, CtClass newClass, boolean compile) 73 { 74 try 75 { 76 registerGeneratedClass(newClass); 77 if (compile) 79 { 80 File file; 81 URLClassLoader loader = Compiler.loader; 82 if (loader == null) 83 { 84 loader = new URLClassLoader (NO_URLS, Thread.currentThread().getContextClassLoader()); 85 } 86 URL url = loader.getResource( 87 classForPackage.getName().replace('.', '/') + ".class"); 88 String path = url.toString(); 89 path = path.substring(0, path.lastIndexOf('/') + 1); 90 path = path + newClass.getSimpleName() + ".class"; 91 URI newUrl = new URI (path); 92 file = new File (newUrl); 93 FileOutputStream fp = new FileOutputStream (file); 94 fp.write(newClass.toBytecode()); 95 fp.close(); 96 } 97 else 98 { 99 if (System.getSecurityManager() == null) 101 { 102 ToClassAction.NON_PRIVILEGED.toClass(newClass, null); 103 } 104 else 105 { 106 ToClassAction.PRIVILEGED.toClass(newClass, null); 107 } 108 } 109 110 if (AspectManager.debugClasses) 111 { 112 newClass.debugWriteFile(); 113 } 114 } 115 catch (Exception e) 116 { 117 throw new RuntimeException (e); 118 } 119 } 120 121 public static Class toClass(CtClass newClass) throws CannotCompileException 122 { 123 registerGeneratedClass(newClass); 124 125 if (System.getSecurityManager() == null) 126 { 127 return ToClassAction.NON_PRIVILEGED.toClass(newClass, null); 128 } 129 else 130 { 131 return ToClassAction.PRIVILEGED.toClass(newClass, null); 132 } 133 } 134 135 public static Class toClass(CtClass newClass, ClassLoader loader) throws CannotCompileException 136 { 137 registerGeneratedClass(newClass); 138 139 if (System.getSecurityManager() == null) 140 { 141 return ToClassAction.NON_PRIVILEGED.toClass(newClass, loader); 142 } 143 else 144 { 145 return ToClassAction.PRIVILEGED.toClass(newClass, loader); 146 } 147 } 148 149 protected static void addInfoField(Instrumentor instrumentor, String infoClassName, String infoName, 150 int modifiers, CtClass addTo, boolean weak, CtField.Initializer init) throws NotFoundException, CannotCompileException 151 { 152 if (weak) 153 { 154 addWeakReferenceInfoField(instrumentor, infoClassName, infoName, modifiers, addTo, init); 155 } 156 else 157 { 158 addStrongReferenceInfoField(instrumentor, infoClassName, infoName, modifiers, addTo, init); 159 } 160 } 161 162 private static void registerGeneratedClass(CtClass newClass) 163 { 164 ClassPool pool = newClass.getClassPool(); 166 if (pool instanceof AOPClassPool) 167 { 168 ((AOPClassPool)pool).registerGeneratedClass(newClass.getName()); 169 } 170 } 171 172 private static void addWeakReferenceInfoField(Instrumentor instrumentor, String infoClassName, String infoName, 173 int modifiers, CtClass addTo, CtField.Initializer init) throws NotFoundException, CannotCompileException 174 { 175 CtClass type = instrumentor.forName(WEAK_REFERENCE); 176 CtField field = new CtField(type, infoName, addTo); 177 field.setModifiers(modifiers); 178 addTo.addField(field, init); 179 } 180 181 private static void addStrongReferenceInfoField(Instrumentor instrumentor, String infoClassName, String infoName, 182 int modifiers, CtClass addTo, CtField.Initializer init) throws NotFoundException, CannotCompileException 183 { 184 CtClass type = instrumentor.forName(infoClassName); 185 CtField field = new CtField(type, infoName, addTo); 186 field.setModifiers(modifiers); 187 addTo.addField(field, init); 188 } 189 190 protected static String infoFromWeakReference(String infoClassName, String localName, String infoName) 191 { 192 return infoClassName + " " + localName + " = (" + infoClassName + ")" + infoName + ".get();"; 193 } 194 195 198 public static CtClass makeNestedClass(CtClass outer, String name, boolean isStatic, int modifiers, CtClass superClass) throws CannotCompileException 199 { 200 CtClass inner = makeNestedClass(outer, name, true); 201 inner.setModifiers(modifiers); 202 inner.setSuperclass(superClass); 203 return inner; 204 } 205 206 209 public static CtClass makeNestedClass(CtClass outer, String name, boolean isStatic) throws CannotCompileException 210 { 211 registerGeneratedClass(outer.getClassPool(), outer.getName() + "$" + name); 212 CtClass inner = outer.makeNestedClass(name, true); 213 return inner; 214 } 215 216 219 public static CtClass makeClass(ClassPool pool, String name) 220 { 221 registerGeneratedClass(pool, name); 222 return pool.makeClass(name); 223 } 224 225 228 public static CtClass makeClass(ClassPool pool, String name, CtClass superClass) 229 { 230 registerGeneratedClass(pool, name); 231 return pool.makeClass(name, superClass); 232 } 233 234 private static void registerGeneratedClass(ClassPool pool, String name) 235 { 236 try 237 { 238 ((AOPClassPool)pool).registerGeneratedClass(name); 239 } 240 catch(ClassCastException e) 241 { 242 243 } 244 } 245 246 private interface ToClassAction 247 { 248 Class toClass(CtClass clazz, ClassLoader loader) throws CannotCompileException; 249 250 ToClassAction PRIVILEGED = new ToClassAction() 251 { 252 public Class toClass(final CtClass clazz, final ClassLoader loader) throws CannotCompileException 253 { 254 try 255 { 256 return (Class )AccessController.doPrivileged(new PrivilegedExceptionAction () 257 { 258 public Object run() throws Exception 259 { 260 if (AspectManager.debugClasses) 261 { 262 clazz.debugWriteFile(); 263 } 264 if (loader != null) 265 { 266 return clazz.toClass(loader, null); 267 } 268 return clazz.toClass(); 269 } 270 }); 271 } 272 catch (PrivilegedActionException e) 273 { 274 Exception actual = e.getException(); 275 if (actual instanceof CannotCompileException) 276 { 277 throw (CannotCompileException)actual; 278 } 279 throw new RuntimeException (actual); 280 } 281 } 282 }; 283 284 ToClassAction NON_PRIVILEGED = new ToClassAction() 285 { 286 public Class toClass(CtClass clazz, ClassLoader loader) throws CannotCompileException 287 { 288 if (AspectManager.debugClasses) 289 { 290 clazz.debugWriteFile(); 291 } 292 if (loader != null) 293 { 294 return clazz.toClass(loader, null); 295 } 296 return clazz.toClass(); 297 } 298 }; 299 } 300 } 301 | Popular Tags |