Search in sources :

Example 1 with SimpleCodeWriter

use of jadx.api.impl.SimpleCodeWriter in project jadx by skylot.

the class CheckRegions method getBlockInsnStr.

private static String getBlockInsnStr(MethodNode mth, IBlock block) {
    ICodeWriter code = new SimpleCodeWriter();
    code.incIndent();
    code.newLine();
    MethodGen mg = MethodGen.getFallbackMethodGen(mth);
    InsnGen ig = new InsnGen(mg, true);
    for (InsnNode insn : block.getInstructions()) {
        try {
            ig.makeInsn(insn, code);
        } catch (CodegenException e) {
        // ignore
        }
    }
    code.newLine();
    return code.getCodeStr();
}
Also used : InsnNode(jadx.core.dex.nodes.InsnNode) InsnGen(jadx.core.codegen.InsnGen) CodegenException(jadx.core.utils.exceptions.CodegenException) SimpleCodeWriter(jadx.api.impl.SimpleCodeWriter) ICodeWriter(jadx.api.ICodeWriter) MethodGen(jadx.core.codegen.MethodGen)

Example 2 with SimpleCodeWriter

use of jadx.api.impl.SimpleCodeWriter in project jadx by skylot.

the class JsonCodeGen method addFields.

private void addFields(ClassNode cls, JsonClass jsonCls, ClassGen classGen) {
    jsonCls.setFields(new ArrayList<>());
    for (FieldNode field : cls.getFields()) {
        if (field.contains(AFlag.DONT_GENERATE)) {
            continue;
        }
        JsonField jsonField = new JsonField();
        jsonField.setName(field.getName());
        if (field.getFieldInfo().hasAlias()) {
            jsonField.setAlias(field.getAlias());
        }
        ICodeWriter cw = new SimpleCodeWriter();
        classGen.addField(cw, field);
        jsonField.setDeclaration(cw.getCodeStr());
        jsonField.setAccessFlags(field.getAccessFlags().rawValue());
        jsonCls.getFields().add(jsonField);
    }
}
Also used : JsonField(jadx.core.codegen.json.cls.JsonField) FieldNode(jadx.core.dex.nodes.FieldNode) SimpleCodeWriter(jadx.api.impl.SimpleCodeWriter) ICodeWriter(jadx.api.ICodeWriter)

Example 3 with SimpleCodeWriter

use of jadx.api.impl.SimpleCodeWriter in project jadx by skylot.

the class ResXmlGen method makeResourcesXml.

