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