Search in sources :

Example 6 with DexFileNode

use of com.googlecode.d2j.node.DexFileNode in project dex2jar by pxb1988.

the class SmaliTest method test.

@Test
public void test() throws IOException {
    DexFileNode dfn = new DexFileNode();
    Smali.smaliFile(new File("src/test/resources/a.smali").toPath(), dfn);
    for (DexClassNode dcn : dfn.clzs) {
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
        new BaksmaliDumper(true, true).baksmaliClass(dcn, new BaksmaliDumpOut(w));
        w.flush();
    }
}
Also used : DexClassNode(com.googlecode.d2j.node.DexClassNode) BaksmaliDumper(com.googlecode.d2j.smali.BaksmaliDumper) DexFileNode(com.googlecode.d2j.node.DexFileNode) DexBackedDexFile(org.jf.dexlib2.dexbacked.DexBackedDexFile) BaksmaliDumpOut(com.googlecode.d2j.smali.BaksmaliDumpOut) Test(org.junit.Test)

Example 7 with DexFileNode

use of com.googlecode.d2j.node.DexFileNode in project dex2jar by pxb1988.

the class SmaliTest method readDex.

Map<String, DexClassNode> readDex(File path) throws IOException {
    DexFileReader dexFileReader = new DexFileReader(ZipUtil.readDex(path));
    DexFileNode dexFileNode = new DexFileNode();
    dexFileReader.accept(dexFileNode);
    Map<String, DexClassNode> map = new HashMap<>();
    for (DexClassNode c : dexFileNode.clzs) {
        map.put(c.className, c);
    }
    return map;
}
Also used : DexClassNode(com.googlecode.d2j.node.DexClassNode) HashMap(java.util.HashMap) DexFileNode(com.googlecode.d2j.node.DexFileNode) DexFileReader(com.googlecode.d2j.reader.DexFileReader)

Example 8 with DexFileNode

use of com.googlecode.d2j.node.DexFileNode in project dex2jar by pxb1988.

the class Dex2jar method doTranslate.

private void doTranslate(final Path dist) throws IOException {
    DexFileNode fileNode = new DexFileNode();
    try {
        reader.accept(fileNode, readerConfig | DexFileReader.IGNORE_READ_EXCEPTION);
    } catch (Exception ex) {
        exceptionHandler.handleFileException(ex);
    }
    ClassVisitorFactory cvf = new ClassVisitorFactory() {

        @Override
        public ClassVisitor create(final String name) {
            return new ClassVisitor(Opcodes.ASM4, new ClassWriter(ClassWriter.COMPUTE_MAXS)) {

                @Override
                public void visitEnd() {
                    super.visitEnd();
                    ClassWriter cw = (ClassWriter) super.cv;
                    byte[] data;
                    try {
                        // FIXME handle 'java.lang.RuntimeException: Method code too large!'
                        data = cw.toByteArray();
                    } catch (Exception ex) {
                        System.err.println(String.format("ASM fail to generate .class file: %s", name));
                        exceptionHandler.handleFileException(ex);
                        return;
                    }
                    try {
                        Path dist1 = dist.resolve(name + ".class");
                        Path parent = dist1.getParent();
                        if (parent != null && !Files.exists(parent)) {
                            Files.createDirectories(parent);
                        }
                        Files.write(dist1, data);
                    } catch (IOException e) {
                        e.printStackTrace(System.err);
                    }
                }
            };
        }
    };
    new ExDex2Asm(exceptionHandler) {

        public void convertCode(DexMethodNode methodNode, MethodVisitor mv) {
            if ((readerConfig & DexFileReader.SKIP_CODE) != 0 && methodNode.method.getName().equals("<clinit>")) {
                // also skip clinit
                return;
            }
            super.convertCode(methodNode, mv);
        }

        @Override
        public void optimize(IrMethod irMethod) {
            T_cleanLabel.transform(irMethod);
            if (0 != (v3Config & V3.TOPOLOGICAL_SORT)) {
            // T_topologicalSort.transform(irMethod);
            }
            T_deadCode.transform(irMethod);
            T_removeLocal.transform(irMethod);
            T_removeConst.transform(irMethod);
            T_zero.transform(irMethod);
            if (T_npe.transformReportChanged(irMethod)) {
                T_deadCode.transform(irMethod);
                T_removeLocal.transform(irMethod);
                T_removeConst.transform(irMethod);
            }
            T_new.transform(irMethod);
            T_fillArray.transform(irMethod);
            T_agg.transform(irMethod);
            T_multiArray.transform(irMethod);
            T_voidInvoke.transform(irMethod);
            if (0 != (v3Config & V3.PRINT_IR)) {
                int i = 0;
                for (Stmt p : irMethod.stmts) {
                    if (p.st == Stmt.ST.LABEL) {
                        LabelStmt labelStmt = (LabelStmt) p;
                        labelStmt.displayName = "L" + i++;
                    }
                }
                System.out.println(irMethod);
            }
            T_type.transform(irMethod);
            T_unssa.transform(irMethod);
            T_ir2jRegAssign.transform(irMethod);
            T_trimEx.transform(irMethod);
        }

        @Override
        public void ir2j(IrMethod irMethod, MethodVisitor mv) {
            new IR2JConverter(0 != (V3.OPTIMIZE_SYNCHRONIZED & v3Config)).convert(irMethod, mv);
        }
    }.convertDex(fileNode, cvf);
}
Also used : Path(java.nio.file.Path) LabelStmt(com.googlecode.dex2jar.ir.stmt.LabelStmt) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) IrMethod(com.googlecode.dex2jar.ir.IrMethod) IOException(java.io.IOException) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) LabelStmt(com.googlecode.dex2jar.ir.stmt.LabelStmt) Stmt(com.googlecode.dex2jar.ir.stmt.Stmt) DexMethodNode(com.googlecode.d2j.node.DexMethodNode) DexFileNode(com.googlecode.d2j.node.DexFileNode) IR2JConverter(com.googlecode.d2j.converter.IR2JConverter)

