Search in sources :

Example 1 with IfCondition

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

the class IfRegionVisitor method simplifyIfCondition.

private static void simplifyIfCondition(IfRegion ifRegion) {
    if (ifRegion.simplifyCondition()) {
        IfCondition condition = ifRegion.getCondition();
        if (condition.getMode() == Mode.NOT) {
            invertIfRegion(ifRegion);
        }
    }
    IContainer elseRegion = ifRegion.getElseRegion();
    if (elseRegion == null || RegionUtils.isEmpty(elseRegion)) {
        return;
    }
    boolean thenIsEmpty = RegionUtils.isEmpty(ifRegion.getThenRegion());
    if (thenIsEmpty || hasSimpleReturnBlock(ifRegion.getThenRegion())) {
        invertIfRegion(ifRegion);
    }
    if (!thenIsEmpty) {
        // move 'if' from then to make 'else if' chain
        if (isIfRegion(ifRegion.getThenRegion()) && !isIfRegion(elseRegion)) {
            invertIfRegion(ifRegion);
        }
    }
}
Also used : IfCondition(jadx.core.dex.regions.conditions.IfCondition) IContainer(jadx.core.dex.nodes.IContainer)

Example 2 with IfCondition

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

the class IfMakerHelper method makeIfInfo.

static IfInfo makeIfInfo(BlockNode ifBlock) {
    IfNode ifNode = (IfNode) ifBlock.getInstructions().get(0);
    IfCondition condition = IfCondition.fromIfNode(ifNode);
    IfInfo info = new IfInfo(condition, ifNode.getThenBlock(), ifNode.getElseBlock());
    info.setIfBlock(ifBlock);
    info.getMergedBlocks().add(ifBlock);
    return info;
}
Also used : IfInfo(jadx.core.dex.regions.conditions.IfInfo) IfNode(jadx.core.dex.instructions.IfNode) IfCondition(jadx.core.dex.regions.conditions.IfCondition)

Example 3 with IfCondition

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

the class TestIfCondition method testSimplify.

@Test
public void testSimplify() {
    // '!(!a || !b)' => 'a && b'
    IfCondition a = makeSimpleCondition();
    IfCondition b = makeSimpleCondition();
    IfCondition c = not(merge(Mode.OR, not(a), not(b)));
    IfCondition simp = simplify(c);
    assertEquals(simp.getMode(), Mode.AND);
    assertEquals(simp.first(), a);
    assertEquals(simp.second(), b);
}
Also used : IfCondition(jadx.core.dex.regions.conditions.IfCondition) Test(org.junit.Test)

Example 4 with IfCondition

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

the class TestIfCondition method testSimplifyNot.

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

Example 5 with IfCondition

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

the class TestIfCondition method testSimplify2.

@Test
public void testSimplify2() {
    // '(!a || !b) && !c' => '!((a && b) || c)'
    IfCondition a = makeSimpleCondition();
    IfCondition b = makeSimpleCondition();
    IfCondition c = makeSimpleCondition();
    IfCondition cond = merge(Mode.AND, merge(Mode.OR, not(a), not(b)), not(c));
    IfCondition simp = simplify(cond);
    assertEquals(simp.getMode(), Mode.NOT);
    IfCondition f = simp.first();
    assertEquals(f.getMode(), Mode.OR);
    assertEquals(f.first().getMode(), Mode.AND);
    assertEquals(f.first().first(), a);
    assertEquals(f.first().second(), b);
    assertEquals(f.second(), c);
}
Also used : IfCondition(jadx.core.dex.regions.conditions.IfCondition) Test(org.junit.Test)

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