Search in sources :

Example 16 with IContainer

use of jadx.core.dex.nodes.IContainer in project jadx by skylot.

the class RegionGen method makeTryCatch.

private void makeTryCatch(TryCatchRegion region, CodeWriter code) throws CodegenException {
    code.startLine("try {");
    makeRegionIndent(code, region.getTryRegion());
    // TODO: move search of 'allHandler' to 'TryCatchRegion'
    ExceptionHandler allHandler = null;
    for (Map.Entry<ExceptionHandler, IContainer> entry : region.getCatchRegions().entrySet()) {
        ExceptionHandler handler = entry.getKey();
        if (handler.isCatchAll()) {
            if (allHandler != null) {
                LOG.warn("Several 'all' handlers in try/catch block in {}", mth);
            }
            allHandler = handler;
        } else {
            makeCatchBlock(code, handler);
        }
    }
    if (allHandler != null) {
        makeCatchBlock(code, allHandler);
    }
    IContainer finallyRegion = region.getFinallyRegion();
    if (finallyRegion != null) {
        code.startLine("} finally {");
        makeRegionIndent(code, finallyRegion);
    }
    code.startLine('}');
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) IContainer(jadx.core.dex.nodes.IContainer) Map(java.util.Map)

Example 17 with IContainer

use of jadx.core.dex.nodes.IContainer in project jadx by skylot.

the class RegionGen method makeSwitch.

private CodeWriter makeSwitch(SwitchRegion sw, CodeWriter code) throws CodegenException {
    SwitchNode insn = (SwitchNode) sw.getHeader().getInstructions().get(0);
    InsnArg arg = insn.getArg(0);
    code.startLine("switch (");
    addArg(code, arg, false);
    code.add(") {");
    code.incIndent();
    int size = sw.getKeys().size();
    for (int i = 0; i < size; i++) {
        List<Object> keys = sw.getKeys().get(i);
        IContainer c = sw.getCases().get(i);
        for (Object k : keys) {
            code.startLine("case ");
            if (k instanceof FieldNode) {
                FieldNode fn = (FieldNode) k;
                if (fn.getParentClass().isEnum()) {
                    code.add(fn.getAlias());
                } else {
                    staticField(code, fn.getFieldInfo());
                    // print original value, sometimes replace with incorrect field
                    FieldInitAttr valueAttr = fn.get(AType.FIELD_INIT);
                    if (valueAttr != null && valueAttr.getValue() != null) {
                        code.add(" /*").add(valueAttr.getValue().toString()).add("*/");
                    }
                }
            } else if (k instanceof Integer) {
                code.add(TypeGen.literalToString((Integer) k, arg.getType(), mth));
            } else {
                throw new JadxRuntimeException("Unexpected key in switch: " + (k != null ? k.getClass() : null));
            }
            code.add(':');
        }
        makeRegionIndent(code, c);
    }
    if (sw.getDefaultCase() != null) {
        code.startLine("default:");
        makeRegionIndent(code, sw.getDefaultCase());
    }
    code.decIndent();
    code.startLine('}');
    return code;
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IContainer(jadx.core.dex.nodes.IContainer) SwitchNode(jadx.core.dex.instructions.SwitchNode) FieldInitAttr(jadx.core.dex.nodes.parser.FieldInitAttr)

Example 18 with IContainer

use of jadx.core.dex.nodes.IContainer in project jadx by skylot.

the class RegionGen method makeCatchBlock.

private void makeCatchBlock(CodeWriter code, ExceptionHandler handler) throws CodegenException {
    IContainer region = handler.getHandlerRegion();
    if (region == null) {
        return;
    }
    code.startLine("} catch (");
    InsnArg arg = handler.getArg();
    if (arg instanceof RegisterArg) {
        declareVar(code, (RegisterArg) arg);
    } else if (arg instanceof NamedArg) {
        if (handler.isCatchAll()) {
            code.add("Throwable");
        } else {
            useClass(code, handler.getCatchType());
        }
        code.add(' ');
        code.add(mgen.getNameGen().assignNamedArg((NamedArg) arg));
    }
    code.add(") {");
    makeRegionIndent(code, region);
}
Also used : RegisterArg(jadx.core.dex.instructions.args.RegisterArg) InsnArg(jadx.core.dex.instructions.args.InsnArg) NamedArg(jadx.core.dex.instructions.args.NamedArg) IContainer(jadx.core.dex.nodes.IContainer)

Example 19 with IContainer

use of jadx.core.dex.nodes.IContainer in project jadx by skylot.

the class IfRegion method invert.

public void invert() {
    condition = IfCondition.invert(condition);
    // swap regions
    IContainer tmp = thenRegion;
    thenRegion = elseRegion;
    elseRegion = tmp;
}
Also used : IContainer(jadx.core.dex.nodes.IContainer)

Example 20 with IContainer

use of jadx.core.dex.nodes.IContainer in project jadx by skylot.

the class CleanRegions method process.

public static void process(MethodNode mth) {
    if (mth.isNoCode() || mth.getBasicBlocks().isEmpty()) {
        return;
    }
    IRegionVisitor removeEmptyBlocks = new AbstractRegionVisitor() {

        @Override
        public boolean enterRegion(MethodNode mth, IRegion region) {
            if (!(region instanceof Region)) {
                return true;
            }
            for (Iterator<IContainer> it = region.getSubBlocks().iterator(); it.hasNext(); ) {
                IContainer container = it.next();
                if (container instanceof BlockNode) {
                    BlockNode block = (BlockNode) container;
                    if (block.getInstructions().isEmpty()) {
                        try {
                            it.remove();
                        } catch (UnsupportedOperationException e) {
                            LOG.warn("Can't remove block: {} from: {}, mth: {}", block, region, mth);
                        }
                    }
                }
            }
            return true;
        }
    };
    DepthRegionTraversal.traverse(mth, removeEmptyBlocks);
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) MethodNode(jadx.core.dex.nodes.MethodNode) IRegion(jadx.core.dex.nodes.IRegion) Region(jadx.core.dex.regions.Region) IContainer(jadx.core.dex.nodes.IContainer) IRegion(jadx.core.dex.nodes.IRegion)

Aggregations

IContainer (jadx.core.dex.nodes.IContainer)22 IRegion (jadx.core.dex.nodes.IRegion)11 InsnNode (jadx.core.dex.nodes.InsnNode)6 Region (jadx.core.dex.regions.Region)6 IBlock (jadx.core.dex.nodes.IBlock)5 BlockNode (jadx.core.dex.nodes.BlockNode)4 IBranchRegion (jadx.core.dex.nodes.IBranchRegion)4 LoopRegion (jadx.core.dex.regions.loops.LoopRegion)4 ExceptionHandler (jadx.core.dex.trycatch.ExceptionHandler)4 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)3 SwitchRegion (jadx.core.dex.regions.SwitchRegion)3 SynchronizedRegion (jadx.core.dex.regions.SynchronizedRegion)3 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)3 InsnArg (jadx.core.dex.instructions.args.InsnArg)2 IfRegion (jadx.core.dex.regions.conditions.IfRegion)2 TryCatchBlock (jadx.core.dex.trycatch.TryCatchBlock)2 HashSet (java.util.HashSet)2 EdgeInsnAttr (jadx.core.dex.attributes.nodes.EdgeInsnAttr)1 InsnType (jadx.core.dex.instructions.InsnType)1 PhiInsn (jadx.core.dex.instructions.PhiInsn)1