Example 9 with DexFileNode

use of com.googlecode.d2j.node.DexFileNode in project dex2jar by pxb1988.

the class GenerateCompileStubFromOdex method doDex.

private void doDex(ByteBuffer bs, final Path out) {
    DexFileReader reader = new DexFileReader(bs);
    DexFileNode fileNode = new DexFileNode();
    reader.accept(fileNode, DexFileReader.SKIP_CODE);
    Dex2Asm dex2Asm = new Dex2Asm();
    dex2Asm.convertDex(fileNode, new ClassVisitorFactory() {

        @Override
        public ClassVisitor create(final String classInternalName) {
            final Path target = out.resolve(classInternalName + ".class");
            if (Files.exists(target)) {
                System.err.println("class " + classInternalName + " is already exists, skipping.");
                return null;
            }
            return new ClassVisitor(Opcodes.ASM4, new ClassWriter(ClassWriter.COMPUTE_MAXS)) {

                @Override
                public void visitEnd() {
                    super.visitEnd();
                    ClassWriter cw = (ClassWriter) cv;
                    byte[] data = cw.toByteArray();
                    try {
                        BaseCmd.createParentDirectories(target);
                        Files.write(target, data);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
                    if (noPrivate && 0 != (access & Opcodes.ACC_PRIVATE)) {
                        return null;
                    }
                    return super.visitField(access, name, desc, signature, value);
                }

                @Override
                public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
                    if (noPrivate && 0 != (access & Opcodes.ACC_PRIVATE)) {
                        return null;
                    }
                    MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
                    if (0 != ((Opcodes.ACC_NATIVE | Opcodes.ACC_ABSTRACT) & access)) {
                        return mv;
                    }
                    mv.visitTypeInsn(Opcodes.NEW, "java/lang/RuntimeException");
                    mv.visitInsn(Opcodes.DUP);
                    mv.visitLdcInsn("stub");
                    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V");
                    mv.visitInsn(Opcodes.ATHROW);
                    return mv;
                }
            };
        }
    });
}
Also used : Path(java.nio.file.Path) DexFileReader(com.googlecode.d2j.reader.DexFileReader) IOException(java.io.IOException) Dex2Asm(com.googlecode.d2j.dex.Dex2Asm) DexFileNode(com.googlecode.d2j.node.DexFileNode) ClassVisitorFactory(com.googlecode.d2j.dex.ClassVisitorFactory)

Aggregations

DexFileNode (com.googlecode.d2j.node.DexFileNode)9 DexClassNode (com.googlecode.d2j.node.DexClassNode)4 DexFileReader (com.googlecode.d2j.reader.DexFileReader)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 ClassVisitorFactory (com.googlecode.d2j.dex.ClassVisitorFactory)2 Test (org.junit.Test)2 ClassVisitor (org.objectweb.asm.ClassVisitor)2 ClassWriter (org.objectweb.asm.ClassWriter)2 IR2JConverter (com.googlecode.d2j.converter.IR2JConverter)1 Dex2Asm (com.googlecode.d2j.dex.Dex2Asm)1 ExDex2Asm (com.googlecode.d2j.dex.ExDex2Asm)1 DexMethodNode (com.googlecode.d2j.node.DexMethodNode)1 BaseDexFileReader (com.googlecode.d2j.reader.BaseDexFileReader)1 BaksmaliDumpOut (com.googlecode.d2j.smali.BaksmaliDumpOut)1 BaksmaliDumper (com.googlecode.d2j.smali.BaksmaliDumper)1 IrMethod (com.googlecode.dex2jar.ir.IrMethod)1 LabelStmt (com.googlecode.dex2jar.ir.stmt.LabelStmt)1 Stmt (com.googlecode.dex2jar.ir.stmt.Stmt)1 File (java.io.File)1