Search in sources :

Example 91 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class MethodCallTest method testSuperInvocation.

@Test
public void testSuperInvocation() throws Exception {
    DynamicType.Loaded<SuperMethodInvocation> loaded = new ByteBuddy().subclass(SuperMethodInvocation.class).method(takesArguments(0).and(named(FOO))).intercept(MethodCall.invokeSuper()).make().load(SuperMethodInvocation.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredMethod(FOO), not(nullValue(Method.class)));
    assertThat(loaded.getLoaded().getDeclaredConstructors().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
    SuperMethodInvocation instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(SuperMethodInvocation.class)));
    assertThat(instance, instanceOf(SuperMethodInvocation.class));
    assertThat(instance.foo(), is(FOO));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 92 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class MethodCallTest method testInvokeOnArgumentDynamic.

@Test
public void testInvokeOnArgumentDynamic() throws Exception {
    DynamicType.Loaded<ArgumentCallDynamic> loaded = new ByteBuddy().subclass(ArgumentCallDynamic.class).method(isDeclaredBy(ArgumentCallDynamic.class)).intercept(MethodCall.invoke(ArgumentCallDynamic.Target.class.getDeclaredMethod("foo")).onArgument(0).withAssigner(Assigner.DEFAULT, Assigner.Typing.DYNAMIC)).make().load(ArgumentCallDynamic.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(loaded.getLoadedAuxiliaryTypes().size(), is(0));
    assertThat(loaded.getLoaded().getDeclaredMethods().length, is(1));
    assertThat(loaded.getLoaded().getDeclaredFields().length, is(0));
    ArgumentCallDynamic instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.foo(new ArgumentCallDynamic.Target(BAR)), is(BAR));
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(InstanceMethod.class)));
    assertThat(instance, instanceOf(ArgumentCallDynamic.class));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 93 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class MethodDelegationDefaultCallTest method testSerializableProxy.

@Test
@JavaVersionRule.Enforce(8)
public void testSerializableProxy() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy().subclass(Object.class).implement(Class.forName(SINGLE_DEFAULT_METHOD)).intercept(MethodDelegation.to(SerializationCheck.class)).make().load(Class.forName(SINGLE_DEFAULT_METHOD).getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    Method method = loaded.getLoaded().getMethod(FOO);
    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 94 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class MethodDelegationDefaultCallTest method testExplicitDefaultCallToOtherInterface.

@Test
@JavaVersionRule.Enforce(8)
public void testExplicitDefaultCallToOtherInterface() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy().subclass(Object.class).implement(Class.forName(SINGLE_DEFAULT_METHOD), Class.forName(CONFLICTING_INTERFACE)).intercept(MethodDelegation.to(Class.forName(CONFLICTING_PREFERRING_INTERCEPTOR))).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    Method method = loaded.getLoaded().getMethod(FOO);
    assertThat(method.invoke(instance), is((Object) QUX));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 95 with ByteBuddy

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy in project byte-buddy by raphw.

the class MethodDelegationDefaultMethodTest method testCallableDefaultCall.

@Test
@JavaVersionRule.Enforce(8)
public void testCallableDefaultCall() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy().subclass(Object.class).implement(Class.forName(SINGLE_DEFAULT_METHOD)).intercept(MethodDelegation.to(SampleClass.class)).make().load(Class.forName(SINGLE_DEFAULT_METHOD).getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    Method method = loaded.getLoaded().getMethod(FOO);
    assertThat(method.invoke(instance), is((Object) FOO));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) DefaultMethod(net.bytebuddy.implementation.bind.annotation.DefaultMethod) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

ByteBuddy (net.bytebuddy.ByteBuddy)384 Test (org.junit.Test)361 DynamicType (net.bytebuddy.dynamic.DynamicType)190 Method (java.lang.reflect.Method)36 TypeDescription (net.bytebuddy.description.type.TypeDescription)25 InvocationTargetException (java.lang.reflect.InvocationTargetException)21 Field (java.lang.reflect.Field)18 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)17 URLClassLoader (java.net.URLClassLoader)15 Instrumentation (java.lang.instrument.Instrumentation)10 StubMethod (net.bytebuddy.implementation.StubMethod)8 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)7 ClassFileLocator (net.bytebuddy.dynamic.ClassFileLocator)6 File (java.io.File)5 ClassFileTransformer (java.lang.instrument.ClassFileTransformer)5 FieldDescription (net.bytebuddy.description.field.FieldDescription)5 ByteArrayClassLoader (net.bytebuddy.dynamic.loading.ByteArrayClassLoader)5 RandomString (net.bytebuddy.utility.RandomString)5 Constructor (java.lang.reflect.Constructor)4 Callable (java.util.concurrent.Callable)4