Search in sources :

Example 11 with TextConstant

use of net.bytebuddy.implementation.bytecode.constant.TextConstant in project byte-buddy by raphw.

the class MethodDelegationChainedTest method testChainingNonVoid.

@Test
public void testChainingNonVoid() throws Exception {
    NonVoidInterceptor nonVoidInterceptor = new NonVoidInterceptor();
    DynamicType.Loaded<Foo> dynamicType = new ByteBuddy().subclass(Foo.class).method(isDeclaredBy(Foo.class)).intercept(MethodDelegation.withDefaultConfiguration().filter(isDeclaredBy(NonVoidInterceptor.class)).to(nonVoidInterceptor).andThen(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE))).make().load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is(FOO));
    assertThat(nonVoidInterceptor.intercepted, is(true));
}
Also used : TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 12 with TextConstant

use of net.bytebuddy.implementation.bytecode.constant.TextConstant in project byte-buddy by raphw.

the class MethodCallTest method testImplementationAppendingMethod.

@Test
public void testImplementationAppendingMethod() throws Exception {
    DynamicType.Loaded<MethodCallAppending> loaded = new ByteBuddy().subclass(MethodCallAppending.class).method(isDeclaredBy(MethodCallAppending.class)).intercept(MethodCall.invokeSuper().andThen(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE))).make().load(MethodCallAppending.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));
    MethodCallAppending instance = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(MethodCallAppending.class)));
    assertThat(instance, instanceOf(MethodCallAppending.class));
    assertThat(instance.foo(), is((Object) FOO));
    instance.assertOnlyCall(FOO);
}
Also used : TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 13 with TextConstant

use of net.bytebuddy.implementation.bytecode.constant.TextConstant in project byte-buddy by raphw.

the class SuperMethodCallOtherTest method testAndThen.

@Test
public void testAndThen() throws Exception {
    DynamicType.Loaded<Foo> loaded = new ByteBuddy().subclass(Foo.class).method(isDeclaredBy(Foo.class)).intercept(SuperMethodCall.INSTANCE.andThen(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE))).make().load(Foo.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    Foo foo = loaded.getLoaded().getDeclaredConstructor().newInstance();
    assertThat(foo.foo(), is(FOO));
    foo.assertOnlyCall(FOO);
}
Also used : TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Aggregations

TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)13 Test (org.junit.Test)13 ByteBuddy (net.bytebuddy.ByteBuddy)7 DynamicType (net.bytebuddy.dynamic.DynamicType)7 URLClassLoader (java.net.URLClassLoader)5 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)3 Type (java.lang.reflect.Type)2 GenericType (net.bytebuddy.test.scope.GenericType)2 TargetType (net.bytebuddy.dynamic.TargetType)1 Implementation (net.bytebuddy.implementation.Implementation)1 StubMethod (net.bytebuddy.implementation.StubMethod)1