KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > transform > hook > AsmClassLoaderPreProcessor


1 /*
2  * Copyright 2003 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.transform.hook;
17
18 import java.io.*;
19 import net.sf.cglib.core.RemappingCodeVisitor;
20 import org.codehaus.aspectwerkz.hook.ClassLoaderPatcher;
21 import org.codehaus.aspectwerkz.hook.ClassLoaderPreProcessor;
22 import org.objectweb.asm.*;
23
24 /**
25  * Instruments the java.lang.ClassLoader to plug in the ClassPreProcessor
26  * mechanism using ASM.
27  *
28  * @author Chris Nokleberg
29  */

30 public class AsmClassLoaderPreProcessor implements ClassLoaderPreProcessor {
31     private static final String JavaDoc DESC_CORE = "Ljava/lang/String;[BIILjava/security/ProtectionDomain;";
32     private static final String JavaDoc DESC_PREFIX = "(" + DESC_CORE;
33     private static final String JavaDoc DESC_HELPER = "(Ljava/lang/ClassLoader;" + DESC_CORE + ")[B";
34
35     public AsmClassLoaderPreProcessor() {
36     }
37
38     public byte[] preProcess(byte[] b) {
39         try {
40             ClassWriter w = new ClassWriter(true) {
41                 private boolean flag;
42                 public void visit(int version, int access, String JavaDoc name, String JavaDoc superName, String JavaDoc[] interfaces, String JavaDoc sourceFile) {
43                     super.visit(version, access, name, superName, interfaces, sourceFile);
44                     flag = name.equals("java/lang/ClassLoader"); // is this ok?
45
}
46                 public CodeVisitor visitMethod(int access, String JavaDoc name, String JavaDoc desc, String JavaDoc[] exceptions, Attribute attrs) {
47                     CodeVisitor v = super.visitMethod(access, name, desc, exceptions, attrs);
48                     if (flag) {
49                         v = new PreProcessingVisitor(v, access, desc);
50                     }
51                     return v;
52                 }
53             };
54             new ClassReader(b).accept(w, false);
55             return w.toByteArray();
56         } catch (Exception JavaDoc e) {
57             System.err.println("failed to patch ClassLoader:");
58             e.printStackTrace();
59             return b;
60         }
61     }
62
63     private static class PreProcessingVisitor extends RemappingCodeVisitor {
64         public PreProcessingVisitor(CodeVisitor v, int access, String JavaDoc desc) {
65             super(v, access, Type.getArgumentTypes(desc));
66         }
67
68         public void visitMethodInsn(int opcode, String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
69             if ("defineClass0".equals(name) && "java/lang/ClassLoader".equals(owner)) {
70                 Type[] args = Type.getArgumentTypes(desc);
71                 if (args.length < 5 || !desc.startsWith(DESC_PREFIX)) {
72                      throw new Error JavaDoc("non standard JDK, native call not supported: " + desc);
73                 }
74                 int[] locals = new int[args.length];
75                 for (int i = args.length - 1; i >= 0; i--) {
76                     cv.visitVarInsn(args[i].getOpcode(Constants.ISTORE),
77                                     locals[i] = nextLocal(args[i].getSize()));
78                 }
79                 for (int i = 0; i < 5; i++) {
80                     cv.visitVarInsn(args[i].getOpcode(Constants.ILOAD), locals[i]);
81                 }
82                 super.visitMethodInsn(Constants.INVOKESTATIC,
83                                       "org/codehaus/aspectwerkz/hook/impl/ClassPreProcessorHelper",
84                                       "defineClass0Pre",
85                                       DESC_HELPER);
86                 cv.visitVarInsn(Constants.ASTORE, locals[1]);
87                 cv.visitVarInsn(Constants.ALOAD, 0);
88                 cv.visitVarInsn(Constants.ALOAD, locals[0]); // name
89
cv.visitVarInsn(Constants.ALOAD, locals[1]); // bytes
90
cv.visitInsn(Constants.ICONST_0); // offset
91
cv.visitVarInsn(Constants.ALOAD, locals[1]);
92                 cv.visitInsn(Constants.ARRAYLENGTH); // length
93
cv.visitVarInsn(Constants.ALOAD, locals[4]); // protection domain
94
for (int i = 5; i < args.length; i++) {
95                     cv.visitVarInsn(args[i].getOpcode(Constants.ILOAD), locals[i]);
96                 }
97             }
98             super.visitMethodInsn(opcode, owner, name, desc);
99         }
100     }
101
102     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
103         ClassLoaderPreProcessor me = new AsmClassLoaderPreProcessor();
104         InputStream is = ClassLoader.getSystemClassLoader().getParent().getResourceAsStream("java/lang/ClassLoader.class");
105         byte[] out = me.preProcess(ClassLoaderPatcher.inputStreamToByteArray(is));
106         is.close();
107         OutputStream os = new FileOutputStream("_boot/java/lang/ClassLoader.class");
108         os.write(out);
109         os.close();
110     }
111 }
112
Popular Tags