use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapOfMethodsWithParametersPrimitive.
@Test
@JavaVersionRule.Enforce(value = 7, hotSpot = 7)
public void testBootstrapOfMethodsWithParametersPrimitive() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
Object value = new Object();
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(FOO, String.class).withBooleanValue(BOOLEAN).withByteValue(BYTE).withShortValue(SHORT).withCharacterValue(CHARACTER).withIntegerValue(INTEGER).withLongValue(LONG).withFloatValue(FLOAT).withDoubleValue(DOUBLE).withType(new TypeDescription.ForLoadedType(CLASS)).withEnumeration(new EnumerationDescription.ForLoadedEnumeration(makeEnum())).withInstance(JavaConstant.MethodType.ofLoaded(makeMethodType(CLASS)), JavaConstant.MethodHandle.ofLoaded(makeMethodHandle())).withValue(FOO, CLASS, makeEnum(), makeMethodType(CLASS), makeMethodHandle(), value)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is("" + BOOLEAN + BYTE + SHORT + CHARACTER + INTEGER + LONG + FLOAT + DOUBLE + CLASS + makeEnum() + makeMethodType(CLASS) + makeMethodHandle() + FOO + CLASS + makeEnum() + makeMethodType(CLASS) + makeMethodHandle() + value));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapWithNullValue.
@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapWithNullValue() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withNullValue(String.class)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(0));
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), nullValue(String.class));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapWithExplicitArgumentsWithoutArgumentsThrowsException.
@Test(expected = IllegalArgumentException.class)
@JavaVersionRule.Enforce(7)
public void testBootstrapWithExplicitArgumentsWithoutArgumentsThrowsException() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(PARAMETER_BOOTSTRAP));
InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP_EXPLICIT_ARGUMENTS)).getOnly()).withoutArguments();
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapWithFieldCreation.
@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapWithFieldCreation() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).defineField(FOO, String.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withField(FOO)).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.description.type.TypeDescription 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));
}
Aggregations