public List<ResContainer> makeResourcesXml() {
    Map<String, ICodeWriter> contMap = new HashMap<>();
    for (ResourceEntry ri : resStorage.getResources()) {
        if (SKIP_RES_TYPES.contains(ri.getTypeName())) {
            continue;
        }
        String fn = getFileName(ri);
        ICodeWriter cw = contMap.get(fn);
        if (cw == null) {
            cw = new SimpleCodeWriter();
            cw.add("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            cw.startLine("<resources>");
            cw.incIndent();
            contMap.put(fn, cw);
        }
        addValue(cw, ri);
    }
    List<ResContainer> files = new ArrayList<>(contMap.size());
    for (Map.Entry<String, ICodeWriter> entry : contMap.entrySet()) {
        String fileName = entry.getKey();
        ICodeWriter content = entry.getValue();
        content.decIndent();
        content.startLine("</resources>");
        ICodeInfo codeInfo = content.finish();
        files.add(ResContainer.textResource(fileName, codeInfo));
    }
    Collections.sort(files);
    return files;
}
Also used : HashMap(java.util.HashMap) ResourceEntry(jadx.core.xmlgen.entry.ResourceEntry) SimpleCodeWriter(jadx.api.impl.SimpleCodeWriter) ArrayList(java.util.ArrayList) ICodeInfo(jadx.api.ICodeInfo) ICodeWriter(jadx.api.ICodeWriter) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with SimpleCodeWriter

use of jadx.api.impl.SimpleCodeWriter in project jadx by skylot.

the class JsonCodeGen method processCls.

private JsonClass processCls(ClassNode cls, @Nullable ClassGen parentCodeGen) {
    ClassGen classGen;
    if (parentCodeGen == null) {
        classGen = new ClassGen(cls, args);
    } else {
        classGen = new ClassGen(cls, parentCodeGen);
    }
    ClassInfo classInfo = cls.getClassInfo();
    JsonClass jsonCls = new JsonClass();
    jsonCls.setPkg(classInfo.getAliasPkg());
    jsonCls.setDex(cls.getInputFileName());
    jsonCls.setName(classInfo.getFullName());
    if (classInfo.hasAlias()) {
        jsonCls.setAlias(classInfo.getAliasFullName());
    }
    jsonCls.setType(getClassTypeStr(cls));
    jsonCls.setAccessFlags(cls.getAccessFlags().rawValue());
    if (!Objects.equals(cls.getSuperClass(), ArgType.OBJECT)) {
        jsonCls.setSuperClass(getTypeAlias(cls.getSuperClass()));
    }
    if (!cls.getInterfaces().isEmpty()) {
        jsonCls.setInterfaces(Utils.collectionMap(cls.getInterfaces(), this::getTypeAlias));
    }
    ICodeWriter cw = new SimpleCodeWriter();
    CodeGenUtils.addErrorsAndComments(cw, cls);
    classGen.addClassDeclaration(cw);
    jsonCls.setDeclaration(cw.getCodeStr());
    addFields(cls, jsonCls, classGen);
    addMethods(cls, jsonCls, classGen);
    addInnerClasses(cls, jsonCls, classGen);
    if (!cls.getClassInfo().isInner()) {
        List<String> imports = Utils.collectionMap(classGen.getImports(), ClassInfo::getAliasFullName);
        Collections.sort(imports);
        jsonCls.setImports(imports);
    }
    return jsonCls;
}
Also used : SimpleCodeWriter(jadx.api.impl.SimpleCodeWriter) ClassGen(jadx.core.codegen.ClassGen) JsonClass(jadx.core.codegen.json.cls.JsonClass) ICodeWriter(jadx.api.ICodeWriter) ClassInfo(jadx.core.dex.info.ClassInfo)

Example 5 with SimpleCodeWriter

use of jadx.api.impl.SimpleCodeWriter in project jadx by skylot.

the class JadxCLI method processAndSave.

private static int processAndSave(JadxCLIArgs cliArgs) {
    LogHelper.initLogLevel(cliArgs);
    LogHelper.setLogLevelsForLoadingStage();
    JadxArgs jadxArgs = cliArgs.toJadxArgs();
    jadxArgs.setCodeCache(new NoOpCodeCache());
    jadxArgs.setCodeWriterProvider(SimpleCodeWriter::new);
    try (JadxDecompiler jadx = new JadxDecompiler(jadxArgs)) {
        jadx.load();
        if (checkForErrors(jadx)) {
            return 1;
        }
        LogHelper.setLogLevelsForDecompileStage();
        if (!SingleClassMode.process(jadx, cliArgs)) {
            save(jadx);
        }
        int errorsCount = jadx.getErrorsCount();
        if (errorsCount != 0) {
            jadx.printErrorsReport();
            LOG.error("finished with errors, count: {}", errorsCount);
        } else {
            LOG.info("done");
        }
    }
    return 0;
}
Also used : JadxDecompiler(jadx.api.JadxDecompiler) SimpleCodeWriter(jadx.api.impl.SimpleCodeWriter) JadxArgs(jadx.api.JadxArgs) NoOpCodeCache(jadx.api.impl.NoOpCodeCache)

Aggregations

SimpleCodeWriter (jadx.api.impl.SimpleCodeWriter)7 ICodeWriter (jadx.api.ICodeWriter)6 InsnGen (jadx.core.codegen.InsnGen)2 MethodGen (jadx.core.codegen.MethodGen)2 InsnNode (jadx.core.dex.nodes.InsnNode)2 CodegenException (jadx.core.utils.exceptions.CodegenException)2 Map (java.util.Map)2 ICodeInfo (jadx.api.ICodeInfo)1 JadxArgs (jadx.api.JadxArgs)1 JadxDecompiler (jadx.api.JadxDecompiler)1 NoOpCodeCache (jadx.api.impl.NoOpCodeCache)1 ClassGen (jadx.core.codegen.ClassGen)1 ConditionGen (jadx.core.codegen.ConditionGen)1 JsonClass (jadx.core.codegen.json.cls.JsonClass)1 JsonField (jadx.core.codegen.json.cls.JsonField)1 AType (jadx.core.dex.attributes.AType)1 IAttributeNode (jadx.core.dex.attributes.IAttributeNode)1 MethodOverrideAttr (jadx.core.dex.attributes.nodes.MethodOverrideAttr)1 ClassInfo (jadx.core.dex.info.ClassInfo)1 BlockNode (jadx.core.dex.nodes.BlockNode)1