use of jadx.core.dex.nodes.IRegion in project jadx by skylot.
the class RegionUtils method getFirstInsn.
public static InsnNode getFirstInsn(IContainer container) {
if (container instanceof IBlock) {
IBlock block = (IBlock) container;
List<InsnNode> insnList = block.getInstructions();
if (insnList.isEmpty()) {
return null;
}
return insnList.get(0);
} else if (container instanceof IBranchRegion) {
return null;
} else if (container instanceof IRegion) {
IRegion region = (IRegion) container;
List<IContainer> blocks = region.getSubBlocks();
if (blocks.isEmpty()) {
return null;
}
return getFirstInsn(blocks.get(0));
} else {
throw new JadxRuntimeException(unknownContainerType(container));
}
}
use of jadx.core.dex.nodes.IRegion in project jadx by skylot.
the class RegionUtils method getLastInsn.
public static InsnNode getLastInsn(IContainer container) {
if (container instanceof IBlock) {
IBlock block = (IBlock) container;
List<InsnNode> insnList = block.getInstructions();
if (insnList.isEmpty()) {
return null;
}
return insnList.get(insnList.size() - 1);
} else if (container instanceof IBranchRegion) {
return null;
} else if (container instanceof IRegion) {
IRegion region = (IRegion) container;
List<IContainer> blocks = region.getSubBlocks();
if (blocks.isEmpty()) {
return null;
}
return getLastInsn(blocks.get(blocks.size() - 1));
} else {
throw new JadxRuntimeException(unknownContainerType(container));
}
}
use of jadx.core.dex.nodes.IRegion in project jadx by skylot.
the class RegionUtils method isRegionContainsRegion.
/**
* Check if {@code region} contains in {@code container}.
* <br>
* For simple region (not from exception handlers) search in parents
* otherwise run recursive search because exception handlers can have several parents
*/
public static boolean isRegionContainsRegion(IContainer container, IRegion region) {
if (container == region) {
return true;
}
if (region == null) {
return false;
}
IRegion parent = region.getParent();
while (container != parent) {
if (parent == null) {
if (region.contains(AType.EXC_HANDLER)) {
return isRegionContainsExcHandlerRegion(container, region);
}
return false;
}
region = parent;
parent = region.getParent();
}
return true;
}
use of jadx.core.dex.nodes.IRegion in project jadx by skylot.
the class ProcessTryCatchRegions method wrapBlocks.
/**
* Extract all block dominated by 'dominator' to separate region and mark as try/catch block
*/
private static boolean wrapBlocks(IRegion replaceRegion, TryCatchBlockAttr tb, BlockNode dominator) {
if (replaceRegion == null) {
return false;
}
if (replaceRegion instanceof LoopRegion) {
LoopRegion loop = (LoopRegion) replaceRegion;
return wrapBlocks(loop.getBody(), tb, dominator);
}
if (replaceRegion instanceof IBranchRegion) {
return wrapBlocks(replaceRegion.getParent(), tb, dominator);
}
Region tryRegion = new Region(replaceRegion);
List<IContainer> subBlocks = replaceRegion.getSubBlocks();
for (IContainer cont : subBlocks) {
if (RegionUtils.hasPathThroughBlock(dominator, cont)) {
if (isHandlerPath(tb, cont)) {
break;
}
tryRegion.getSubBlocks().add(cont);
}
}
if (tryRegion.getSubBlocks().isEmpty()) {
return false;
}
TryCatchRegion tryCatchRegion = new TryCatchRegion(replaceRegion, tryRegion);
tryRegion.setParent(tryCatchRegion);
tryCatchRegion.setTryCatchBlock(tb);
// replace first node by region
IContainer firstNode = tryRegion.getSubBlocks().get(0);
if (!replaceRegion.replaceSubBlock(firstNode, tryCatchRegion)) {
return false;
}
subBlocks.removeAll(tryRegion.getSubBlocks());
// fix parents for tryRegion sub blocks
for (IContainer cont : tryRegion.getSubBlocks()) {
if (cont instanceof AbstractRegion) {
AbstractRegion aReg = (AbstractRegion) cont;
aReg.setParent(tryRegion);
}
}
return true;
}
use of jadx.core.dex.nodes.IRegion in project jadx by skylot.
the class RegionMakerVisitor method visit.
@Override
public void visit(MethodNode mth) throws JadxException {
if (mth.isNoCode() || mth.getBasicBlocks().isEmpty()) {
return;
}
RegionMaker rm = new RegionMaker(mth);
RegionStack state = new RegionStack(mth);
// fill region structure
BlockNode startBlock = Utils.first(mth.getEnterBlock().getCleanSuccessors());
mth.setRegion(rm.makeRegion(startBlock, state));
if (!mth.isNoExceptionHandlers()) {
IRegion expOutBlock = rm.processTryCatchBlocks(mth);
if (expOutBlock != null) {
mth.getRegion().add(expOutBlock);
}
}
postProcessRegions(mth);
}
Aggregations