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));
}
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));
}
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));
}
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();
}
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));
}
Aggregations