| 30 | import static java.util.Objects.requireNonNull; |
| 31 | |
| 32 | public class TryCatch |
| 33 | implements FlowControl |
| 34 | { |
| 35 | private final String comment; |
| 36 | private final BytecodeNode tryNode; |
| 37 | private final List<CatchBlock> catchBlocks; |
| 38 | |
| 39 | public TryCatch(BytecodeNode tryNode, List<CatchBlock> catchBlocks) |
| 40 | { |
| 41 | this(null, tryNode, catchBlocks); |
| 42 | } |
| 43 | |
| 44 | public TryCatch(String comment, BytecodeNode tryNode, List<CatchBlock> catchBlocks) |
| 45 | { |
| 46 | this.comment = comment; |
| 47 | this.tryNode = requireNonNull(tryNode, "tryNode is null"); |
| 48 | this.catchBlocks = ImmutableList.copyOf(requireNonNull(catchBlocks, "catchBlocks is null")); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public String getComment() |
| 53 | { |
| 54 | return comment; |
| 55 | } |
| 56 | |
| 57 | public BytecodeNode getTryNode() |
| 58 | { |
| 59 | return tryNode; |
| 60 | } |
| 61 | |
| 62 | public List<CatchBlock> getCatchBlocks() |
| 63 | { |
| 64 | return catchBlocks; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) |
| 69 | { |
| 70 | LabelNode tryStart = new LabelNode("tryStart"); |
| 71 | LabelNode tryEnd = new LabelNode("tryEnd"); |
| 72 | List<LabelNode> handlers = new ArrayList<>(); |
| 73 | LabelNode done = new LabelNode("done"); |
| 74 | |
| 75 | BytecodeBlock block = new BytecodeBlock(); |
| 76 | |
| 77 | // try block |
| 78 | block.visitLabel(tryStart) |
| 79 | .append(tryNode) |
| 80 | .visitLabel(tryEnd) |
| 81 | .gotoLabel(done); |
| 82 | |
| 83 | // catch blocks |
| 84 | for (int i = 0; i < catchBlocks.size(); i++) { |
| 85 | BytecodeNode handlerBlock = catchBlocks.get(i).getHandler(); |
| 86 | LabelNode handler = new LabelNode("handler" + i); |
| 87 | handlers.add(handler); |
| 88 | block.visitLabel(handler) |
| 89 | .append(handlerBlock); |
nothing calls this directly
no outgoing calls
no test coverage detected