use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceVariableAccessTest method testArray.
@Test
public void testArray() throws Exception {
Class<?> dynamicType = new ByteBuddy().redefine(sample).visit(Advice.to(AdviceVariableAccessTest.class).on(named(READ).or(named(WRITE)))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
Object instance = dynamicType.getDeclaredConstructor().newInstance();
assertThat(dynamicType.getDeclaredMethod(WRITE, type).invoke(instance, value), nullValue(Object.class));
assertThat(dynamicType.getDeclaredMethod(READ).invoke(instance), is(value));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceFrameTest method testFrameAdvice.
@Test
public void testFrameAdvice() throws Exception {
Class<?> type = new ByteBuddy().redefine(FrameSample.class).visit(Advice.to(advice).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
assertThat(type.getDeclaredMethod(FOO, String.class).invoke(type.getDeclaredConstructor().newInstance(), FOO), is((Object) FOO));
assertThat(type.getField(COUNT).getInt(null), is((Object) count));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceFrameTest method testFrameAdviceStaticMethodExpanded.
@Test
public void testFrameAdviceStaticMethodExpanded() throws Exception {
Class<?> type = new ByteBuddy().redefine(FrameSample.class).visit(Advice.to(advice).on(named(BAR)).readerFlags(ClassReader.EXPAND_FRAMES)).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
assertThat(type.getDeclaredMethod(BAR, String.class).invoke(null, FOO), is((Object) FOO));
assertThat(type.getField(COUNT).getInt(null), is((Object) count));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceNoRegularReturnTest method testNoRegularReturnWithHandler.
@Test
public void testNoRegularReturnWithHandler() throws Exception {
Class<?> type = new ByteBuddy().redefine(this.type).visit(Advice.to(ExitAdviceWithHandler.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));
}
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceNoRegularReturnTest method testNoRegularReturnWithSkip.
@Test
public void testNoRegularReturnWithSkip() throws Exception {
Class<?> type = new ByteBuddy().redefine(this.type).visit(Advice.to(EnterAdviceSkip.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));
}
}
Aggregations