View Javadoc
1 package net.sf.cglib.transform.hook; 2 3 4 5 import java.io.*; 6 7 import net.sf.cglib.core.RemappingCodeVisitor; 8 9 import org.codehaus.aspectwerkz.hook.ClassLoaderPatcher; 10 11 import org.codehaus.aspectwerkz.hook.ClassLoaderPreProcessor; 12 13 import org.objectweb.asm.*; 14 15 16 17 /*** 18 19 * Instruments the java.lang.ClassLoader to plug in the ClassPreProcessor 20 21 * mechanism using ASM. 22 23 * 24 25 * @author Chris Nokleberg 26 27 */ 28 29 public class AsmClassLoaderPreProcessor implements ClassLoaderPreProcessor { 30 31 private static final String DESC_CORE = "Ljava/lang/String;[BIILjava/security/ProtectionDomain;"; 32 33 private static final String DESC_PREFIX = "(" + DESC_CORE; 34 35 private static final String DESC_HELPER = "(Ljava/lang/ClassLoader;" + DESC_CORE + ")[B"; 36 37 38 39 public AsmClassLoaderPreProcessor() { 40 41 } 42 43 44 45 public byte[] preProcess(byte[] b) { 46 47 try { 48 49 ClassWriter w = new ClassWriter(true) { 50 51 private boolean flag; 52 53 public void visit(int access, String name, String superName, String[] interfaces, String sourceFile) { 54 55 super.visit(access, name, superName, interfaces, sourceFile); 56 57 flag = name.equals("java/lang/ClassLoader"); // is this ok? 58 59 } 60 61 public CodeVisitor visitMethod(int access, String name, String desc, String[] exceptions, Attribute attrs) { 62 63 CodeVisitor v = super.visitMethod(access, name, desc, exceptions, attrs); 64 65 if (flag) { 66 67 v = new PreProcessingVisitor(v, access, desc); 68 69 } 70 71 return v; 72 73 } 74 75 }; 76 77 new ClassReader(b).accept(w, false); 78 79 return w.toByteArray(); 80 81 } catch (Exception e) { 82 83 System.err.println("failed to patch ClassLoader:"); 84 85 e.printStackTrace(); 86 87 return b; 88 89 } 90 91 } 92 93 94 95 private static class PreProcessingVisitor extends RemappingCodeVisitor { 96 97 public PreProcessingVisitor(CodeVisitor v, int access, String desc) { 98 99 super(v, access, Type.getArgumentTypes(desc)); 100 101 } 102 103 104 105 public void visitMethodInsn(int opcode, String owner, String name, String desc) { 106 107 if ("defineClass0".equals(name) && "java/lang/ClassLoader".equals(owner)) { 108 109 Type[] args = Type.getArgumentTypes(desc); 110 111 if (args.length < 5 || !desc.startsWith(DESC_PREFIX)) { 112 113 throw new Error("non standard JDK, native call not supported: " + desc); 114 115 } 116 117 int[] locals = new int[args.length]; 118 119 for (int i = args.length - 1; i >= 0; i--) { 120 121 cv.visitVarInsn(args[i].getOpcode(Constants.ISTORE), 122 123 locals[i] = nextLocal(args[i].getSize())); 124 125 } 126 127 for (int i = 0; i < 5; i++) { 128 129 cv.visitVarInsn(args[i].getOpcode(Constants.ILOAD), locals[i]); 130 131 } 132 133 super.visitMethodInsn(Constants.INVOKESTATIC, 134 135 "org/codehaus/aspectwerkz/hook/impl/ClassPreProcessorHelper", 136 137 "defineClass0Pre", 138 139 DESC_HELPER); 140 141 cv.visitVarInsn(Constants.ASTORE, locals[1]); 142 143 cv.visitVarInsn(Constants.ALOAD, 0); 144 145 cv.visitVarInsn(Constants.ALOAD, locals[0]); // name 146 147 cv.visitVarInsn(Constants.ALOAD, locals[1]); // bytes 148 149 cv.visitInsn(Constants.ICONST_0); // offset 150 151 cv.visitVarInsn(Constants.ALOAD, locals[1]); 152 153 cv.visitInsn(Constants.ARRAYLENGTH); // length 154 155 cv.visitVarInsn(Constants.ALOAD, locals[4]); // protection domain 156 157 for (int i = 5; i < args.length; i++) { 158 159 cv.visitVarInsn(args[i].getOpcode(Constants.ILOAD), locals[i]); 160 161 } 162 163 } 164 165 super.visitMethodInsn(opcode, owner, name, desc); 166 167 } 168 169 } 170 171 172 173 public static void main(String args[]) throws Exception { 174 175 ClassLoaderPreProcessor me = new AsmClassLoaderPreProcessor(); 176 177 InputStream is = ClassLoader.getSystemClassLoader().getParent().getResourceAsStream("java/lang/ClassLoader.class"); 178 179 byte[] out = me.preProcess(ClassLoaderPatcher.inputStreamToByteArray(is)); 180 181 is.close(); 182 183 OutputStream os = new FileOutputStream("_boot/java/lang/ClassLoader.class"); 184 185 os.write(out); 186 187 os.close(); 188 189 } 190 191 } 192

This page was automatically generated by Maven