Commenting out a line that causes a lot of loops; let the exclusion trace continue...
[jpf-core.git] / examples / SunClassLoader.java
1 import org.objectweb.asm.Opcodes;
2 import org.objectweb.asm.ClassWriter;
3 import org.objectweb.asm.MethodVisitor;
4
5 import java.util.Map;
6 import java.util.HashMap;
7
8 /**
9  * Special class loader, which when running on Sun VM allows to generate accessor classes for any method
10  */
11 public class SunClassLoader extends ClassLoader implements Opcodes {
12
13     private void loadMagic() {
14         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
15         cw.visit(Opcodes.V1_4, Opcodes.ACC_PUBLIC, "sun/reflect/GroovyMagic", null, "sun/reflect/MagicAccessorImpl", null);
16         MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
17         mv.visitCode();
18         mv.visitVarInsn(ALOAD, 0);
19         mv.visitMethodInsn(INVOKESPECIAL, "sun/reflect/MagicAccessorImpl", "<init>", "()V", false);
20         mv.visitInsn(RETURN);
21         mv.visitMaxs(0,0);
22         mv.visitEnd();
23         cw.visitEnd();
24
25         define(cw.toByteArray(), "sun.reflect.GroovyMagic");
26     }
27
28     protected void define(byte[] bytes, final String name) {
29         //knownClasses.put(name, defineClass(name, bytes, 0, bytes.length));
30         Class cls = defineClass(name, bytes, 0, bytes.length);
31         //Class cls2 = defineClass(name, bytes, 0, bytes.length);
32     }
33
34     protected final Map<String,Class> knownClasses = new HashMap<String,Class>();
35
36     public static void main(String[] args) throws Exception {
37         SunClassLoader sun = new SunClassLoader();
38         sun.loadMagic();
39         
40         Class cls2 = sun.loadClass("java/lang/Object");
41     }
42 }