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