use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testTrivialAdviceDistributedExitOnly.
@Test
public void testTrivialAdviceDistributedExitOnly() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(EmptyAdvice.class, TrivialAdvice.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) 0));
assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 1));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testExceptionCatchedAdvice.
@Test
public void testExceptionCatchedAdvice() throws Exception {
Class<?> type = new ByteBuddy().redefine(ExceptionCatchedAdvice.class).visit(Advice.to(ExceptionCatchedAdvice.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(RuntimeException.class));
}
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 testNonAssignableCasting.
@Test
public void testNonAssignableCasting() throws Exception {
Class<?> type = new ByteBuddy().redefine(NonAssignableReturnTypeAdvice.class).visit(Advice.to(NonAssignableReturnTypeAdvice.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(ClassCastException.class));
}
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testTrivialAdviceWithHandlerAndSuppression.
@Test
public void testTrivialAdviceWithHandlerAndSuppression() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(TrivialAdviceWithSuppression.class).on(named(FOO + BAZ))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
assertThat(type.getDeclaredMethod(FOO + BAZ).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 testAdviceWithEntranceValue.
@Test
public void testAdviceWithEntranceValue() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(EntranceValueAdvice.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));
}
Aggregations