private static BuilderExceptionHandler buildHandler(int codeAddress, VirtualClass exceptionType) { BuilderExceptionHandler handler = mock(BuilderExceptionHandler.class); String name = exceptionType.getName(); when(handler.getExceptionType()).thenReturn(name); when(handler.getHandlerCodeAddress()).thenReturn(codeAddress); return handler; }
static BuilderExceptionHandler newExceptionHandler(final String exceptionType, Label handler) { if (exceptionType == null) { return newExceptionHandler(handler); } return new BuilderExceptionHandler(handler) { @Override public String getExceptionType() { return exceptionType; } @Override public int getHandlerCodeAddress() { return handler.getCodeAddress(); } }; }
public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nonnull Label handler) { this.start = start; this.end = end; this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(handler); }
static BuilderExceptionHandler newExceptionHandler(Label handler) { return new BuilderExceptionHandler(handler) { @Override public String getExceptionType() { return null; } @Override public int getHandlerCodeAddress() { return handler.getCodeAddress(); } }; }
private static Set<Integer> getExceptionHandlerAddresses(ExecutionGraphManipulator manipulator) { int[] allAddresses = manipulator.getAddresses(); Arrays.sort(allAddresses); int highestAddress = allAddresses[allAddresses.length - 1]; Set<Integer> handlerAddresses = new HashSet<>(); List<BuilderTryBlock> tryBlocks = manipulator.getTryBlocks(); for (BuilderTryBlock tryBlock : tryBlocks) { List<? extends BuilderExceptionHandler> handlers = tryBlock.getExceptionHandlers(); for (BuilderExceptionHandler handler : handlers) { int address = handler.getHandlerCodeAddress(); BuilderInstruction instruction = manipulator.getInstruction(address); while (address < highestAddress) { // Add all instructions until return, goto, etc. handlerAddresses.add(address); address += instruction.getCodeUnits(); instruction = manipulator.getInstruction(address); if (!instruction.getOpcode().canContinue()) { break; } } } } return handlerAddresses; }
static BuilderExceptionHandler newExceptionHandler(final TypeReference exceptionType, Label handler) { if (exceptionType == null) { return newExceptionHandler(handler); } return new BuilderExceptionHandler(handler) { @Override public String getExceptionType() { return exceptionType.getType(); } @Override public int getHandlerCodeAddress() { return handler.getCodeAddress(); } @Override public TypeReference getExceptionTypeReference() { return exceptionType; } }; }
public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nullable TypeReference exceptionType, @Nonnull Label handler) { this.start = start; this.end = end; this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler); }
static BuilderExceptionHandler newExceptionHandler(@Nonnull Label handler) { return new BuilderExceptionHandler(handler) { @Nullable @Override public String getExceptionType() { return null; } @Override public int getHandlerCodeAddress() { return handler.getCodeAddress(); } }; }
@Test public void testNewLabelByAddress() { MethodImplementationBuilder builder = new MethodImplementationBuilder(10); builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); builder.addInstruction(new BuilderInstruction32x(Opcode.MOVE_16, 0, 0)); MutableMethodImplementation mutableMethodImplementation = new MutableMethodImplementation(builder.getMethodImplementation()); mutableMethodImplementation.addCatch( mutableMethodImplementation.newLabelForAddress(0), mutableMethodImplementation.newLabelForAddress(8), mutableMethodImplementation.newLabelForAddress(1)); Assert.assertEquals(0, mutableMethodImplementation.getTryBlocks().get(0).getStartCodeAddress()); Assert.assertEquals(8, mutableMethodImplementation.getTryBlocks().get(0).getCodeUnitCount()); Assert.assertEquals(1, mutableMethodImplementation.getTryBlocks().get(0).getExceptionHandlers().get(0) .getHandlerCodeAddress()); }
static BuilderExceptionHandler newExceptionHandler(@Nullable final String exceptionType, @Nonnull Label handler) { if (exceptionType == null) { return newExceptionHandler(handler); } return new BuilderExceptionHandler(handler) { @Nullable @Override public String getExceptionType() { return exceptionType; } @Override public int getHandlerCodeAddress() { return handler.getCodeAddress(); } }; } }
public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nonnull Label handler) { this.start = start; this.end = end; this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(handler); }
static BuilderExceptionHandler newExceptionHandler(@Nonnull Label handler) { return new BuilderExceptionHandler(handler) { @Nullable @Override public String getExceptionType() { return null; } @Override public int getHandlerCodeAddress() { return handler.getCodeAddress(); } }; }
@Test public void testNewLabelByIndex() { MethodImplementationBuilder builder = new MethodImplementationBuilder(10); builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); builder.addInstruction(new BuilderInstruction10x(Opcode.NOP)); builder.addInstruction(new BuilderInstruction32x(Opcode.MOVE_16, 0, 0)); MutableMethodImplementation mutableMethodImplementation = new MutableMethodImplementation(builder.getMethodImplementation()); mutableMethodImplementation.addCatch( mutableMethodImplementation.newLabelForIndex(0), mutableMethodImplementation.newLabelForIndex(6), mutableMethodImplementation.newLabelForIndex(1)); Assert.assertEquals(0, mutableMethodImplementation.getTryBlocks().get(0).getStartCodeAddress()); Assert.assertEquals(8, mutableMethodImplementation.getTryBlocks().get(0).getCodeUnitCount()); Assert.assertEquals(1, mutableMethodImplementation.getTryBlocks().get(0).getExceptionHandlers().get(0) .getHandlerCodeAddress()); } }
static BuilderExceptionHandler newExceptionHandler(@Nullable final String exceptionType, @Nonnull Label handler) { if (exceptionType == null) { return newExceptionHandler(handler); } return new BuilderExceptionHandler(handler) { @Nullable @Override public String getExceptionType() { return exceptionType; } @Override public int getHandlerCodeAddress() { return handler.getCodeAddress(); } }; } }
public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nullable TypeReference exceptionType, @Nonnull Label handler) { this.start = start; this.end = end; this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler); }
static BuilderExceptionHandler newExceptionHandler(@Nonnull Label handler) { return new BuilderExceptionHandler(handler) { @Nullable @Override public String getExceptionType() { return null; } @Override public int getHandlerCodeAddress() { return handler.getCodeAddress(); } }; }
static BuilderExceptionHandler newExceptionHandler(@Nullable final String exceptionType, @Nonnull Label handler) { if (exceptionType == null) { return newExceptionHandler(handler); } return new BuilderExceptionHandler(handler) { @Nullable @Override public String getExceptionType() { return exceptionType; } @Override public int getHandlerCodeAddress() { return handler.getCodeAddress(); } }; } }
public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nullable TypeReference exceptionType, @Nonnull Label handler) { this.start = start; this.end = end; this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(exceptionType, handler); }
static BuilderExceptionHandler newExceptionHandler(@Nullable final TypeReference exceptionType, @Nonnull Label handler) { if (exceptionType == null) { return newExceptionHandler(handler); } return new BuilderExceptionHandler(handler) { @Nullable @Override public String getExceptionType() { return exceptionType.getType(); } @Override public int getHandlerCodeAddress() { return handler.getCodeAddress(); } @Nullable @Override public TypeReference getExceptionTypeReference() { return exceptionType; } }; }
public BuilderTryBlock(@Nonnull Label start, @Nonnull Label end, @Nonnull Label handler) { this.start = start; this.end = end; this.exceptionHandler = BuilderExceptionHandler.newExceptionHandler(handler); }