vendredi 10 juillet 2015

How Java method accepts parameters of anouymous classes

I have difficulty in using the generated bytecode class which is loaded by Unsafe.defineAnonymousClass(). I am wondering how to pass objects of anonymous to a method as parameters.

Take an example class Callee below for example, its constructor accepts Callee2 as parameter.

Class Callee{
    Callee2 _call2;
    public Callee(Callee2 callee2){
        ...
    }
}

During runtime, I generated a new bytecode for both Callee2 and Callee, both of which are loaded by Unsafe.defineAnonymousClass(). For the Callee, the constructor is also modified to be:

 public test.code.jit.asm.simple.Callee(test.code.jit.asm.simple.Callee2.1506553666);
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=3, args_size=2
         0: aload_0       
         1: invokespecial #65                 // Method java/lang/Object."<init>":()V
           ....
         8: return       

while the generated class name of Callee2 is:

      class test.code.jit.asm.simple.Callee2/1506553666

I created an instance of `Callee2/1506553666' and want to create instance of new Callee with it, but fail:

        newCls.getConstructor(args).newInstance(objs); 

where args = [class test.code.jit.asm.simple.Callee2/1506553666] and objs= [test.code.jit.asm.simple.Callee2/1506553666@39b0a038]

The exception occurs on the invocation of getConstructor (args) with message:

java.lang.NoClassDefFoundError: test/code/jit/asm/simple/Callee2/1506553666
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2483)
    at java.lang.Class.getConstructor0(Class.java:2793)
    at java.lang.Class.getConstructor(Class.java:1708)
    at code.jit.asm.util.ReflectionUtil.adapt2GeneratedObject(ReflectionUtil.java:36)
    at code.jit.asm.services.BytecodeGenerator.generator(BytecodeGenerator.java:164)





Aucun commentaire:

Enregistrer un commentaire