qdrzwd / dexmaker

Automatically exported from code.google.com/p/dexmaker
0 stars 0 forks source link

Non-trivial examples? #11

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

Liking the project very much so far.

However, I'm having trouble building a non-trivial class.  I'd like to create a 
class with a constructor accepting (for example) a String as an argument.  It's 
not clear how this is done:

1.  Do I have to explicitly call the no-arg constructor for Object (the direct 
"super")?  The verifier keeps rejecting my attempts:

W/dalvikvm( 6556): VFY: invalid call to Ljava/lang/Object;.<init>
W/dalvikvm( 6556): VFY:  rejecting call to Ljava/lang/Object;.<init> ()V
W/dalvikvm( 6556): VFY:  rejecting opcode 0x6f at 0x0000

Anyway, here's what I'm doing.  I've tried many variations of the call, 
including not making the call to Object.<init>.  Any insight is welcome.

        TypeId<?> someClassType = TypeId.get("LSomeClass;");
        TypeId<Object> objectType = TypeId.get(Object.class);
        MethodId objectConstructor = objectType.getConstructor();

        dexMaker.declare(someClassType, "NewClass.generated", Modifier.PUBLIC, TypeId.OBJECT);

        MethodId constructor1 = fileType.getConstructor(TypeId.STRING);

        Code code = dexMaker.declare(constructor1, Modifier.PUBLIC);
        code.invokeSuper(objectConstructor, null, null);//?

        code.returnVoid();//proper close of constructor?

        MethodId newMethod = someClassType.getMethod(TypeId.VOID, "newMethod", TypeId.STRING);
        code = dexMaker.declare(newMethod, Modifier.PUBLIC);
        //do some stuff
        code.returnVoid();

Original issue reported on code.google.com by w.swaney...@gmail.com on 25 Jul 2012 at 10:54

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
For best results, questions like this should be asked on StackOverflow!

I think in this case your best example is 
com.google.dexmaker.stock.ProxyBuilder. It does this:
            MethodId<T, ?> superConstructor = superType.getConstructor(types);
            constructorCode.invokeDirect(superConstructor, null, thisRef, params);
            constructorCode.returnVoid();

Original comment by limpbizkit on 10 Oct 2012 at 3:48