Fixing the ClassLoader.defineClass() method issue that could not find the necessary...
[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     private void loadMagic() {
13         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
14         cw.visit(Opcodes.V1_4, Opcodes.ACC_PUBLIC, "sun/reflect/GroovyMagic", null, "sun/reflect/MagicAccessorImpl", null);
15         MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
16         mv.visitCode();
17         mv.visitVarInsn(ALOAD, 0);
18         mv.visitMethodInsn(INVOKESPECIAL, "sun/reflect/MagicAccessorImpl", "<init>", "()V", false);
19         mv.visitInsn(RETURN);
20         mv.visitMaxs(0,0);
21         mv.visitEnd();
22         cw.visitEnd();
23
24         define(cw.toByteArray(), "sun.reflect.GroovyMagic");
25     }
26
27     protected void define(byte[] bytes, final String name) {
28         //knownClasses.put(name, defineClass(name, bytes, 0, bytes.length));
29         Class cls = defineClass(name, bytes, 0, bytes.length);
30     }
31
32     protected final Map<String,Class> knownClasses = new HashMap<String,Class>();
33
34     public static void main(String[] args) {
35         SunClassLoader sun = new SunClassLoader();
36         sun.loadMagic();
37     }
38 }