use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testAdviceNotSkipExceptionExplicit.
@Test
public void testAdviceNotSkipExceptionExplicit() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(TrivialAdvice.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) 1));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testOriginAdvice.
@Test
public void testOriginAdvice() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(OriginAdvice.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 testTrivialAdviceMultipleMethodsWithSuppression.
@Test
public void testTrivialAdviceMultipleMethodsWithSuppression() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(TrivialAdviceWithSuppression.class).on(isMethod())).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));
assertThat(type.getDeclaredMethod(BAZ).invoke(null), is((Object) FOO));
assertThat(type.getDeclaredField(ENTER).get(null), is((Object) 2));
assertThat(type.getDeclaredField(EXIT).get(null), is((Object) 2));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testTrivialAdviceWithDelegationEnterOnly.
@Test
public void testTrivialAdviceWithDelegationEnterOnly() throws Exception {
Class<?> type = new ByteBuddy().redefine(TrivialAdviceDelegation.class).visit(Advice.to(TrivialAdviceDelegation.class, EmptyAdvice.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) 0));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testOriginMethodStackSizeAdvice.
@Test
public void testOriginMethodStackSizeAdvice() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(OriginMethodStackSizeAdvice.class).on(named(BAR))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
assertThat(type.getDeclaredMethod(BAR, String.class).invoke(type.getDeclaredConstructor().newInstance(), FOO), is((Object) FOO));
}
Aggregations