Search in sources :

Example 6 with TextConstant

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

the class AbstractDynamicTypeBuilderForInliningTest method testMethodTransformationExistingMethod.

@Test
public void testMethodTransformationExistingMethod() throws Exception {
    Class<?> type = create(Transform.class).method(named(FOO)).intercept(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE)).transform(Transformer.ForMethod.withModifiers(MethodManifestation.FINAL)).make().load(new URLClassLoader(new URL[0], null), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    Method foo = type.getDeclaredMethod(FOO);
    assertThat(foo.invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
    assertThat(foo.getModifiers(), is(Opcodes.ACC_FINAL | Opcodes.ACC_PUBLIC));
}
Also used : TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) URLClassLoader(java.net.URLClassLoader) StubMethod(net.bytebuddy.implementation.StubMethod) Implementation(net.bytebuddy.implementation.Implementation) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 7 with TextConstant

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

the class AbstractDynamicTypeBuilderForInliningTest method testBridgeMethodCreation.

@Test
@SuppressWarnings("unchecked")
public void testBridgeMethodCreation() throws Exception {
    Class<?> dynamicType = create(BridgeRetention.Inner.class).method(named(FOO)).intercept(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST).getLoaded();
    assertEquals(String.class, dynamicType.getDeclaredMethod(FOO).getReturnType());
    assertThat(dynamicType.getDeclaredMethod(FOO).getGenericReturnType(), is((Type) String.class));
    BridgeRetention<String> bridgeRetention = (BridgeRetention<String>) dynamicType.getDeclaredConstructor().newInstance();
    assertThat(bridgeRetention.foo(), is(FOO));
    bridgeRetention.assertZeroCalls();
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) Type(java.lang.reflect.Type) GenericType(net.bytebuddy.test.scope.GenericType) TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 8 with TextConstant

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

the class RedefinitionDynamicTypeBuilderTest method testDefaultInterfaceSubInterface.

@Test
@JavaVersionRule.Enforce(8)
public void testDefaultInterfaceSubInterface() throws Exception {
    Class<?> interfaceType = Class.forName(DEFAULT_METHOD_INTERFACE);
    Class<?> dynamicInterfaceType = new ByteBuddy().redefine(interfaceType).method(named(FOO)).intercept(new Implementation.Simple(new TextConstant(BAR), MethodReturn.REFERENCE)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.CHILD_FIRST).getLoaded();
    Class<?> dynamicClassType = new ByteBuddy().subclass(dynamicInterfaceType).make().load(dynamicInterfaceType.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(dynamicClassType.getMethod(FOO).invoke(dynamicClassType.getDeclaredConstructor().newInstance()), is((Object) BAR));
    assertThat(dynamicInterfaceType.getDeclaredMethods().length, is(1));
    assertThat(dynamicClassType.getDeclaredMethods().length, is(0));
}
Also used : TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 9 with TextConstant

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

the class MethodCallTest method testImplementationAppendingConstructor.

@Test
public void testImplementationAppendingConstructor() throws Exception {
    DynamicType.Loaded<MethodCallAppending> loaded = new ByteBuddy().subclass(MethodCallAppending.class).method(isDeclaredBy(MethodCallAppending.class)).intercept(MethodCall.construct(Object.class.getDeclaredConstructor()).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.assertZeroCalls();
}
Also used : TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) DynamicType(net.bytebuddy.dynamic.DynamicType) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 10 with TextConstant

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

the class MethodDelegationChainedTest method testChainingVoid.

@Test
public void testChainingVoid() throws Exception {
    VoidInterceptor voidInterceptor = new VoidInterceptor();
    DynamicType.Loaded<Foo> dynamicType = new ByteBuddy().subclass(Foo.class).method(isDeclaredBy(Foo.class)).intercept(MethodDelegation.withDefaultConfiguration().filter(isDeclaredBy(VoidInterceptor.class)).to(voidInterceptor).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(voidInterceptor.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)

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