in bytekit-core/src/main/java/com/alibaba/bytekit/asm/location/InvokeLocationMatcher.java [119:173]
public List<Location> matchForException(MethodProcessor methodProcessor) {
List<Location> locations = new ArrayList<Location>();
AbstractInsnNode insnNode = methodProcessor.getEnterInsnNode();
MethodNode methodNode = methodProcessor.getMethodNode();
List<MethodInsnNode> methodInsnNodes = new ArrayList<MethodInsnNode>();
LocationFilter locationFilter = methodProcessor.getLocationFilter();
LocationType locationType = LocationType.INVOKE_EXCEPTION_EXIT;
int matchedCount = 0;
while (insnNode != null) {
if (insnNode instanceof MethodInsnNode) {
MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode;
if (matchCall(methodInsnNode)) {
if(locationFilter.allow(methodInsnNode, locationType, this.whenComplete)) {
matchedCount++;
if (count <= 0 || count == matchedCount) {
methodInsnNodes.add(methodInsnNode);
}
}
}
}
insnNode = insnNode.getNext();
}
// insert try/catch
for (MethodInsnNode methodInsnNode : methodInsnNodes) {
TryCatchBlock tryCatchBlock = new TryCatchBlock(methodNode);
InsnList toInsert = new InsnList();
LabelNode gotoDest = new LabelNode();
LabelNode startLabelNode = tryCatchBlock.getStartLabelNode();
LabelNode endLabelNode = tryCatchBlock.getEndLabelNode();
toInsert.add(new JumpInsnNode(Opcodes.GOTO, gotoDest));
toInsert.add(endLabelNode);
AsmOpUtils.throwException(toInsert);
locations.add(new InvokeExceptionExitLocation(methodInsnNode, endLabelNode));
toInsert.add(gotoDest);
methodNode.instructions.insertBefore(methodInsnNode, startLabelNode);
methodNode.instructions.insert(methodInsnNode, toInsert);
tryCatchBlock.sort();
}
return locations;
}