Search in sources :

Example 1 with DexClassNode

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

the class BaksmaliDexFileVisitor method visit.

@Override
public DexClassVisitor visit(int access_flags, String className, String superClass, String[] interfaceNames) {
    return new DexClassNode(access_flags, className, superClass, interfaceNames) {

        @Override
        public void visitEnd() {
            super.visitEnd();
            Path smaliFile = dir.resolve(rebuildFileName(className.substring(1, className.length() - 1)) + ".smali");
            try {
                Path parent = smaliFile.getParent();
                if (parent != null && !Files.exists(parent)) {
                    Files.createDirectories(parent);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try (BufferedWriter writer = Files.newBufferedWriter(smaliFile, StandardCharsets.UTF_8)) {
                BaksmaliDumpOut out = new BaksmaliDumpOut(writer);
                bs.baksmaliClass(this, out);
                writer.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    };
}
Also used : Path(java.nio.file.Path) DexClassNode(com.googlecode.d2j.node.DexClassNode) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 2 with DexClassNode

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

the class DexWaveTest method test0.

private void test0(DexWeaver iw, String prefix) throws IOException, RecognitionException {
    DexClassNode before = Smali.smaliFile2Node(prefix + "-before.smali", getClass().getResourceAsStream("/weave/smali/" + prefix + "-before.smali"));
    DexClassNode expectedAfter = Smali.smaliFile2Node(prefix + "-after.smali", getClass().getResourceAsStream("/weave/smali/" + prefix + "-after.smali"));
    DexFileNode dfn = new DexFileNode();
    before.accept(iw.wrap(dfn));
    assertEqual(expectedAfter, dfn.clzs.get(0));
    DexClassNode expectedGen = Smali.smaliFile2Node(prefix + "-gen.j", getClass().getResourceAsStream("/weave/smali/" + prefix + "-gen.smali"));
    dfn.clzs.clear();
    iw.buildInvocationClz(dfn);
    assertEqual(expectedGen, dfn.clzs.get(0));
}
Also used : DexClassNode(com.googlecode.d2j.node.DexClassNode) DexFileNode(com.googlecode.d2j.node.DexFileNode)

Example 3 with DexClassNode

use of com.googlecode.d2j.node.DexClassNode 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 4 with DexClassNode

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

the class DexTranslatorRunner method methodInvoker.

@Override
protected Statement methodInvoker(final FrameworkMethod method, final Object test) {
    if (method.getMethod().getParameterTypes().length > 0) {
        return new Statement() {

            @Override
            public void evaluate() throws Throwable {
                // 1.invoke the method
                DexClassNode clzNode = new DexClassNode(DexConstants.ACC_PUBLIC, "La;", "Ljava/lang/Object;", null);
                if (method.isStatic()) {
                    method.invokeExplosively(null, clzNode);
                } else {
                    method.invokeExplosively(test, clzNode);
                }
                // 2. convert and verify
                TestUtils.translateAndCheck(clzNode);
            }
        };
    } else {
        return super.methodInvoker(method, test);
    }
}
Also used : DexClassNode(com.googlecode.d2j.node.DexClassNode) Statement(org.junit.runners.model.Statement)

Example 5 with DexClassNode

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

the class TestUtils method testDexASMifier.

public static byte[] testDexASMifier(Class<?> clz, String methodName, String generateClassName) throws Exception {
    DexClassNode clzNode = new DexClassNode(DexConstants.ACC_PUBLIC, "L" + generateClassName + ";", "Ljava/lang/Object;", null);
    Method m = clz.getMethod(methodName, DexClassVisitor.class);
    if (m == null) {
        throw new java.lang.NoSuchMethodException(methodName);
    }
    m.setAccessible(true);
    if (Modifier.isStatic(m.getModifiers())) {
        m.invoke(null, clzNode);
    } else {
        m.invoke(clz.newInstance(), clzNode);
    }
    return translateAndCheck(clzNode);
}
Also used : DexClassNode(com.googlecode.d2j.node.DexClassNode) Method(java.lang.reflect.Method)

Aggregations

DexClassNode (com.googlecode.d2j.node.DexClassNode)8 DexFileNode (com.googlecode.d2j.node.DexFileNode)4 IOException (java.io.IOException)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 BaseDexFileReader (com.googlecode.d2j.reader.BaseDexFileReader)1 DexFileReader (com.googlecode.d2j.reader.DexFileReader)1 BaksmaliDumpOut (com.googlecode.d2j.smali.BaksmaliDumpOut)1 BaksmaliDumper (com.googlecode.d2j.smali.BaksmaliDumper)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 Method (java.lang.reflect.Method)1 FileSystem (java.nio.file.FileSystem)1 HashMap (java.util.HashMap)1 Opcodes (org.jf.dexlib2.Opcodes)1 DexBackedClassDef (org.jf.dexlib2.dexbacked.DexBackedClassDef)1 DexBackedDexFile (org.jf.dexlib2.dexbacked.DexBackedDexFile)1 Test (org.junit.Test)1