■ ■ ■ ■ ■ ■
jadx-core/src/main/java/jadx/core/dex/visitors/regions/IfRegionVisitor.java
| 1 | + | package jadx.core.dex.visitors.regions; |
| 2 | + | |
| 3 | + | import jadx.core.dex.attributes.AttributeFlag; |
| 4 | + | import jadx.core.dex.instructions.args.ArgType; |
| 5 | + | import jadx.core.dex.nodes.IBlock; |
| 6 | + | import jadx.core.dex.nodes.IContainer; |
| 7 | + | import jadx.core.dex.nodes.IRegion; |
| 8 | + | import jadx.core.dex.nodes.MethodNode; |
| 9 | + | import jadx.core.dex.regions.IfCondition; |
| 10 | + | import jadx.core.dex.regions.IfRegion; |
| 11 | + | import jadx.core.dex.regions.Region; |
| 12 | + | import jadx.core.dex.visitors.AbstractVisitor; |
| 13 | + | import jadx.core.utils.RegionUtils; |
| 14 | + | |
| 15 | + | import java.util.List; |
| 16 | + | |
| 17 | + | public class IfRegionVisitor extends AbstractVisitor implements IRegionVisitor, IRegionIterativeVisitor { |
| 18 | + | |
| 19 | + | @Override |
| 20 | + | public void visit(MethodNode mth) { |
| 21 | + | DepthRegionTraversal.traverseAll(mth, this); |
| 22 | + | DepthRegionTraversal.traverseAllIterative(mth, this); |
| 23 | + | } |
| 24 | + | |
| 25 | + | @Override |
| 26 | + | public void enterRegion(MethodNode mth, IRegion region) { |
| 27 | + | if (region instanceof IfRegion) { |
| 28 | + | processIfRegion(mth, (IfRegion) region); |
| 29 | + | } |
| 30 | + | } |
| 31 | + | |
| 32 | + | @Override |
| 33 | + | public boolean visitRegion(MethodNode mth, IRegion region) { |
| 34 | + | if (region instanceof IfRegion) { |
| 35 | + | return removeRedundantElseBlock((IfRegion) region); |
| 36 | + | } |
| 37 | + | return false; |
| 38 | + | } |
| 39 | + | |
| 40 | + | @Override |
| 41 | + | public void processBlock(MethodNode mth, IBlock container) { |
| 42 | + | } |
| 43 | + | |
| 44 | + | @Override |
| 45 | + | public void leaveRegion(MethodNode mth, IRegion region) { |
| 46 | + | } |
| 47 | + | |
| 48 | + | private static void processIfRegion(MethodNode mth, IfRegion ifRegion) { |
| 49 | + | simplifyIfCondition(ifRegion); |
| 50 | + | moveReturnToThenBlock(mth, ifRegion); |
| 51 | + | markElseIfChains(ifRegion); |
| 52 | + | |
| 53 | + | TernaryMod.makeTernaryInsn(mth, ifRegion); |
| 54 | + | } |
| 55 | + | |
| 56 | + | private static void simplifyIfCondition(IfRegion ifRegion) { |
| 57 | + | if (ifRegion.simplifyCondition()) { |
| 58 | + | IfCondition condition = ifRegion.getCondition(); |
| 59 | + | if (condition.getMode() == IfCondition.Mode.NOT) { |
| 60 | + | tryInvertIfRegion(ifRegion); |
| 61 | + | } |
| 62 | + | } |
| 63 | + | } |
| 64 | + | |
| 65 | + | private static void moveReturnToThenBlock(MethodNode mth, IfRegion ifRegion) { |
| 66 | + | if (!mth.getReturnType().equals(ArgType.VOID) |
| 67 | + | && hasSimpleReturnBlock(ifRegion.getElseRegion()) |
| 68 | + | && !hasSimpleReturnBlock(ifRegion.getThenRegion())) { |
| 69 | + | tryInvertIfRegion(ifRegion); |
| 70 | + | } |
| 71 | + | } |
| 72 | + | |
| 73 | + | private static boolean removeRedundantElseBlock(IfRegion ifRegion) { |
| 74 | + | if (ifRegion.getElseRegion() != null && hasSimpleReturnBlock(ifRegion.getThenRegion())) { |
| 75 | + | IRegion parent = ifRegion.getParent(); |
| 76 | + | Region newRegion = new Region(parent); |
| 77 | + | if (parent.replaceSubBlock(ifRegion, newRegion)) { |
| 78 | + | newRegion.add(ifRegion); |
| 79 | + | newRegion.add(ifRegion.getElseRegion()); |
| 80 | + | ifRegion.setElseRegion(null); |
| 81 | + | return true; |
| 82 | + | } |
| 83 | + | } |
| 84 | + | return false; |
| 85 | + | } |
| 86 | + | |
| 87 | + | /** |
| 88 | + | * Mark if-else-if chains |
| 89 | + | */ |
| 90 | + | private static void markElseIfChains(IfRegion ifRegion) { |
| 91 | + | IContainer elsRegion = ifRegion.getElseRegion(); |
| 92 | + | if (elsRegion != null) { |
| 93 | + | if (elsRegion instanceof IfRegion) { |
| 94 | + | elsRegion.getAttributes().add(AttributeFlag.ELSE_IF_CHAIN); |
| 95 | + | } else if (elsRegion instanceof Region) { |
| 96 | + | List<IContainer> subBlocks = ((Region) elsRegion).getSubBlocks(); |
| 97 | + | if (subBlocks.size() == 1 && subBlocks.get(0) instanceof IfRegion) { |
| 98 | + | subBlocks.get(0).getAttributes().add(AttributeFlag.ELSE_IF_CHAIN); |
| 99 | + | } |
| 100 | + | } |
| 101 | + | } |
| 102 | + | } |
| 103 | + | |
| 104 | + | private static void tryInvertIfRegion(IfRegion ifRegion) { |
| 105 | + | IContainer elseRegion = ifRegion.getElseRegion(); |
| 106 | + | if (elseRegion != null && RegionUtils.notEmpty(elseRegion)) { |
| 107 | + | ifRegion.invert(); |
| 108 | + | } |
| 109 | + | } |
| 110 | + | |
| 111 | + | private static boolean hasSimpleReturnBlock(IContainer region) { |
| 112 | + | if (region == null) { |
| 113 | + | return false; |
| 114 | + | } |
| 115 | + | if (region.getAttributes().contains(AttributeFlag.RETURN)) { |
| 116 | + | return true; |
| 117 | + | } |
| 118 | + | if (region instanceof IRegion) { |
| 119 | + | List<IContainer> subBlocks = ((IRegion) region).getSubBlocks(); |
| 120 | + | if (subBlocks.size() == 1 |
| 121 | + | && subBlocks.get(0).getAttributes().contains(AttributeFlag.RETURN)) { |
| 122 | + | return true; |
| 123 | + | } |
| 124 | + | } |
| 125 | + | return false; |
| 126 | + | } |
| 127 | + | } |
| 128 | + | |