1 22 package org.jboss.aop.hook; 23 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 28 import javassist.CannotCompileException; 29 import javassist.ClassPool; 30 import javassist.CtClass; 31 import javassist.CtMethod; 32 import javassist.CtNewMethod; 33 import javassist.LoaderClassPath; 34 import javassist.Modifier; 35 import javassist.NotFoundException; 36 37 42 public class GenerateInstrumentedClassLoader 43 { 44 45 private static void declare5(CtClass clazz, CtMethod method) throws NotFoundException, CannotCompileException 46 { 47 method.setName("wrappedDefineClass"); 48 CtMethod wrapper = CtNewMethod.make(Modifier.PROTECTED, method.getReturnType(), "defineClass", method.getParameterTypes(), method.getExceptionTypes(), null, clazz); 49 50 String code = "{" 51 + " byte[] newBytes = org.jboss.aop.AspectManager.instance().translate($1, $0, $2) ;" 52 + " if (newBytes != (byte[])null) {" 53 + " return wrappedDefineClass($1, newBytes, 0, newBytes.length, $5); " 54 + " } else {" 55 + " return wrappedDefineClass($1, $2, $3, $4, $5); " 56 + " }" 57 + "}"; 58 wrapper.setBody(code); 59 clazz.addMethod(wrapper); 60 61 } 62 63 72 public static byte[] getInstrumentedClassLoader() 73 throws NotFoundException, IOException , CannotCompileException 74 { 75 ClassPool classpool = ClassPool.getDefault(); 76 classpool = ClassPool.getDefault(); 77 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 78 classpool.insertClassPath(new LoaderClassPath(cl)); 79 80 final CtClass clazz = classpool.get(ClassLoader .class.getName()); 81 CtMethod[] methods = clazz.getDeclaredMethods(); 82 for (int i = 0; i < methods.length; i++) 83 { 84 if (methods[i].getName().equals("defineClass")) 85 { 86 if (methods[i].getParameterTypes().length == 5 87 && methods[i].getParameterTypes()[1].isArray()) 88 { 89 declare5(clazz, methods[i]); 90 } 91 } 92 } 93 return clazz.toBytecode(); 94 } 95 96 101 public static void main(final String [] args) 102 { 103 if (args.length != 1) 104 { 105 System.err.println("Usage: java " + GeneratePluggableInstrumentedClassLoader.class.getName() + " <output directory>"); 106 System.exit(1); 107 } 108 final String filename = args[0] + File.separatorChar + 109 "java" + File.separatorChar + 110 "lang" + File.separatorChar + 111 "ClassLoader.class"; 112 final File file = new File (filename); 113 if (file.exists()) 114 { 115 if (!file.canWrite()) 116 { 117 System.err.println("Cannot write to existing file: " + file.getAbsolutePath()); 118 System.exit(2); 119 } 120 } 121 else 122 { 123 final File dir = file.getParentFile(); 124 if (!dir.exists()) 125 { 126 dir.mkdirs(); 127 } 128 if (!dir.canWrite()) 129 { 130 System.err.println("Cannot write to parent directory: " + dir.getAbsolutePath()); 131 } 132 } 133 134 final byte[] bytes; 135 try 136 { 137 bytes = getInstrumentedClassLoader(); 138 } 139 catch (final Throwable th) 140 { 141 System.err.println("Unexpected exception caught during instrumentation: " + th.getMessage()); 142 th.printStackTrace(System.err); 143 System.exit(5); 144 return; 145 } 146 147 try 148 { 149 final FileOutputStream fos = new FileOutputStream (file); 150 fos.write(bytes); 151 fos.close(); 152 } 153 catch (final IOException ioe) 154 { 155 System.err.println("Unexpected exception caught while writing class file: " + ioe.getMessage()); 156 ioe.printStackTrace(System.err); 157 System.exit(6); 158 } 159 } 160 } 161 | Popular Tags |