use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapFieldNotAssignable.
@Test(expected = IllegalStateException.class)
@JavaVersionRule.Enforce(7)
public void testBootstrapFieldNotAssignable() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
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)).make();
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapWithArrayArgumentsWithoutArguments.
@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapWithArrayArgumentsWithoutArguments() throws Exception {
Class<?> type = Class.forName(PARAMETER_BOOTSTRAP);
Field field = type.getDeclaredField(ARGUMENTS_FIELD_NAME);
field.set(null, null);
TypeDescription typeDescription = new TypeDescription.ForLoadedType(type);
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP_ARRAY_ARGUMENTS)).getOnly()).withoutArguments()).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is(FOO));
Object[] arguments = (Object[]) field.get(null);
assertThat(arguments.length, is(0));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapWithImplicitArgument.
@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapWithImplicitArgument() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
DynamicType.Loaded<SimpleWithArgument> dynamicType = new ByteBuddy().subclass(SimpleWithArgument.class).method(isDeclaredBy(SimpleWithArgument.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withMethodArguments()).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(0));
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(FOO), is(FOO));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapOfMethodsWithParametersWrapperConstantPool.
@Test
@JavaVersionRule.Enforce(value = 7, hotSpot = 7)
public void testBootstrapOfMethodsWithParametersWrapperConstantPool() 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(BAR, String.class).withValue(BOOLEAN, BYTE, SHORT, CHARACTER, INTEGER, LONG, FLOAT, DOUBLE, FOO, CLASS, makeEnum(), makeMethodType(CLASS), makeMethodHandle(), value)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(1));
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is("" + BOOLEAN + BYTE + SHORT + CHARACTER + INTEGER + LONG + FLOAT + DOUBLE + FOO + CLASS + makeEnum() + makeMethodType(CLASS) + makeMethodHandle() + value));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapWithExplicitArgumentsWithArguments.
@Test
@JavaVersionRule.Enforce(value = 7, hotSpot = 7)
public void testBootstrapWithExplicitArgumentsWithArguments() throws Exception {
Class<?> type = Class.forName(PARAMETER_BOOTSTRAP);
Field field = type.getDeclaredField(ARGUMENTS_FIELD_NAME);
field.set(null, null);
TypeDescription typeDescription = new TypeDescription.ForLoadedType(type);
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP_EXPLICIT_ARGUMENTS)).getOnly(), INTEGER, LONG, FLOAT, DOUBLE, FOO, CLASS, makeMethodType(CLASS), makeMethodHandle()).withoutArguments()).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is(FOO));
Object[] arguments = (Object[]) field.get(null);
assertThat(arguments.length, is(8));
assertThat(arguments[0], is((Object) INTEGER));
assertThat(arguments[1], is((Object) LONG));
assertThat(arguments[2], is((Object) FLOAT));
assertThat(arguments[3], is((Object) DOUBLE));
assertThat(arguments[4], is((Object) FOO));
assertThat(arguments[5], is((Object) CLASS));
assertThat(arguments[6], is(makeMethodType(CLASS)));
assertThat(JavaConstant.MethodHandle.ofLoaded(arguments[7]), is(JavaConstant.MethodHandle.ofLoaded(makeMethodHandle())));
}
Aggregations