Search in sources :

Example 1 with BaseDexFileReader

use of com.googlecode.d2j.reader.BaseDexFileReader in project dex2jar by pxb1988.

the class Dex2jarMultiThreadCmd method run0.

private void run0(String fileName, final ExecutorService executorService) throws IOException {
    // long baseTS = System.currentTimeMillis();
    String baseName = getBaseName(new File(fileName).toPath());
    Path currentDir = new File(".").toPath();
    Path file = currentDir.resolve(baseName + "-dex2jar.jar");
    final Path errorFile = currentDir.resolve(baseName + "-error.zip");
    System.err.println("dex2jar " + fileName + " -> " + file);
    final BaksmaliBaseDexExceptionHandler exceptionHandler = new BaksmaliBaseDexExceptionHandler();
    BaseDexFileReader reader = MultiDexFileReader.open(Files.readAllBytes(new File(fileName).toPath()));
    DexFileNode fileNode = new DexFileNode();
    try {
        reader.accept(fileNode, DexFileReader.SKIP_DEBUG | DexFileReader.IGNORE_READ_EXCEPTION);
    } catch (Exception ex) {
        exceptionHandler.handleFileException(ex);
        throw ex;
    }
    final FileSystem fs = createZip(file);
    final Path dist = fs.getPath("/");
    ClassVisitorFactory cvf = new ClassVisitorFactory() {

        @Override
        public ClassVisitor create(final String name) {
            final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            final LambadaNameSafeClassAdapter rca = new LambadaNameSafeClassAdapter(cw);
            return new ClassVisitor(Opcodes.ASM4, rca) {

                @Override
                public void visitEnd() {
                    super.visitEnd();
                    String className = rca.getClassName();
                    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", className));
                        exceptionHandler.handleFileException(ex);
                        return;
                    }
                    try {
                        Path dist1 = dist.resolve(className + ".class");
                        BaseCmd.createParentDirectories(dist1);
                        Files.write(dist1, data);
                    } catch (IOException e) {
                        exceptionHandler.handleFileException(e);
                    }
                }
            };
        }
    };
    new ExDex2Asm(exceptionHandler) {

        @Override
        public void convertDex(final DexFileNode fileNode, final ClassVisitorFactory cvf) {
            if (fileNode.clzs != null) {
                final Map<String, Clz> classes = collectClzInfo(fileNode);
                final List<Future<?>> results = new ArrayList<>(fileNode.clzs.size());
                for (final DexClassNode classNode : fileNode.clzs) {
                    results.add(executorService.submit(new Runnable() {

                        @Override
                        public void run() {
                            convertClass(fileNode, classNode, cvf, classes);
                        }
                    }));
                }
                executorService.submit(new Runnable() {

                    @Override
                    public void run() {
                        for (Future<?> result : results) {
                            try {
                                result.get();
                            } catch (InterruptedException | ExecutionException e) {
                                e.printStackTrace();
                            }
                        }
                        BaksmaliBaseDexExceptionHandler exceptionHandler1 = (BaksmaliBaseDexExceptionHandler) exceptionHandler;
                        if (exceptionHandler1.hasException()) {
                            exceptionHandler1.dump(errorFile, new String[0]);
                        }
                        try {
                            fs.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }.convertDex(fileNode, cvf);
}
Also used : LambadaNameSafeClassAdapter(com.googlecode.d2j.dex.LambadaNameSafeClassAdapter) ClassVisitor(org.objectweb.asm.ClassVisitor) DexClassNode(com.googlecode.d2j.node.DexClassNode) FileSystem(java.nio.file.FileSystem) ExDex2Asm(com.googlecode.d2j.dex.ExDex2Asm) Path(java.nio.file.Path) IOException(java.io.IOException) IOException(java.io.IOException) ClassWriter(org.objectweb.asm.ClassWriter) DexFileNode(com.googlecode.d2j.node.DexFileNode) ClassVisitorFactory(com.googlecode.d2j.dex.ClassVisitorFactory) BaseDexFileReader(com.googlecode.d2j.reader.BaseDexFileReader) File(java.io.File)

Example 2 with BaseDexFileReader

use of com.googlecode.d2j.reader.BaseDexFileReader in project dex2jar by pxb1988.

the class Dex2jarCmd method doCommandLine.

@Override
protected void doCommandLine() throws Exception {
    if (remainingArgs.length == 0) {
        usage();
        return;
    }
    if ((exceptionFile != null || output != null) && remainingArgs.length != 1) {
        System.err.println("-e/-o can only used with one file");
        return;
    }
    if (debugInfo && reuseReg) {
        System.err.println("-d/-r can not use together");
        return;
    }
    Path currentDir = new File(".").toPath();
    if (output != null) {
        if (Files.exists(output) && !forceOverwrite) {
            System.err.println(output + " exists, use --force to overwrite");
            return;
        }
    } else {
        for (String fileName : remainingArgs) {
            Path file = currentDir.resolve(getBaseName(new File(fileName).toPath()) + "-dex2jar.jar");
            if (Files.exists(file) && !forceOverwrite) {
                System.err.println(file + " exists, use --force to overwrite");
                return;
            }
        }
    }
    for (String fileName : remainingArgs) {
        // long baseTS = System.currentTimeMillis();
        String baseName = getBaseName(new File(fileName).toPath());
        Path file = output == null ? currentDir.resolve(baseName + "-dex2jar.jar") : output;
        System.err.println("dex2jar " + fileName + " -> " + file);
        BaseDexFileReader reader = MultiDexFileReader.open(Files.readAllBytes(new File(fileName).toPath()));
        BaksmaliBaseDexExceptionHandler handler = notHandleException ? null : new BaksmaliBaseDexExceptionHandler();
        Dex2jar.from(reader).withExceptionHandler(handler).reUseReg(reuseReg).topoLogicalSort().skipDebug(!debugInfo).optimizeSynchronized(this.optmizeSynchronized).printIR(printIR).noCode(noCode).skipExceptions(skipExceptions).to(file);
        if (!notHandleException) {
            if (handler.hasException()) {
                Path errorFile = exceptionFile == null ? currentDir.resolve(baseName + "-error.zip") : exceptionFile;
                System.err.println("Detail Error Information in File " + errorFile);
                System.err.println(BaksmaliBaseDexExceptionHandler.REPORT_MESSAGE);
                handler.dump(errorFile, orginalArgs);
            }
        }
    // long endTS = System.currentTimeMillis();
    // System.err.println(String.format("%.2f", (float) (endTS - baseTS) / 1000));
    }
}
Also used : Path(java.nio.file.Path) BaseDexFileReader(com.googlecode.d2j.reader.BaseDexFileReader) File(java.io.File)

Aggregations

BaseDexFileReader (com.googlecode.d2j.reader.BaseDexFileReader)2 File (java.io.File)2 Path (java.nio.file.Path)2 ClassVisitorFactory (com.googlecode.d2j.dex.ClassVisitorFactory)1 ExDex2Asm (com.googlecode.d2j.dex.ExDex2Asm)1 LambadaNameSafeClassAdapter (com.googlecode.d2j.dex.LambadaNameSafeClassAdapter)1 DexClassNode (com.googlecode.d2j.node.DexClassNode)1 DexFileNode (com.googlecode.d2j.node.DexFileNode)1 IOException (java.io.IOException)1 FileSystem (java.nio.file.FileSystem)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 ClassWriter (org.objectweb.asm.ClassWriter)1