Search in sources :

Example 1 with FALLBACK_MODE

use of jadx.core.codegen.MethodGen.FallbackOption.FALLBACK_MODE in project jadx by skylot.

the class MethodGen method addFallbackMethodCode.

public void addFallbackMethodCode(ICodeWriter code, FallbackOption fallbackOption) {
    if (fallbackOption != FALLBACK_MODE) {
        // preserve error before unload
        List<JadxError> errors = mth.getAll(AType.JADX_ERROR);
        try {
            // load original instructions
            mth.unload();
            mth.load();
            for (IDexTreeVisitor visitor : Jadx.getFallbackPassesList()) {
                DepthTraversal.visit(visitor, mth);
            }
            errors.forEach(err -> mth.addAttr(AType.JADX_ERROR, err));
        } catch (Exception e) {
            LOG.error("Error reload instructions in fallback mode:", e);
            code.startLine("// Can't load method instructions: " + e.getMessage());
            return;
        } finally {
            errors.forEach(err -> mth.addAttr(AType.JADX_ERROR, err));
        }
    }
    InsnNode[] insnArr = mth.getInstructions();
    if (insnArr == null) {
        code.startLine("// Can't load method instructions.");
        return;
    }
    if (fallbackOption == COMMENTED_DUMP && mth.getCommentsLevel() != CommentsLevel.DEBUG) {
        long insnCountEstimate = Stream.of(insnArr).filter(Objects::nonNull).filter(insn -> insn.getType() != InsnType.NOP).count();
        if (insnCountEstimate > 100) {
            code.incIndent();
            code.startLine("Method dump skipped, instructions count: " + insnArr.length);
            if (code.isMetadataSupported()) {
                code.startLine("To view this dump change 'Code comments level' option to 'DEBUG'");
            } else {
                code.startLine("To view this dump add '--comments-level debug' option");
            }
            code.decIndent();
            return;
        }
    }
    code.incIndent();
    if (mth.getThisArg() != null) {
        code.startLine(nameGen.useArg(mth.getThisArg())).add(" = this;");
    }
    addFallbackInsns(code, mth, insnArr, fallbackOption);
    code.decIndent();
}
Also used : IDexTreeVisitor(jadx.core.dex.visitors.IDexTreeVisitor) ArgType(jadx.core.dex.instructions.args.ArgType) CodeVar(jadx.core.dex.instructions.args.CodeVar) IDexTreeVisitor(jadx.core.dex.visitors.IDexTreeVisitor) JumpInfo(jadx.core.dex.attributes.nodes.JumpInfo) MethodNode(jadx.core.dex.nodes.MethodNode) Consts(jadx.core.Consts) AType(jadx.core.dex.attributes.AType) AFlag(jadx.core.dex.attributes.AFlag) LoggerFactory(org.slf4j.LoggerFactory) InsnType(jadx.core.dex.instructions.InsnType) CatchAttr(jadx.core.dex.trycatch.CatchAttr) JadxOverflowException(jadx.core.utils.exceptions.JadxOverflowException) AccessInfo(jadx.core.dex.info.AccessInfo) InsnCodeOffset(jadx.api.data.annotations.InsnCodeOffset) IfNode(jadx.core.dex.instructions.IfNode) InsnUtils(jadx.core.utils.InsnUtils) CodeGenUtils(jadx.core.utils.CodeGenUtils) CommentsLevel(jadx.api.CommentsLevel) InsnNode(jadx.core.dex.nodes.InsnNode) SSAVar(jadx.core.dex.instructions.args.SSAVar) VarDeclareRef(jadx.api.data.annotations.VarDeclareRef) MethodOverrideAttr(jadx.core.dex.attributes.nodes.MethodOverrideAttr) CodegenException(jadx.core.utils.exceptions.CodegenException) Jadx(jadx.core.Jadx) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) JadxAttrType(jadx.api.plugins.input.data.attributes.JadxAttrType) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DepthTraversal(jadx.core.dex.visitors.DepthTraversal) BLOCK_DUMP(jadx.core.codegen.MethodGen.FallbackOption.BLOCK_DUMP) AnnotationMethodParamsAttr(jadx.api.plugins.input.data.attributes.types.AnnotationMethodParamsAttr) ConstStringNode(jadx.core.dex.instructions.ConstStringNode) FALLBACK_MODE(jadx.core.codegen.MethodGen.FallbackOption.FALLBACK_MODE) Objects(java.util.Objects) JadxError(jadx.core.dex.attributes.nodes.JadxError) List(java.util.List) Stream(java.util.stream.Stream) AccessFlags(jadx.api.plugins.input.data.AccessFlags) COMMENTED_DUMP(jadx.core.codegen.MethodGen.FallbackOption.COMMENTED_DUMP) Collections(java.util.Collections) ICodeWriter(jadx.api.ICodeWriter) Utils(jadx.core.utils.Utils) InsnNode(jadx.core.dex.nodes.InsnNode) Objects(java.util.Objects) JadxError(jadx.core.dex.attributes.nodes.JadxError) JadxOverflowException(jadx.core.utils.exceptions.JadxOverflowException) CodegenException(jadx.core.utils.exceptions.CodegenException)

Aggregations

CommentsLevel (jadx.api.CommentsLevel)1 ICodeWriter (jadx.api.ICodeWriter)1 InsnCodeOffset (jadx.api.data.annotations.InsnCodeOffset)1 VarDeclareRef (jadx.api.data.annotations.VarDeclareRef)1 AccessFlags (jadx.api.plugins.input.data.AccessFlags)1 EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)1 JadxAttrType (jadx.api.plugins.input.data.attributes.JadxAttrType)1 AnnotationMethodParamsAttr (jadx.api.plugins.input.data.attributes.types.AnnotationMethodParamsAttr)1 Consts (jadx.core.Consts)1 Jadx (jadx.core.Jadx)1 BLOCK_DUMP (jadx.core.codegen.MethodGen.FallbackOption.BLOCK_DUMP)1 COMMENTED_DUMP (jadx.core.codegen.MethodGen.FallbackOption.COMMENTED_DUMP)1 FALLBACK_MODE (jadx.core.codegen.MethodGen.FallbackOption.FALLBACK_MODE)1 AFlag (jadx.core.dex.attributes.AFlag)1 AType (jadx.core.dex.attributes.AType)1 JadxError (jadx.core.dex.attributes.nodes.JadxError)1 JumpInfo (jadx.core.dex.attributes.nodes.JumpInfo)1 MethodOverrideAttr (jadx.core.dex.attributes.nodes.MethodOverrideAttr)1 AccessInfo (jadx.core.dex.info.AccessInfo)1 ConstStringNode (jadx.core.dex.instructions.ConstStringNode)1