Search in sources :

Example 96 with ByteBuddy

use of net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class SuperMethodCallOtherTest method testInheritedDefaultMethod.

@Test
@JavaVersionRule.Enforce(8)
public void testInheritedDefaultMethod() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy().subclass(Class.forName(SINGLE_DEFAULT_METHOD_CLASS)).method(not(isDeclaredBy(Object.class))).intercept(SuperMethodCall.INSTANCE).make().load(Class.forName(SINGLE_DEFAULT_METHOD_CLASS).getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    Method method = loaded.getLoaded().getDeclaredMethod(FOO);
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(method.invoke(instance), is((Object) FOO));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 97 with ByteBuddy

use of net.bytebuddy.ByteBuddy 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();
}
Also used : TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 98 with ByteBuddy

use of net.bytebuddy.ByteBuddy 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));
}
Also used : Field(java.lang.reflect.Field) DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 99 with ByteBuddy

use of net.bytebuddy.ByteBuddy 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));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 100 with ByteBuddy

use of net.bytebuddy.ByteBuddy 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));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Aggregations

ByteBuddy (net.bytebuddy.ByteBuddy)369 Test (org.junit.Test)355 DynamicType (net.bytebuddy.dynamic.DynamicType)188 Method (java.lang.reflect.Method)34 TypeDescription (net.bytebuddy.description.type.TypeDescription)26 InvocationTargetException (java.lang.reflect.InvocationTargetException)19 Field (java.lang.reflect.Field)18 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)17 URLClassLoader (java.net.URLClassLoader)14 Instrumentation (java.lang.instrument.Instrumentation)10 StubMethod (net.bytebuddy.implementation.StubMethod)7 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)7 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)6 RandomString (net.bytebuddy.utility.RandomString)6 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)5 MethodDescription (net.bytebuddy.description.method.MethodDescription)5 File (java.io.File)4 Constructor (java.lang.reflect.Constructor)4 FieldDescription (net.bytebuddy.description.field.FieldDescription)4 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)4