Search in sources :

Example 11 with DexCodeVisitor

use of com.googlecode.d2j.visitors.DexCodeVisitor in project dex2jar by pxb1988.

the class OptSyncTest method test.

/**
     * Generate the following code
     * 
     * <pre>
     * public static void a() {
     * a0 = System.out
     * L0: 
     * lock a0 <= a0 is inside a try-catch
     * a1="haha"
     * a0.println(a1)
     * L1: 
     * unlock a0 
     * return
     * L2: 
     * a1 := @Exception 
     * unlock a0 
     * throw a1
     * ============= 
     * .catch L0 - L1 > L2 // all 
     * }
     * </pre>
     * 
     * @param cv
     */
@Test
public void test(DexClassVisitor cv) {
    DexMethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_STATIC, new Method("La;", "a", new String[] {}, "V"));
    DexCodeVisitor code = mv.visitCode();
    int v0 = 0;
    int v1 = 1;
    DexLabel try_start = new DexLabel();
    DexLabel try_end = new DexLabel();
    DexLabel catch_a = new DexLabel();
    code.visitTryCatch(try_start, try_end, new DexLabel[] { catch_a }, new String[] { null });
    code.visitRegister(2);
    code.visitFieldStmt(SGET_OBJECT, v0, -1, new Field("Ljava/lang/System;", "out", "Ljava/io/PrintStream;"));
    code.visitLabel(try_start);
    code.visitStmt1R(MONITOR_ENTER, v0);
    code.visitConstStmt(CONST_STRING, v1, "haha");
    code.visitMethodStmt(INVOKE_VIRTUAL, new int[] { v0, v1 }, new Method("Ljava/io/PrintString;", "println", new String[] { "Ljava/lang/String;" }, "V"));
    code.visitLabel(try_end);
    code.visitStmt1R(MONITOR_EXIT, v0);
    code.visitStmt0R(RETURN_VOID);
    code.visitLabel(catch_a);
    code.visitStmt1R(MOVE_EXCEPTION, v1);
    code.visitStmt1R(MONITOR_EXIT, v0);
    code.visitStmt1R(THROW, v1);
    code.visitEnd();
    mv.visitEnd();
}
Also used : Field(com.googlecode.d2j.Field) DexCodeVisitor(com.googlecode.d2j.visitors.DexCodeVisitor) DexLabel(com.googlecode.d2j.DexLabel) Method(com.googlecode.d2j.Method) DexMethodVisitor(com.googlecode.d2j.visitors.DexMethodVisitor) Test(org.junit.Test)

Example 12 with DexCodeVisitor

use of com.googlecode.d2j.visitors.DexCodeVisitor in project dex2jar by pxb1988.

the class ZeroTest method testZero.

@Test
public static void testZero(DexClassVisitor cv) {
    DexMethodVisitor mv = cv.visitMethod(ACC_STATIC, new Method("La;", "b", new String[] {}, "[Ljava/lang/Object;"));
    if (mv != null) {
        DexCodeVisitor code = mv.visitCode();
        if (code != null) {
            int v0 = 0;
            code.visitRegister(1);
            DexLabel L1 = new DexLabel();
            DexLabel L2 = new DexLabel();
            DexLabel L3 = new DexLabel();
            code.visitConstStmt(CONST, v0, 0);
            code.visitJumpStmt(IF_EQ, v0, v0, L2);
            code.visitLabel(L1);
            code.visitStmt1R(RETURN_OBJECT, v0);
            code.visitLabel(L2);
            code.visitJumpStmt(IF_EQ, v0, v0, L3);
            code.visitMethodStmt(INVOKE_STATIC, new int[0], new Method("La;", "getBytes", new String[0], "[B"));
            code.visitStmt1R(MOVE_RESULT_OBJECT, v0);
            code.visitLabel(L3);
            code.visitMethodStmt(INVOKE_STATIC, new int[] { v0 }, new Method("La;", "useBytes", new String[] { "[B" }, "V"));
            code.visitMethodStmt(INVOKE_STATIC, new int[0], new Method("La;", "getObjects", new String[0], "[Ljava/lang/Object;"));
            code.visitStmt1R(MOVE_RESULT, v0);
            code.visitJumpStmt(GOTO, -1, -1, L1);
            code.visitEnd();
        }
        mv.visitEnd();
    }
}
Also used : DexCodeVisitor(com.googlecode.d2j.visitors.DexCodeVisitor) DexLabel(com.googlecode.d2j.DexLabel) Method(com.googlecode.d2j.Method) DexMethodVisitor(com.googlecode.d2j.visitors.DexMethodVisitor) Test(org.junit.Test)

Example 13 with DexCodeVisitor

use of com.googlecode.d2j.visitors.DexCodeVisitor in project dex2jar by pxb1988.

the class DexFix method fixStaticFinalFieldValue.

/**
     * init value to default if the field is static and final, and the field is not init in clinit method
     *
     * erase the default value if the field is init in clinit method
     * 
     * @param classNode
     */
