Search in sources :

Example 91 with ByteBuddy

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));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 92 with ByteBuddy

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));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 93 with ByteBuddy

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));
}
Also used : ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 94 with ByteBuddy

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))));
}
Also used : EntryPoint(net.bytebuddy.build.EntryPoint) ByteBuddy(net.bytebuddy.ByteBuddy) Plugin(net.bytebuddy.build.Plugin) Test(org.junit.Test)

Example 95 with ByteBuddy

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();
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Callable(java.util.concurrent.Callable)

Aggregations

ByteBuddy (net.bytebuddy.ByteBuddy)369 Test (org.junit.Test)355 DynamicType (net.bytebuddy.dynamic.DynamicType)188 Method (java.lang.reflect.Method)34 TypeDescription (net.bytebuddy.description.type.TypeDescription)26 InvocationTargetException (java.lang.reflect.InvocationTargetException)19 Field (java.lang.reflect.Field)18 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)17 URLClassLoader (java.net.URLClassLoader)14 Instrumentation (java.lang.instrument.Instrumentation)10 StubMethod (net.bytebuddy.implementation.StubMethod)7 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)7 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)6 RandomString (net.bytebuddy.utility.RandomString)6 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)5 MethodDescription (net.bytebuddy.description.method.MethodDescription)5 File (java.io.File)4 Constructor (java.lang.reflect.Constructor)4 FieldDescription (net.bytebuddy.description.field.FieldDescription)4 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)4