Search in sources :

Example 1 with ICodeReader

use of jadx.api.plugins.input.data.ICodeReader in project jadx by skylot.

the class DexInputPluginTest method processFile.

private static void processFile(Path sample) throws IOException {
    System.out.println("Input file: " + sample.toAbsolutePath());
    long start = System.currentTimeMillis();
    List<Path> files = Collections.singletonList(sample);
    try (ILoadResult result = new DexInputPlugin().loadFiles(files)) {
        AtomicInteger count = new AtomicInteger();
        result.visitClasses(cls -> {
            System.out.println();
            System.out.println("Class: " + cls.getType());
            System.out.println("AccessFlags: " + AccessFlags.format(cls.getAccessFlags(), AccessFlagsScope.CLASS));
            System.out.println("SuperType: " + cls.getSuperType());
            System.out.println("Interfaces: " + cls.getInterfacesTypes());
            System.out.println("Attributes: " + cls.getAttributes());
            count.getAndIncrement();
            cls.visitFieldsAndMethods(System.out::println, mth -> {
                System.out.println("---");
                System.out.println(mth);
                ICodeReader codeReader = mth.getCodeReader();
                if (codeReader != null) {
                    codeReader.visitInstructions(insn -> {
                        insn.decode();
                        System.out.println(insn);
                    });
                }
                System.out.println("---");
                System.out.println(mth.disassembleMethod());
                System.out.println("---");
            });
            System.out.println("----");
            System.out.println(cls.getDisassembledCode());
            System.out.println("----");
        });
        assertThat(count.get()).isGreaterThan(0);
    }
    System.out.println("Time: " + (System.currentTimeMillis() - start) + "ms");
}
Also used : Path(java.nio.file.Path) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ILoadResult(jadx.api.plugins.input.data.ILoadResult) ICodeReader(jadx.api.plugins.input.data.ICodeReader)

Example 2 with ICodeReader

use of jadx.api.plugins.input.data.ICodeReader in project jadx by skylot.

the class Smali method writeMethod.

private void writeMethod(SmaliWriter smali, MethodNode methodNode, IMethodData mth, LineInfo line) {
    if (insnDecoder == null) {
        insnDecoder = new SmaliInsnDecoder(methodNode);
    }
    smali.startLine().startLine(".method ");
    writeMethodDef(smali, mth, line);
    ICodeReader codeReader = mth.getCodeReader();
    if (codeReader != null) {
        line.smaliMthNode.setParamRegStart(getParamStartRegNum(mth));
        line.smaliMthNode.setRegCount(codeReader.getRegistersCount());
        Map<Long, InsnNode> nodes = new HashMap<>(codeReader.getUnitsCount() / 2);
        line.smaliMthNode.setInsnNodes(nodes, codeReader.getUnitsCount());
        line.smaliMthNode.initRegInfoList(codeReader.getRegistersCount(), codeReader.getUnitsCount());
        smali.incIndent();
        smali.startLine(".registers ").add("" + codeReader.getRegistersCount()).startLine();
        writeTries(codeReader, line);
        if (formatMthParamInfo(mth, smali, codeReader, line)) {
            smali.startLine();
        }
        smali.startLine();
        if (codeReader.getDebugInfo() != null) {
            formatDbgInfo(codeReader.getDebugInfo(), line);
        }
        codeReader.visitInstructions(insn -> {
            InsnNode node = decodeInsn(insn, line);
            nodes.put((long) insn.getOffset(), node);
        });
        line.write(smali);
        insnMap.put(methodNode.getMethodInfo().getRawFullId(), line.smaliMthNode);
        smali.decIndent();
    }
    smali.startLine(".end method");
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ICodeReader(jadx.api.plugins.input.data.ICodeReader)

Example 3 with ICodeReader

use of jadx.api.plugins.input.data.ICodeReader in project jadx by skylot.

the class SmaliPrinter method printMethod.

public static String printMethod(DexMethodData mth) {
    SmaliCodeWriter codeWriter = new SmaliCodeWriter();
    codeWriter.startLine(".method ");
    codeWriter.add(AccessFlags.format(mth.getAccessFlags(), METHOD));
    DexMethodRef methodRef = mth.getMethodRef();
    methodRef.load();
    codeWriter.add(methodRef.getName());
    codeWriter.add('(').addArgs(methodRef.getArgTypes()).add(')');
    codeWriter.add(methodRef.getReturnType());
    codeWriter.incIndent();
    ICodeReader codeReader = mth.getCodeReader();
    if (codeReader != null) {
        codeWriter.startLine(".registers ").add(codeReader.getRegistersCount());
        SmaliInsnFormat insnFormat = SmaliInsnFormat.getInstance();
        InsnFormatterInfo formatterInfo = new InsnFormatterInfo(codeWriter, mth);
        codeReader.visitInstructions(insn -> {
            codeWriter.startLine();
            formatterInfo.setInsn(insn);
            insnFormat.format(formatterInfo);
        });
        codeWriter.decIndent();
    }
    codeWriter.startLine(".end method");
    return codeWriter.getCode();
}
Also used : DexMethodRef(jadx.plugins.input.dex.sections.DexMethodRef) ICodeReader(jadx.api.plugins.input.data.ICodeReader)

Example 4 with ICodeReader

use of jadx.api.plugins.input.data.ICodeReader in project jadx by skylot.

the class UsageInfoVisitor method processInstructions.

private static void processInstructions(MethodNode mth, UsageInfo usageInfo) {
    if (mth.isNoCode()) {
        return;
    }
    ICodeReader codeReader = mth.getCodeReader();
    if (codeReader == null) {
        return;
    }
    RootNode root = mth.root();
    codeReader.visitInstructions(insnData -> {
        try {
            processInsn(root, mth, insnData, usageInfo);
        } catch (Exception e) {
            mth.addError("Dependency scan failed at insn: " + insnData, e);
        }
    });
}
Also used : RootNode(jadx.core.dex.nodes.RootNode) ICodeReader(jadx.api.plugins.input.data.ICodeReader)

Aggregations

ICodeReader (jadx.api.plugins.input.data.ICodeReader)4 ILoadResult (jadx.api.plugins.input.data.ILoadResult)1 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)1 InsnNode (jadx.core.dex.nodes.InsnNode)1 RootNode (jadx.core.dex.nodes.RootNode)1 DexMethodRef (jadx.plugins.input.dex.sections.DexMethodRef)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1