use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testTrivialDelegation.
@Test
public void testTrivialDelegation() throws Exception {
Class<?> type = new ByteBuddy().redefine(EmptyDelegationAdvice.class).visit(Advice.to(EmptyDelegationAdvice.class).on(named(FOO))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), nullValue(Object.class));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class AdviceTest method testAdviceOnConstructorExitAdviceWithSuppression.
@Test
public void testAdviceOnConstructorExitAdviceWithSuppression() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(TrivialAdviceSkipExceptionWithSuppression.class).on(isConstructor())).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
assertThat(type.getDeclaredConstructor().newInstance(), notNullValue(Object.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 testVariableMappingAdviceLarger.
@Test
public void testVariableMappingAdviceLarger() throws Exception {
Class<?> type = new ByteBuddy().redefine(Sample.class).visit(Advice.to(AdviceWithVariableValues.class).on(named(BAR))).make().load(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassLoadingStrategy.Default.WRAPPER).getLoaded();
assertThat(type.getDeclaredMethod(BAR, String.class).invoke(type.getDeclaredConstructor().newInstance(), FOO + BAR + QUX + BAZ), is((Object) (FOO + BAR + QUX + BAZ)));
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 AgentBuilderDefaultTest method testBuildPluginWithEntryPoint.
@Test
public void testBuildPluginWithEntryPoint() throws Exception {
Plugin plugin = mock(Plugin.class);
EntryPoint entryPoint = mock(EntryPoint.class);
ByteBuddy byteBuddy = mock(ByteBuddy.class);
when(entryPoint.getByteBuddy()).thenReturn(byteBuddy);
assertThat(AgentBuilder.Default.of(entryPoint, plugin), is((AgentBuilder) new AgentBuilder.Default(byteBuddy).with(new AgentBuilder.TypeStrategy.ForBuildEntryPoint(entryPoint)).type(plugin).transform(new AgentBuilder.Transformer.ForBuildPlugin(plugin))));
}
use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.
the class MethodCallProxy method make.
@Override
public DynamicType make(String auxiliaryTypeName, ClassFileVersion classFileVersion, MethodAccessorFactory methodAccessorFactory) {
MethodDescription accessorMethod = methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT);
LinkedHashMap<String, TypeDescription> parameterFields = extractFields(accessorMethod);
DynamicType.Builder<?> builder = new ByteBuddy(classFileVersion).with(PrecomputedMethodGraph.INSTANCE).subclass(Object.class, ConstructorStrategy.Default.NO_CONSTRUCTORS).name(auxiliaryTypeName).modifiers(DEFAULT_TYPE_MODIFIER).implement(Runnable.class, Callable.class).intercept(new MethodCall(accessorMethod, assigner)).implement(serializableProxy ? new Class<?>[] { Serializable.class } : new Class<?>[0]).defineConstructor().withParameters(parameterFields.values()).intercept(ConstructorCall.INSTANCE);
for (Map.Entry<String, TypeDescription> field : parameterFields.entrySet()) {
builder = builder.defineField(field.getKey(), field.getValue(), Visibility.PRIVATE);
}
return builder.make();
}
Aggregations