public void process(@NotNull ParserClassNode classNode, @NotNull RuleMethod method) throws Exception {
if (method.getNumberOfReturns() == 1) return;
Preconditions.checkState(method.getNumberOfReturns() > 1);
AbstractInsnNode current = method.instructions.getLast();
// find last return
while (current.getOpcode() != ARETURN) {
current = current.getPrevious();
}
LabelNode lastReturnLabel = new LabelNode();
method.instructions.insertBefore(current, lastReturnLabel);
// iterate backwards up to first instructions
while ((current = current.getPrevious()) != null) {
// replace returns with gotos
if (current.getOpcode() == ARETURN) {
JumpInsnNode gotoInstruction = new JumpInsnNode(GOTO, lastReturnLabel);
method.instructions.set(current, gotoInstruction);
current = gotoInstruction;
}
}