Search in sources :

Example 1 with DynamicType

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class TrivialTypeTest method testPlain.

@Test
public void testPlain() throws Exception {
    when(classFileVersion.getMinorMajorVersion()).thenReturn(ClassFileVersion.JAVA_V5.getMinorMajorVersion());
    DynamicType dynamicType = TrivialType.PLAIN.make(FOO, classFileVersion, methodAccessorFactory);
    assertThat(dynamicType.getTypeDescription().getName(), is(FOO));
    assertThat(dynamicType.getTypeDescription().getModifiers(), is(Opcodes.ACC_SYNTHETIC));
    assertThat(dynamicType.getTypeDescription().getDeclaredAnnotations().size(), is(0));
    assertThat(dynamicType.getAuxiliaryTypes().size(), is(0));
    assertThat(dynamicType.getLoadedTypeInitializers().get(dynamicType.getTypeDescription()).isAlive(), is(false));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) Test(org.junit.Test)

Example 2 with DynamicType

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class ClassInjectorUsingInstrumentationTest method testSystemInjection.

@Test
@AgentAttachmentRule.Enforce
public void testSystemInjection() throws Exception {
    ClassInjector classInjector = ClassInjector.UsingInstrumentation.of(folder, ClassInjector.UsingInstrumentation.Target.SYSTEM, ByteBuddyAgent.install());
    String name = BAR + RandomString.make();
    DynamicType dynamicType = new ByteBuddy().subclass(Object.class).name(name).make();
    Map<TypeDescription, Class<?>> types = classInjector.inject(Collections.singletonMap(dynamicType.getTypeDescription(), dynamicType.getBytes()));
    assertThat(types.size(), is(1));
    assertThat(types.get(dynamicType.getTypeDescription()).getName(), is(name));
    assertThat(types.get(dynamicType.getTypeDescription()).getClassLoader(), is(ClassLoader.getSystemClassLoader()));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) TypeDescription(net.bytebuddy.description.type.TypeDescription) RandomString(net.bytebuddy.utility.RandomString) Test(org.junit.Test)

Example 3 with DynamicType

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType 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)

Example 4 with DynamicType

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class InvokeDynamicTest method testBootstrapWithArrayArgumentsWithoutArguments.

@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapWithArrayArgumentsWithoutArguments() throws Exception {
    Class<?> type = Class.forName(PARAMETER_BOOTSTRAP);
    Field field = type.getDeclaredField(ARGUMENTS_FIELD_NAME);
    field.set(null, null);
    TypeDescription typeDescription = new TypeDescription.ForLoadedType(type);
    DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP_ARRAY_ARGUMENTS)).getOnly()).withoutArguments()).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is(FOO));
    Object[] arguments = (Object[]) field.get(null);
    assertThat(arguments.length, is(0));
}
Also used : Field(java.lang.reflect.Field) DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 5 with DynamicType

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class InvokeDynamicTest method testBootstrapWithImplicitArgument.

@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapWithImplicitArgument() throws Exception {
    TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
    DynamicType.Loaded<SimpleWithArgument> dynamicType = new ByteBuddy().subclass(SimpleWithArgument.class).method(isDeclaredBy(SimpleWithArgument.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withMethodArguments()).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(0));
    assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(FOO), is(FOO));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Aggregations

DynamicType (net.bytebuddy.dynamic.DynamicType)38 Test (org.junit.Test)33 ByteBuddy (net.bytebuddy.ByteBuddy)30 TypeDescription (net.bytebuddy.description.type.TypeDescription)23 Field (java.lang.reflect.Field)10 URLClassLoader (java.net.URLClassLoader)7 Type (java.lang.reflect.Type)5 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)5 GenericType (net.bytebuddy.test.scope.GenericType)5 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)4 RandomString (net.bytebuddy.utility.RandomString)4 TargetType (net.bytebuddy.dynamic.TargetType)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 Callable (java.util.concurrent.Callable)2 Plugin (net.bytebuddy.build.Plugin)2 MethodDescription (net.bytebuddy.description.method.MethodDescription)2 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)2 StubMethod (net.bytebuddy.implementation.StubMethod)2