use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testExceptionPriniting.
@Test
public void testExceptionPriniting() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(ExceptionWriterTest.class).withExceptionPrinting().on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
PrintStream printStream = mock(PrintStream.class);
PrintStream err = System.err;
synchronized (System.err) {
System.setErr(printStream);
try {
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
} finally {
System.setErr(err);
}
}
verify(printStream, times(2)).println(Mockito.any(RuntimeException.class));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testAdviceSkipExceptionExplicit.
@Test
public void testAdviceSkipExceptionExplicit() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(TrivialAdviceSkipException.class).on(named(BAR + BAZ))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
try {
type.getDeclaredMethod(BAR + BAZ).invoke(type.getDeclaredConstructor().newInstance());
fail();
} catch (InvocationTargetException exception) {
assertThat(exception.getCause(), instanceOf(NullPointerException.class));
}
assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1));
assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 0));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testAdviceWithExceptionHandler.
@Test
public void testAdviceWithExceptionHandler() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(ExceptionHandlerAdvice.class).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1));
assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testFrameAdviceSimpleShiftExpanded.
@Test
public void testFrameAdviceSimpleShiftExpanded() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(FrameShiftAdvice.class).on(named(FOO)).readerFlags(ClassReader.EXPAND_FRAMES)).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testAdviceThrowNotSuppressedOnExit.
@Test
public void testAdviceThrowNotSuppressedOnExit() throws Exception {
Class<?> type = new ByteBuddy().redefine(TracableSample.class).visit(Advice.to(ThrowNotSuppressedOnExit.class).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
try {
type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance());
fail();
} catch (InvocationTargetException exception) {
assertThat(exception.getCause(), instanceOf(Exception.class));
}
assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 1));
assertThat(type.getDeclaredField(INSIDE).get(null), is((Object) 1));
assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1));
}
Aggregations