Search in sources :

Example 21 with Method

use of com.googlecode.d2j.Method in project dex2jar by pxb1988.

the class AutoCastTest method strict.

/**
     * generate code, it works fine on JVM, but fails on Dalvik VM
     * 
     * <pre>
     * class a {
     *     private static short theField;
     * 
     *     public a() {
     *         theField = 0xffFFffFF + theField;// the 0xffFFffFF is not casted
     *     }
     * }
     * </pre>
     * 
     * @param cv
     */
public static void strict(DexClassVisitor cv) {
    Field f = new Field("La;", "theField", "S");
    DexMethodVisitor mv = cv.visitMethod(ACC_PUBLIC, new Method("La;", "<init>", new String[] {}, "V"));
    if (mv != null) {
        DexCodeVisitor code = mv.visitCode();
        if (code != null) {
            code.visitRegister(3);
            code.visitMethodStmt(INVOKE_SUPER, new int[] { 2 }, new Method("Ljava/lang/Object;", "<init>", new String[] {}, "V"));
            code.visitFieldStmt(SGET_BOOLEAN, 0, -1, f);
            code.visitConstStmt(CONST, 1, 0xffFFffFF);
            code.visitStmt3R(ADD_INT, 0, 0, 1);
            code.visitFieldStmt(SPUT_SHORT, 0, -1, f);
            code.visitStmt0R(RETURN_VOID);
            code.visitEnd();
        }
        mv.visitEnd();
    }
    DexFieldVisitor fv = cv.visitField(ACC_PRIVATE | ACC_STATIC, f, 0);
    if (fv != null) {
        fv.visitEnd();
    }
}
Also used : Field(com.googlecode.d2j.Field) DexFieldVisitor(com.googlecode.d2j.visitors.DexFieldVisitor) DexCodeVisitor(com.googlecode.d2j.visitors.DexCodeVisitor) Method(com.googlecode.d2j.Method) DexMethodVisitor(com.googlecode.d2j.visitors.DexMethodVisitor)

Example 22 with Method

use of com.googlecode.d2j.Method in project dex2jar by pxb1988.

the class AppWriterTest method test3.

@Test
public void test3() {
    DexFileWriter w = new DexFileWriter();
    DexClassVisitor cv = w.visit(0x1, "La/c;", null, new String[] { "Ljava/lang/Comparable;" });
    cv.visitSource("c.java");
    cv.visitAnnotation("LAnn;", Visibility.SYSTEM).visitEnd();
    DexFieldVisitor fv = cv.visitField(ACC_PUBLIC | ACC_STATIC, new Field("La/c;", "a", "I"), 55);
    fv.visitAnnotation("LE;", Visibility.RUNTIME).visitEnd();
    fv.visitEnd();
    DexMethodVisitor mv = cv.visitMethod(ACC_STATIC, new Method("La/c;", "bb", new String[] { "I" }, "V"));
    mv.visitAnnotation("Laaa;", Visibility.RUNTIME).visitEnd();
    DexAnnotationVisitor dav = mv.visitParameterAnnotation(0).visitAnnotation("Laaa;", Visibility.RUNTIME);
    dav.visit("abc", true);
    DexAnnotationVisitor dav2 = dav.visitArray("efg");
    dav2.visit("", "123");
    dav2.visit("", "456");
    dav2.visitEnd();
    dav.visitEnd();
    DexCodeVisitor code = mv.visitCode();
    code.visitRegister(2);
    code.visitStmt0R(Op.RETURN_VOID);
    code.visitEnd();
    mv.visitEnd();
    cv.visitEnd();
    w.visitEnd();
    w.toByteArray();
}
Also used : Field(com.googlecode.d2j.Field) DexFileWriter(com.googlecode.d2j.dex.writer.DexFileWriter) Method(com.googlecode.d2j.Method) Test(org.junit.Test)

Aggregations

Method (com.googlecode.d2j.Method)22 DexCodeVisitor (com.googlecode.d2j.visitors.DexCodeVisitor)18 DexMethodVisitor (com.googlecode.d2j.visitors.DexMethodVisitor)18 Test (org.junit.Test)13 DexLabel (com.googlecode.d2j.DexLabel)10 Field (com.googlecode.d2j.Field)9 DexClassVisitor (com.googlecode.d2j.visitors.DexClassVisitor)4 Op (com.googlecode.d2j.reader.Op)3 HashMap (java.util.HashMap)3 DexFileWriter (com.googlecode.d2j.dex.writer.DexFileWriter)2 DexFieldVisitor (com.googlecode.d2j.visitors.DexFieldVisitor)2 File (java.io.File)2 DexType (com.googlecode.d2j.DexType)1 DexCodeNode (com.googlecode.d2j.node.DexCodeNode)1 DexMethodNode (com.googlecode.d2j.node.DexMethodNode)1 DvmInterpreter (com.googlecode.d2j.node.analysis.DvmInterpreter)1 DexFileReader (com.googlecode.d2j.reader.DexFileReader)1 ASMifierFileV (com.googlecode.d2j.util.ASMifierFileV)1 DexAnnotationAble (com.googlecode.d2j.visitors.DexAnnotationAble)1 DexAnnotationVisitor (com.googlecode.d2j.visitors.DexAnnotationVisitor)1