Search in sources :

Example 26 with DynamicType

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

the class SubclassDynamicTypeBuilderTest method testBridgeMethodCreation.

@Test
@SuppressWarnings("unchecked")
public void testBridgeMethodCreation() throws Exception {
    Class<?> dynamicType = new ByteBuddy().subclass(BridgeRetention.Inner.class).method(named(FOO)).intercept(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST).getLoaded();
    assertEquals(String.class, dynamicType.getDeclaredMethod(FOO).getReturnType());
    assertThat(dynamicType.getDeclaredMethod(FOO).getGenericReturnType(), is((Type) String.class));
    BridgeRetention<String> bridgeRetention = (BridgeRetention<String>) dynamicType.getDeclaredConstructor().newInstance();
    assertThat(bridgeRetention.foo(), is(FOO));
    bridgeRetention.assertZeroCalls();
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) GenericType(net.bytebuddy.test.scope.GenericType) Type(java.lang.reflect.Type) TargetType(net.bytebuddy.dynamic.TargetType) TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) ByteBuddy(net.bytebuddy.ByteBuddy) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 27 with DynamicType

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

the class AbstractDynamicTypeBuilderForInliningTest method testBridgeMethodCreation.

@Test
@SuppressWarnings("unchecked")
public void testBridgeMethodCreation() throws Exception {
    Class<?> dynamicType = create(BridgeRetention.Inner.class).method(named(FOO)).intercept(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST).getLoaded();
    assertEquals(String.class, dynamicType.getDeclaredMethod(FOO).getReturnType());
    assertThat(dynamicType.getDeclaredMethod(FOO).getGenericReturnType(), is((Type) String.class));
    BridgeRetention<String> bridgeRetention = (BridgeRetention<String>) dynamicType.getDeclaredConstructor().newInstance();
    assertThat(bridgeRetention.foo(), is(FOO));
    bridgeRetention.assertZeroCalls();
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) Type(java.lang.reflect.Type) GenericType(net.bytebuddy.test.scope.GenericType) TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 28 with DynamicType

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

the class AbstractDynamicTypeBuilderForInliningTest method testGenericType.

