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