Search in sources :

Example 96 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 testExplicitDefaultCall.

@Test
@JavaVersionRule.Enforce(8)
public void testExplicitDefaultCall() 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(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) 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)

Example 97 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 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) DefaultMethod(net.bytebuddy.implementation.bind.annotation.DefaultMethod) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 98 with ByteBuddy

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

the class MethodDelegationDefaultTest method testDefaultInterface.

@Test
@JavaVersionRule.Enforce(8)
public void testDefaultInterface() throws Exception {
    DynamicType.Loaded<?> loaded = new ByteBuddy().subclass(Object.class).implement(Class.forName(DEFAULT_INTERFACE)).intercept(MethodDelegation.to(Class.forName(DELEGATION_TARGET))).make().load(Class.forName(DEFAULT_INTERFACE).getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Object instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass().getDeclaredMethod(FOO).invoke(instance), is((Object) (FOO + BAR)));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 99 with ByteBuddy

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

the class MethodDelegationFieldProxyTest method testIncompatibleTypeThrowsExceptionSetDuplex.

@Test(expected = ClassCastException.class)
public void testIncompatibleTypeThrowsExceptionSetDuplex() throws Exception {
    DynamicType.Loaded<Explicit> loaded = new ByteBuddy().subclass(Explicit.class).method(isDeclaredBy(Explicit.class)).intercept(MethodDelegation.withDefaultConfiguration().withBinders(FieldProxy.Binder.install(GetSet.class)).to(SetterIncompatibleDuplex.class)).make().load(Explicit.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Explicit explicit = loaded.getLoaded().getDeclaredConstructor().newInstance();
    explicit.swap();
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 100 with ByteBuddy

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

the class MethodDelegationFieldProxyTest method testExplicitFieldAccessStatic.

@Test
public void testExplicitFieldAccessStatic() throws Exception {
    DynamicType.Loaded<ExplicitStatic> loaded = new ByteBuddy().subclass(ExplicitStatic.class).method(isDeclaredBy(ExplicitStatic.class)).intercept(MethodDelegation.withDefaultConfiguration().withBinders(FieldProxy.Binder.install(Get.class, Set.class)).to(Swap.class)).make().load(ExplicitStatic.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    ExplicitStatic explicit = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(ExplicitStatic.foo, is(FOO));
    explicit.swap();
    assertThat(ExplicitStatic.foo, is(FOO + BAR));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) 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