Search in sources :

Example 6 with Field

use of com.googlecode.d2j.Field 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 7 with Field

use of com.googlecode.d2j.Field 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 8 with Field

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

the class EmptyTrapTest method m005_toJSONString.

@Test
public static void m005_toJSONString(DexClassVisitor cv) {
    DexMethodVisitor mv = cv.visitMethod(0, new Method("LJSResponseTest;", "toJSONString", new String[] {}, "Ljava/lang/String;"));
    if (mv != null) {
        DexCodeVisitor code = mv.visitCode();
        if (code != null) {
            code.visitRegister(6);
            DexLabel L8 = new DexLabel();
            DexLabel L9 = new DexLabel();
            DexLabel L10 = new DexLabel();
            DexLabel L0 = new DexLabel();
            DexLabel L1 = new DexLabel();
            DexLabel L2 = new DexLabel();
            code.visitTryCatch(L0, L1, new DexLabel[] { L2 }, new String[] { "Lorg/json/JSONException;" });
            DexLabel L3 = new DexLabel();
            DexLabel L4 = new DexLabel();
            code.visitTryCatch(L3, L4, new DexLabel[] { L2 }, new String[] { "Lorg/json/JSONException;" });
            DexLabel L5 = new DexLabel();
            DexLabel L6 = new DexLabel();
            code.visitTryCatch(L5, L6, new DexLabel[] { L2 }, new String[] { "Lorg/json/JSONException;" });
            code.visitConstStmt(CONST_STRING, 2, "response");
            code.visitConstStmt(CONST_STRING, 4, "");
            code.visitLabel(L0);
            code.visitFieldStmt(IGET, 2, 5, new Field("LJSResponseTest;", "className", "Ljava/lang/String;"));
            code.visitJumpStmt(IF_EQZ, 2, -1, L8);
            code.visitFieldStmt(IGET, 2, 5, new Field("LJSResponseTest;", "methodName", "Ljava/lang/String;"));
            code.visitJumpStmt(IF_NEZ, 2, -1, L10);
            code.visitLabel(L8);
            code.visitConstStmt(CONST_STRING, 2, "");
            code.visitStmt2R(MOVE, 2, 4);
            code.visitLabel(L9);
            code.visitStmt1R(RETURN_OBJECT, 2);
            code.visitLabel(L10);
            code.visitTypeStmt(NEW_INSTANCE, 1, -1, "Lorg/json/JSONObject;");
            code.visitMethodStmt(INVOKE_DIRECT, new int[] { 1 }, new Method("Lorg/json/JSONObject;", "<init>", new String[] {}, "V"));
            code.visitConstStmt(CONST_STRING, 2, "class");
            code.visitFieldStmt(IGET, 3, 5, new Field("LJSResponseTest;", "className", "Ljava/lang/String;"));
            code.visitMethodStmt(INVOKE_VIRTUAL, new int[] { 1, 2, 3 }, new Method("Lorg/json/JSONObject;", "put", new String[] { "Ljava/lang/String;", "Ljava/lang/Object;" }, "Lorg/json/JSONObject;"));
            code.visitConstStmt(CONST_STRING, 2, "call");
            code.visitFieldStmt(IGET, 3, 5, new Field("LJSResponseTest;", "methodName", "Ljava/lang/String;"));
            code.visitMethodStmt(INVOKE_VIRTUAL, new int[] { 1, 2, 3 }, new Method("Lorg/json/JSONObject;", "put", new String[] { "Ljava/lang/String;", "Ljava/lang/Object;" }, "Lorg/json/JSONObject;"));
            code.visitConstStmt(CONST_STRING, 2, "result");
            code.visitFieldStmt(IGET, 3, 5, new Field("LJSResponseTest;", "result", "I"));
            code.visitMethodStmt(INVOKE_VIRTUAL, new int[] { 1, 2, 3 }, new Method("Lorg/json/JSONObject;", "put", new String[] { "Ljava/lang/String;", "I" }, "Lorg/json/JSONObject;"));
            code.visitFieldStmt(IGET, 2, 5, new Field("LJSResponseTest;", "response", "Ljava/lang/Object;"));
            code.visitJumpStmt(IF_EQZ, 2, -1, L3);
            code.visitConstStmt(CONST_STRING, 2, "response");
            code.visitFieldStmt(IGET, 3, 5, new Field("LJSResponseTest;", "response", "Ljava/lang/Object;"));
            code.visitMethodStmt(INVOKE_VIRTUAL, new int[] { 1, 2, 3 }, new Method("Lorg/json/JSONObject;", "put", new String[] { "Ljava/lang/String;", "Ljava/lang/Object;" }, "Lorg/json/JSONObject;"));
            code.visitLabel(L1);
            code.visitMethodStmt(INVOKE_VIRTUAL, new int[] { 1 }, new Method("Lorg/json/JSONObject;", "toString", new String[] {}, "Ljava/lang/String;"));
            code.visitStmt1R(MOVE_RESULT_OBJECT, 2);
            code.visitJumpStmt(GOTO, -1, -1, L9);
            code.visitLabel(L3);
            code.visitFieldStmt(IGET, 2, 5, new Field("LJSResponseTest;", "dataResponse", "[B"));
            code.visitJumpStmt(IF_EQZ, 2, -1, L5);
            code.visitConstStmt(CONST_STRING, 2, "response");
            code.visitFieldStmt(IGET, 3, 5, new Field("LJSResponseTest;", "dataResponse", "[B"));
            code.visitMethodStmt(INVOKE_STATIC, new int[] { 3 }, new Method("LBase64;", "encode", new String[] { "[B" }, "Ljava/lang/String;"));
            code.visitStmt1R(MOVE_RESULT, 3);
            code.visitMethodStmt(INVOKE_VIRTUAL, new int[] { 1, 2, 3 }, new Method("Lorg/json/JSONObject;", "put", new String[] { "Ljava/lang/String;", "Ljava/lang/Object;" }, "Lorg/json/JSONObject;"));
            code.visitLabel(L4);
            code.visitJumpStmt(GOTO, -1, -1, L1);
            code.visitLabel(L2);
            code.visitStmt1R(MOVE_EXCEPTION, 2);
            code.visitStmt2R(MOVE, 0, 2);
            code.visitConstStmt(CONST_STRING, 2, "MillennialMediaSDK");
            code.visitMethodStmt(INVOKE_VIRTUAL, new int[] { 0 }, new Method("Lorg/json/JSONException;", "getMessage", new String[] {}, "Ljava/lang/String;"));
            code.visitStmt1R(MOVE_RESULT, 3);
            code.visitMethodStmt(INVOKE_STATIC, new int[] { 2, 3 }, new Method("Landroid/util/Log;", "e", new String[] { "Ljava/lang/String;", "Ljava/lang/String;" }, "I"));
            code.visitConstStmt(CONST_STRING, 2, "");
            code.visitStmt2R(MOVE, 2, 4);
            code.visitJumpStmt(GOTO, -1, -1, L9);
            code.visitLabel(L5);
            code.visitConstStmt(CONST_STRING, 2, "");
            code.visitLabel(L6);
            code.visitStmt2R(MOVE, 2, 4);
            code.visitJumpStmt(GOTO, -1, -1, L9);
            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 9 with Field

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

the class Utils method doAccept.

public static void doAccept(DexAnnotationVisitor dexAnnotationVisitor, String k, Object value) {
    if (value instanceof ArrayList) {
        DexAnnotationVisitor a = dexAnnotationVisitor.visitArray(k);
        for (Object o : (ArrayList) value) {
            doAccept(a, null, o);
        }
        a.visitEnd();
    } else if (value instanceof Ann) {
        Ann ann = (Ann) value;
        DexAnnotationVisitor a = dexAnnotationVisitor.visitAnnotation(k, ann.name);
        for (Map.Entry<String, Object> e : ann.elements) {
            doAccept(a, e.getKey(), e.getValue());
        }
        a.visitEnd();
    } else if (value instanceof Field) {
        Field f = (Field) value;
        dexAnnotationVisitor.visitEnum(k, f.getOwner(), f.getName());
    } else {
        dexAnnotationVisitor.visit(k, value);
    }
}
Also used : Field(com.googlecode.d2j.Field) DexAnnotationVisitor(com.googlecode.d2j.visitors.DexAnnotationVisitor) ArrayList(java.util.ArrayList)

Example 10 with Field

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

the class DexWeaver method genSwitchMethod.

private void genSwitchMethod(DexClassVisitor dcv, String typeNameDesc, String methodName, CB callback) {
    DexMethodVisitor dmv = dcv.visitMethod(DexConstants.ACC_PUBLIC, new Method(typeNameDesc, methodName, new String[0], "Ljava/lang/String;"));
    DexCodeVisitor code = dmv.visitCode();
    code.visitRegister(3);
    code.visitFieldStmt(Op.IGET, 0, 2, new Field(typeNameDesc, "idx", "I"));
    DexLabel[] labels = new DexLabel[callbacks.size()];
    Map<String, DexLabel> strMap = new TreeMap<>();
    for (int i = 0; i < labels.length; i++) {
        Callback cb = callbacks.get(i);
        String key = callback.getKey((Method) cb.target);
        DexLabel label = strMap.get(key);
        if (label == null) {
            label = new DexLabel();
            strMap.put(key, label);
        }
        labels[i] = label;
    }
    code.visitPackedSwitchStmt(Op.PACKED_SWITCH, 0, 0, labels);
    code.visitTypeStmt(Op.NEW_INSTANCE, 0, 0, "Ljava/lang/RuntimeException;");
    code.visitConstStmt(Op.CONST_STRING, 1, "invalid idx");
    code.visitMethodStmt(Op.INVOKE_DIRECT, new int[] { 0, 1 }, new Method("Ljava/lang/RuntimeException;", "<init>", new String[] { "Ljava/lang/String;" }, "V"));
    code.visitStmt1R(Op.THROW, 0);
    for (Map.Entry<String, DexLabel> e : strMap.entrySet()) {
        code.visitLabel(e.getValue());
        code.visitConstStmt(Op.CONST_STRING, 0, e.getKey());
        code.visitStmt1R(Op.RETURN_OBJECT, 0);
    }
    code.visitEnd();
    dmv.visitEnd();
}
Also used : Method(com.googlecode.d2j.Method) TreeMap(java.util.TreeMap) Field(com.googlecode.d2j.Field) DexCodeVisitor(com.googlecode.d2j.visitors.DexCodeVisitor) DexLabel(com.googlecode.d2j.DexLabel) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) DexMethodVisitor(com.googlecode.d2j.visitors.DexMethodVisitor)

Aggregations

Field (com.googlecode.d2j.Field)12 Method (com.googlecode.d2j.Method)9 DexCodeVisitor (com.googlecode.d2j.visitors.DexCodeVisitor)8 DexMethodVisitor (com.googlecode.d2j.visitors.DexMethodVisitor)7 DexLabel (com.googlecode.d2j.DexLabel)6 Test (org.junit.Test)5 DexAnnotationVisitor (com.googlecode.d2j.visitors.DexAnnotationVisitor)3 Op (com.googlecode.d2j.reader.Op)2 DexClassVisitor (com.googlecode.d2j.visitors.DexClassVisitor)2 DexFieldVisitor (com.googlecode.d2j.visitors.DexFieldVisitor)2 HashMap (java.util.HashMap)2 DexType (com.googlecode.d2j.DexType)1 DexFileWriter (com.googlecode.d2j.dex.writer.DexFileWriter)1 DexFieldNode (com.googlecode.d2j.node.DexFieldNode)1 DexMethodNode (com.googlecode.d2j.node.DexMethodNode)1 DvmInterpreter (com.googlecode.d2j.node.analysis.DvmInterpreter)1 ASMifierFileV (com.googlecode.d2j.util.ASMifierFileV)1 DexAnnotationAble (com.googlecode.d2j.visitors.DexAnnotationAble)1 IrMethod (com.googlecode.dex2jar.ir.IrMethod)1 Local (com.googlecode.dex2jar.ir.expr.Local)1