@Test
public void testGenericType() throws Exception {
    ClassLoader classLoader = new ByteArrayClassLoader(ClassLoadingStrategy.BOOTSTRAP_LOADER, ClassFileExtraction.of(GenericType.class));
    Class<?> dynamicType = create(GenericType.Inner.class).method(named(FOO)).intercept(StubMethod.INSTANCE).make().load(classLoader, ClassLoadingStrategy.Default.INJECTION).getLoaded();
    assertThat(dynamicType.getTypeParameters().length, is(2));
    assertThat(dynamicType.getTypeParameters()[0].getName(), is("T"));
    assertThat(dynamicType.getTypeParameters()[0].getBounds().length, is(1));
    assertThat(dynamicType.getTypeParameters()[0].getBounds()[0], instanceOf(Class.class));
    assertThat(dynamicType.getTypeParameters()[0].getBounds()[0], is((Type) String.class));
    assertThat(dynamicType.getTypeParameters()[1].getName(), is("S"));
    assertThat(dynamicType.getTypeParameters()[1].getBounds().length, is(1));
    assertThat(dynamicType.getTypeParameters()[1].getBounds()[0], is((Type) dynamicType.getTypeParameters()[0]));
    assertThat(dynamicType.getGenericSuperclass(), instanceOf(ParameterizedType.class));
    assertThat(((ParameterizedType) dynamicType.getGenericSuperclass()).getActualTypeArguments().length, is(1));
    assertThat(((ParameterizedType) dynamicType.getGenericSuperclass()).getActualTypeArguments()[0], instanceOf(ParameterizedType.class));
    ParameterizedType superClass = (ParameterizedType) ((ParameterizedType) dynamicType.getGenericSuperclass()).getActualTypeArguments()[0];
    assertThat(superClass.getActualTypeArguments().length, is(2));
    assertThat(superClass.getActualTypeArguments()[0], is((Type) dynamicType.getTypeParameters()[0]));
    assertThat(superClass.getActualTypeArguments()[1], is((Type) dynamicType.getTypeParameters()[1]));
    assertThat(superClass.getOwnerType(), instanceOf(ParameterizedType.class));
    assertThat(((ParameterizedType) superClass.getOwnerType()).getRawType(), instanceOf(Class.class));
    assertThat(((Class<?>) ((ParameterizedType) superClass.getOwnerType()).getRawType()).getName(), is(GenericType.class.getName()));
    assertThat(((ParameterizedType) superClass.getOwnerType()).getActualTypeArguments().length, is(1));
    assertThat(((ParameterizedType) superClass.getOwnerType()).getActualTypeArguments()[0], is((Type) ((Class<?>) ((ParameterizedType) superClass.getOwnerType()).getRawType()).getTypeParameters()[0]));
    assertThat(dynamicType.getGenericInterfaces().length, is(1));
    assertThat(dynamicType.getGenericInterfaces()[0], instanceOf(ParameterizedType.class));
    assertThat(((ParameterizedType) dynamicType.getGenericInterfaces()[0]).getActualTypeArguments()[0], instanceOf(ParameterizedType.class));
    assertThat(((ParameterizedType) dynamicType.getGenericInterfaces()[0]).getRawType(), is((Type) Callable.class));
    assertThat(((ParameterizedType) dynamicType.getGenericInterfaces()[0]).getOwnerType(), nullValue(Type.class));
    assertThat(((ParameterizedType) ((ParameterizedType) dynamicType.getGenericInterfaces()[0]).getActualTypeArguments()[0]).getActualTypeArguments().length, is(2));
    ParameterizedType interfaceType = (ParameterizedType) ((ParameterizedType) dynamicType.getGenericInterfaces()[0]).getActualTypeArguments()[0];
    assertThat(interfaceType.getRawType(), is((Type) Map.class));
    assertThat(interfaceType.getActualTypeArguments().length, is(2));
    assertThat(interfaceType.getActualTypeArguments()[0], instanceOf(WildcardType.class));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[0]).getUpperBounds().length, is(1));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[0]).getUpperBounds()[0], is((Type) Object.class));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[0]).getLowerBounds().length, is(1));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[0]).getLowerBounds()[0], is((Type) String.class));
    assertThat(interfaceType.getActualTypeArguments()[1], instanceOf(WildcardType.class));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[1]).getUpperBounds().length, is(1));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[1]).getUpperBounds()[0], is((Type) String.class));
    assertThat(((WildcardType) interfaceType.getActualTypeArguments()[1]).getLowerBounds().length, is(0));
    Method foo = dynamicType.getDeclaredMethod(FOO, String.class);
    assertThat(foo.getGenericReturnType(), instanceOf(ParameterizedType.class));
    assertThat(((ParameterizedType) foo.getGenericReturnType()).getActualTypeArguments().length, is(1));
    assertThat(((ParameterizedType) foo.getGenericReturnType()).getActualTypeArguments()[0], instanceOf(GenericArrayType.class));
    assertThat(((GenericArrayType) ((ParameterizedType) foo.getGenericReturnType()).getActualTypeArguments()[0]).getGenericComponentType(), is((Type) dynamicType.getTypeParameters()[0]));
    assertThat(foo.getTypeParameters().length, is(2));
    assertThat(foo.getTypeParameters()[0].getName(), is("V"));
    assertThat(foo.getTypeParameters()[0].getBounds().length, is(1));
    assertThat(foo.getTypeParameters()[0].getBounds()[0], is((Type) dynamicType.getTypeParameters()[0]));
    assertThat(foo.getTypeParameters()[1].getName(), is("W"));
    assertThat(foo.getTypeParameters()[1].getBounds().length, is(1));
    assertThat(foo.getTypeParameters()[1].getBounds()[0], is((Type) Exception.class));
    assertThat(foo.getGenericParameterTypes().length, is(1));
    assertThat(foo.getGenericParameterTypes()[0], is((Type) foo.getTypeParameters()[0]));
    assertThat(foo.getGenericExceptionTypes().length, is(1));
    assertThat(foo.getGenericExceptionTypes()[0], is((Type) foo.getTypeParameters()[1]));
    Method call = dynamicType.getDeclaredMethod("call");
    assertThat(call.getGenericReturnType(), is((Type) interfaceType));
}
Also used : GenericType(net.bytebuddy.test.scope.GenericType) DynamicType(net.bytebuddy.dynamic.DynamicType) Type(java.lang.reflect.Type) GenericType(net.bytebuddy.test.scope.GenericType) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) URLClassLoader(java.net.URLClassLoader) ByteArrayClassLoader(net.bytebuddy.dynamic.loading.ByteArrayClassLoader) StubMethod(net.bytebuddy.implementation.StubMethod) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 29 with DynamicType

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

the class RebaseDynamicTypeBuilderTest method testConstructorRetentionNoAuxiliaryType.

@Test
public void testConstructorRetentionNoAuxiliaryType() throws Exception {
    DynamicType.Unloaded<?> dynamicType = new ByteBuddy().rebase(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 30 with DynamicType

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

the class RedefinitionDynamicTypeBuilderTest method testConstructorRebaseSingleAuxiliaryType.

@Test
public void testConstructorRebaseSingleAuxiliaryType() throws Exception {
    DynamicType.Unloaded<?> dynamicType = new ByteBuddy().redefine(Bar.class).constructor(any()).intercept(MethodCall.invoke(Object.class.getDeclaredConstructor())).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)), nullValue(Object.class));
}
Also used : Field(java.lang.reflect.Field) DynamicType(net.bytebuddy.dynamic.DynamicType) URLClassLoader(java.net.URLClassLoader) 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