Search in sources :

Example 1 with TextConstant

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

the class AbstractDynamicTypeBuilderTest method testIgnoredMethod.

@Test
public void testIgnoredMethod() throws Exception {
    Class<?> type = createPlain().ignoreAlso(named(TO_STRING)).method(named(TO_STRING)).intercept(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE)).make().load(new URLClassLoader(new URL[0], null), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredConstructor().newInstance().toString(), CoreMatchers.not(FOO));
}
Also used : TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 2 with TextConstant

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

the class AbstractDynamicTypeBuilderTest method testMethodDefinition.

@Test
public void testMethodDefinition() throws Exception {
    Class<?> type = createPlain().defineMethod(FOO, Object.class, Visibility.PUBLIC).throwing(Exception.class).intercept(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE)).make().load(new URLClassLoader(new URL[0], null), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    Method method = type.getDeclaredMethod(FOO);
    assertThat(method.getReturnType(), CoreMatchers.<Class<?>>is(Object.class));
    assertThat(method.getExceptionTypes(), is(new Class<?>[] { Exception.class }));
    assertThat(method.getModifiers(), is(Modifier.PUBLIC));
    assertThat(method.invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
}
Also used : TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 3 with TextConstant

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

the class AbstractDynamicTypeBuilderTest method testIgnoredMethodDoesNotApplyForDefined.

@Test
public void testIgnoredMethodDoesNotApplyForDefined() throws Exception {
    Class<?> type = createPlain().ignoreAlso(named(FOO)).defineMethod(FOO, String.class, Visibility.PUBLIC).intercept(new Implementation.Simple(new TextConstant(FOO), MethodReturn.REFERENCE)).make().load(new URLClassLoader(new URL[0], null), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(type.getDeclaredMethod(FOO).invoke(type.getDeclaredConstructor().newInstance()), is((Object) FOO));
}
Also used : TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Example 4 with TextConstant

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

the class SubclassDynamicTypeBuilderTest method testBridgeMethodCreation.

@Test
@SuppressWarnings("unchecked")
public void testBridgeMethodCreation() throws Exception {
    Class<?> dynamicType = new ByteBuddy().subclass(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) GenericType(net.bytebuddy.test.scope.GenericType) Type(java.lang.reflect.Type) TargetType(net.bytebuddy.dynamic.TargetType) TextConstant(net.bytebuddy.implementation.bytecode.constant.TextConstant) ByteBuddy(net.bytebuddy.ByteBuddy) AbstractDynamicTypeBuilderTest(net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest) Test(org.junit.Test)

Example 5 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)

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