Search in sources :

Example 11 with DynamicType

use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class InvokeDynamicTest method testBootstrapWithFieldExplicitType.

@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapWithFieldExplicitType() throws Exception {
    TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
    DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).defineField(FOO, Object.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withField(FOO).as(String.class).withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(1));
    Simple instance = dynamicType.getLoaded().getDeclaredConstructor().newInstance();
    Field field = dynamicType.getLoaded().getDeclaredField(FOO);
    field.setAccessible(true);
    field.set(instance, FOO);
    assertThat(instance.foo(), is(FOO));
}
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 12 with DynamicType

use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class RebaseDynamicTypeBuilderTest method testConstructorRebaseSingleAuxiliaryType.

@Test
public void testConstructorRebaseSingleAuxiliaryType() throws Exception {
    DynamicType.Unloaded<?> dynamicType = new ByteBuddy().rebase(Bar.class).constructor(any()).intercept(SuperMethodCall.INSTANCE).make();
    assertThat(dynamicType.getAuxiliaryTypes().size(), is(1));
    Class<?> type = dynamicType.load(new URLClassLoader(new URL[0], null), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredConstructors().length, is(2));
    assertThat(type.getDeclaredMethods().length, is(0));
    Field field = type.getDeclaredField(BAR);
    assertThat(field.get(type.getDeclaredConstructor(String.class).newInstance(FOO)), is((Object) FOO));
}
Also used : Field(java.lang.reflect.Field) DynamicType(net.bytebuddy.dynamic.DynamicType) URLClassLoader(java.net.URLClassLoader) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 13 with DynamicType

use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class RedefinitionDynamicTypeBuilderTest method testConstructorRetentionNoAuxiliaryType.

@Test
public void testConstructorRetentionNoAuxiliaryType() throws Exception {
    DynamicType.Unloaded<?> dynamicType = new ByteBuddy().redefine(Bar.class).make();
    assertThat(dynamicType.getAuxiliaryTypes().size(), is(0));
    Class<?> type = dynamicType.load(new URLClassLoader(new URL[0], null), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredConstructors().length, is(1));
    assertThat(type.getDeclaredMethods().length, is(0));
    Field field = type.getDeclaredField(BAR);
    assertThat(field.get(type.getDeclaredConstructor(String.class).newInstance(FOO)), is((Object) FOO));
}
Also used : Field(java.lang.reflect.Field) DynamicType(net.bytebuddy.dynamic.DynamicType) URLClassLoader(java.net.URLClassLoader) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 14 with DynamicType

use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class SubclassDynamicTypeBuilderTest method testGenericTypeRawExtension.

@Test
public void testGenericTypeRawExtension() throws Exception {
    Class<?> dynamicType = new ByteBuddy().subclass(GenericType.Inner.class).method(named(FOO).or(named("call"))).intercept(StubMethod.INSTANCE).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded();
    assertThat(dynamicType.getTypeParameters().length, is(0));
    assertThat(dynamicType.getGenericSuperclass(), instanceOf(Class.class));
    assertThat(dynamicType.getGenericSuperclass(), is((Type) GenericType.Inner.class));
    assertThat(dynamicType.getGenericInterfaces().length, is(0));
    Method foo = dynamicType.getDeclaredMethod(FOO, String.class);
    assertThat(foo.getTypeParameters().length, is(0));
    assertThat(foo.getGenericReturnType(), is((Object) List.class));
    Method call = dynamicType.getDeclaredMethod("call");
    assertThat(call.getGenericReturnType(), is((Object) Map.class));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) GenericType(net.bytebuddy.test.scope.GenericType) Type(java.lang.reflect.Type) TargetType(net.bytebuddy.dynamic.TargetType) GenericType(net.bytebuddy.test.scope.GenericType) ByteBuddy(net.bytebuddy.ByteBuddy) StubMethod(net.bytebuddy.implementation.StubMethod) Method(java.lang.reflect.Method) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 15 with DynamicType

use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class SubclassDynamicTypeBuilderTest method testBridgeMethodForAbstractMethod.

@Test
public void testBridgeMethodForAbstractMethod() throws Exception {
    Class<?> dynamicType = new ByteBuddy().subclass(AbstractGenericType.Inner.class).modifiers(Opcodes.ACC_ABSTRACT | Opcodes.ACC_PUBLIC).method(named(FOO)).withoutCode().make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST).getLoaded();
    assertThat(dynamicType.getDeclaredMethods().length, is(2));
    assertEquals(Void.class, dynamicType.getDeclaredMethod(FOO, Void.class).getReturnType());
    assertThat(dynamicType.getDeclaredMethod(FOO, Void.class).getGenericReturnType(), is((Type) Void.class));
    assertThat(dynamicType.getDeclaredMethod(FOO, Void.class).isBridge(), is(false));
    assertThat(Modifier.isAbstract(dynamicType.getDeclaredMethod(FOO, Void.class).getModifiers()), is(true));
    assertEquals(Object.class, dynamicType.getDeclaredMethod(FOO, Object.class).getReturnType());
    assertThat(dynamicType.getDeclaredMethod(FOO, Object.class).getGenericReturnType(), is((Type) Object.class));
    assertThat(dynamicType.getDeclaredMethod(FOO, Object.class).isBridge(), is(true));
    assertThat(Modifier.isAbstract(dynamicType.getDeclaredMethod(FOO, Object.class).getModifiers()), is(false));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) GenericType(net.bytebuddy.test.scope.GenericType) Type(java.lang.reflect.Type) TargetType(net.bytebuddy.dynamic.TargetType) ByteBuddy(net.bytebuddy.ByteBuddy) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) 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