public static void fixStaticFinalFieldValue(final DexClassNode classNode) {
    if (classNode.fields == null) {
        return;
    }
    final Map<String, DexFieldNode> fs = new HashMap<>();
    final Map<String, DexFieldNode> shouldNotBeAssigned = new HashMap<>();
    for (DexFieldNode fn : classNode.fields) {
        if ((fn.access & ACC_STATIC_FINAL) == ACC_STATIC_FINAL) {
            if (fn.cst == null) {
                char t = fn.field.getType().charAt(0);
                if (t == 'L' || t == '[') {
                    // ignore Object
                    continue;
                }
                fs.put(fn.field.getName() + ":" + fn.field.getType(), fn);
            } else if (isPrimitiveZero(fn.field.getType(), fn.cst)) {
                shouldNotBeAssigned.put(fn.field.getName() + ":" + fn.field.getType(), fn);
            }
        }
    }
    if (fs.isEmpty() && shouldNotBeAssigned.isEmpty()) {
        return;
    }
    DexMethodNode node = null;
    if (classNode.methods != null) {
        for (DexMethodNode mn : classNode.methods) {
            if (mn.method.getName().equals("<clinit>")) {
                node = mn;
                break;
            }
        }
    }
    if (node != null) {
        if (node.codeNode != null) {
            node.codeNode.accept(new DexCodeVisitor() {

                @Override
                public void visitFieldStmt(Op op, int a, int b, Field field) {
                    switch(op) {
                        case SPUT:
                        case SPUT_BOOLEAN:
                        case SPUT_BYTE:
                        case SPUT_CHAR:
                        case SPUT_OBJECT:
                        case SPUT_SHORT:
                        case SPUT_WIDE:
                            if (field.getOwner().equals(classNode.className)) {
                                String key = field.getName() + ":" + field.getType();
                                fs.remove(key);
                                DexFieldNode dn = shouldNotBeAssigned.get(key);
                                if (dn != null) {
                                    //System.out.println(field.getName() + ":" + field.getType());
                                    dn.cst = null;
                                }
                            }
                            break;
                        default:
                            // ignored
                            break;
                    }
                }
            });
        } else {
            // has init but no code
            return;
        }
    }
    for (DexFieldNode fn : fs.values()) {
        fn.cst = getDefaultValueOfType(fn.field.getType().charAt(0));
    }
}
Also used : Op(com.googlecode.d2j.reader.Op) Field(com.googlecode.d2j.Field) HashMap(java.util.HashMap) DexFieldNode(com.googlecode.d2j.node.DexFieldNode) DexMethodNode(com.googlecode.d2j.node.DexMethodNode) DexCodeVisitor(com.googlecode.d2j.visitors.DexCodeVisitor)

Example 14 with DexCodeVisitor

use of com.googlecode.d2j.visitors.DexCodeVisitor in project dex2jar by pxb1988.

the class ArrayTypeTest method a123.

@Test
public static void a123(DexClassVisitor cv) {
    DexMethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_STATIC, new Method("La;", "b", new String[] {}, "V"));
    DexCodeVisitor code = mv.visitCode();
    code.visitRegister(3);
    code.visitConstStmt(CONST, 0, 0);
    code.visitConstStmt(CONST, 1, 1);
    code.visitConstStmt(CONST, 2, 0x63);
    code.visitStmt3R(APUT, 2, 0, 1);
    code.visitStmt0R(RETURN_VOID);
    code.visitEnd();
    mv.visitEnd();
}
Also used : DexCodeVisitor(com.googlecode.d2j.visitors.DexCodeVisitor) Method(com.googlecode.d2j.Method) DexMethodVisitor(com.googlecode.d2j.visitors.DexMethodVisitor) Test(org.junit.Test)

Example 15 with DexCodeVisitor

use of com.googlecode.d2j.visitors.DexCodeVisitor in project dex2jar by pxb1988.

the class ArrayTypeTest method a122.

@Test
public static void a122(DexClassVisitor cv) {
    DexMethodVisitor mv = cv.visitMethod(ACC_PUBLIC | ACC_STATIC, new Method("La;", "b", new String[] {}, "V"));
    DexCodeVisitor code = mv.visitCode();
    code.visitRegister(3);
    code.visitConstStmt(CONST, 0, Integer.valueOf(0));
    code.visitConstStmt(CONST, 2, Integer.valueOf(1));
    code.visitStmt3R(AGET, 1, 0, 2);
    code.visitStmt0R(RETURN_VOID);
    code.visitEnd();
    mv.visitEnd();
}
Also used : DexCodeVisitor(com.googlecode.d2j.visitors.DexCodeVisitor) Method(com.googlecode.d2j.Method) DexMethodVisitor(com.googlecode.d2j.visitors.DexMethodVisitor) Test(org.junit.Test)

Aggregations

DexCodeVisitor (com.googlecode.d2j.visitors.DexCodeVisitor)23 DexMethodVisitor (com.googlecode.d2j.visitors.DexMethodVisitor)19 Method (com.googlecode.d2j.Method)18 Test (org.junit.Test)13 DexLabel (com.googlecode.d2j.DexLabel)9 Field (com.googlecode.d2j.Field)8 Op (com.googlecode.d2j.reader.Op)4 DexClassVisitor (com.googlecode.d2j.visitors.DexClassVisitor)4 HashMap (java.util.HashMap)4 DexMethodNode (com.googlecode.d2j.node.DexMethodNode)2 DexAnnotationVisitor (com.googlecode.d2j.visitors.DexAnnotationVisitor)2 DexFieldVisitor (com.googlecode.d2j.visitors.DexFieldVisitor)2 File (java.io.File)2 DexFileWriter (com.googlecode.d2j.dex.writer.DexFileWriter)1 DexCodeNode (com.googlecode.d2j.node.DexCodeNode)1 DexFieldNode (com.googlecode.d2j.node.DexFieldNode)1 DexStmtNode (com.googlecode.d2j.node.insn.DexStmtNode)1 DexFileReader (com.googlecode.d2j.reader.DexFileReader)1 ASMifierFileV (com.googlecode.d2j.util.ASMifierFileV)1 DexAnnotationAble (com.googlecode.d2j.visitors.DexAnnotationAble)1