Search in sources :

Example 6 with IfCondition

use of jadx.core.dex.regions.conditions.IfCondition in project jadx by skylot.

the class TestIfCondition method testSimplifyNot2.

@Test
public void testSimplifyNot2() {
    // !(!a) => a
    IfCondition a = not(makeNegCondition());
    assertEquals(simplify(a), a);
}
Also used : IfCondition(jadx.core.dex.regions.conditions.IfCondition) Test(org.junit.Test)

Example 7 with IfCondition

use of jadx.core.dex.regions.conditions.IfCondition in project jadx by skylot.

the class TestIfCondition method testMerge.

@Test
public void testMerge() {
    IfCondition a = makeSimpleCondition();
    IfCondition b = makeSimpleCondition();
    IfCondition c = merge(Mode.OR, a, b);
    assertEquals(c.getMode(), Mode.OR);
    assertEquals(c.first(), a);
    assertEquals(c.second(), b);
}
Also used : IfCondition(jadx.core.dex.regions.conditions.IfCondition) Test(org.junit.Test)

Example 8 with IfCondition

use of jadx.core.dex.regions.conditions.IfCondition in project jadx by skylot.

the class TestIfCondition method testNormalize.

@Test
public void testNormalize() {
    // 'a != false' => 'a == true'
    InsnArg a = mockArg();
    IfCondition c = makeCondition(IfOp.NE, a, LiteralArg.FALSE);
    IfCondition simp = simplify(c);
    assertEquals(simp.getMode(), Mode.COMPARE);
    Compare compare = simp.getCompare();
    assertEquals(compare.getA(), a);
    assertEquals(compare.getB(), LiteralArg.TRUE);
}
Also used : InsnArg(jadx.core.dex.instructions.args.InsnArg) Compare(jadx.core.dex.regions.conditions.Compare) IfCondition(jadx.core.dex.regions.conditions.IfCondition) Test(org.junit.Test)

Example 9 with IfCondition

use of jadx.core.dex.regions.conditions.IfCondition in project jadx by skylot.

the class RegionGen method makeLoop.

private CodeWriter makeLoop(LoopRegion region, CodeWriter code) throws CodegenException {
    BlockNode header = region.getHeader();
    if (header != null) {
        List<InsnNode> headerInsns = header.getInstructions();
        if (headerInsns.size() > 1) {
            ErrorsCounter.methodError(mth, "Found not inlined instructions from loop header");
            int last = headerInsns.size() - 1;
            for (int i = 0; i < last; i++) {
                InsnNode insn = headerInsns.get(i);
                makeInsn(insn, code);
            }
        }
    }
    LoopLabelAttr labelAttr = region.getInfo().getStart().get(AType.LOOP_LABEL);
    if (labelAttr != null) {
        code.startLine(mgen.getNameGen().getLoopLabel(labelAttr)).add(':');
    }
    IfCondition condition = region.getCondition();
    if (condition == null) {
        // infinite loop
        code.startLine("while (true) {");
        makeRegionIndent(code, region.getBody());
        code.startLine('}');
        return code;
    }
    ConditionGen conditionGen = new ConditionGen(this);
    LoopType type = region.getType();
    if (type != null) {
        if (type instanceof ForLoop) {
            ForLoop forLoop = (ForLoop) type;
            code.startLine("for (");
            makeInsn(forLoop.getInitInsn(), code, Flags.INLINE);
            code.add("; ");
            conditionGen.add(code, condition);
            code.add("; ");
            makeInsn(forLoop.getIncrInsn(), code, Flags.INLINE);
            code.add(") {");
            makeRegionIndent(code, region.getBody());
            code.startLine('}');
            return code;
        }
        if (type instanceof ForEachLoop) {
            ForEachLoop forEachLoop = (ForEachLoop) type;
            code.startLine("for (");
            declareVar(code, forEachLoop.getVarArg());
            code.add(" : ");
            addArg(code, forEachLoop.getIterableArg(), false);
            code.add(") {");
            makeRegionIndent(code, region.getBody());
            code.startLine('}');
            return code;
        }
        throw new JadxRuntimeException("Unknown loop type: " + type.getClass());
    }
    if (region.isConditionAtEnd()) {
        code.startLine("do {");
        makeRegionIndent(code, region.getBody());
        code.startLine("} while (");
        conditionGen.add(code, condition);
        code.add(");");
    } else {
        code.startLine("while (");
        conditionGen.add(code, condition);
        code.add(") {");
        makeRegionIndent(code, region.getBody());
        code.startLine('}');
    }
    return code;
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode) ForLoop(jadx.core.dex.regions.loops.ForLoop) LoopLabelAttr(jadx.core.dex.attributes.nodes.LoopLabelAttr) LoopType(jadx.core.dex.regions.loops.LoopType) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IfCondition(jadx.core.dex.regions.conditions.IfCondition) ForEachLoop(jadx.core.dex.regions.loops.ForEachLoop)

Example 10 with IfCondition

use of jadx.core.dex.regions.conditions.IfCondition in project jadx by skylot.

the class IfMakerHelper method mergeTernaryConditions.

private static IfInfo mergeTernaryConditions(IfInfo currentIf, IfInfo nextThen, IfInfo nextElse) {
    IfCondition newCondition = IfCondition.ternary(currentIf.getCondition(), nextThen.getCondition(), nextElse.getCondition());
    IfInfo result = new IfInfo(newCondition, nextThen.getThenBlock(), nextThen.getElseBlock());
    result.setIfBlock(currentIf.getIfBlock());
    result.merge(currentIf, nextThen, nextElse);
    confirmMerge(result);
    return result;
}
Also used : IfInfo(jadx.core.dex.regions.conditions.IfInfo) IfCondition(jadx.core.dex.regions.conditions.IfCondition)

Aggregations

IfCondition (jadx.core.dex.regions.conditions.IfCondition)11 Test (org.junit.Test)6 IfInfo (jadx.core.dex.regions.conditions.IfInfo)3 BlockNode (jadx.core.dex.nodes.BlockNode)2 LoopLabelAttr (jadx.core.dex.attributes.nodes.LoopLabelAttr)1 IfNode (jadx.core.dex.instructions.IfNode)1 InsnArg (jadx.core.dex.instructions.args.InsnArg)1 IContainer (jadx.core.dex.nodes.IContainer)1 InsnNode (jadx.core.dex.nodes.InsnNode)1 Compare (jadx.core.dex.regions.conditions.Compare)1 Mode (jadx.core.dex.regions.conditions.IfCondition.Mode)1 ForEachLoop (jadx.core.dex.regions.loops.ForEachLoop)1 ForLoop (jadx.core.dex.regions.loops.ForLoop)1 LoopType (jadx.core.dex.regions.loops.LoopType)1 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)1