use of 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();
}
use of 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();
}
use of 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));
}
use of 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));
}
use of 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));
}
Aggregations