Search in sources :

Example 36 with DynamicType

use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class InvokeDynamicTest method testBootstrapWithFieldUse.

@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapWithFieldUse() throws Exception {
    TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
    DynamicType.Loaded<SimpleWithField> dynamicType = new ByteBuddy().subclass(SimpleWithField.class).method(isDeclaredBy(SimpleWithField.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withField(FOO)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
    assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(0));
    SimpleWithField instance = dynamicType.getLoaded().getDeclaredConstructor().newInstance();
    Field field = SimpleWithField.class.getDeclaredField(FOO);
    field.setAccessible(true);
    field.set(instance, FOO);
    assertThat(instance.foo(), is(FOO));
}
Also used : Field(java.lang.reflect.Field) DynamicType(net.bytebuddy.dynamic.DynamicType) TypeDescription(net.bytebuddy.description.type.TypeDescription) ByteBuddy(net.bytebuddy.ByteBuddy) Test(org.junit.Test)

Example 37 with DynamicType

use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class AbstractMethodCallProxyTest method proxyOnlyDeclaredMethodOf.

protected Class<?> proxyOnlyDeclaredMethodOf(Class<?> proxyTarget) throws Exception {
    MethodDescription.InDefinedShape proxyMethod = new TypeDescription.ForLoadedType(proxyTarget).getDeclaredMethods().filter(not(isConstructor())).getOnly();
    when(methodAccessorFactory.registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT)).thenReturn(proxyMethod);
    String auxiliaryTypeName = getClass().getName() + "$" + proxyTarget.getSimpleName() + "$Proxy";
    DynamicType dynamicType = new MethodCallProxy(specialMethodInvocation, false).make(auxiliaryTypeName, ClassFileVersion.ofThisVm(), methodAccessorFactory);
    DynamicType.Unloaded<?> unloaded = (DynamicType.Unloaded<?>) dynamicType;
    Class<?> auxiliaryType = unloaded.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
    assertThat(auxiliaryType.getName(), is(auxiliaryTypeName));
    verify(methodAccessorFactory).registerAccessorFor(specialMethodInvocation, MethodAccessorFactory.AccessType.DEFAULT);
    verifyNoMoreInteractions(methodAccessorFactory);
    verifyZeroInteractions(specialMethodInvocation);
    assertThat(auxiliaryType.getModifiers(), is(Opcodes.ACC_SYNTHETIC));
    assertThat(Callable.class.isAssignableFrom(auxiliaryType), is(true));
    assertThat(Runnable.class.isAssignableFrom(auxiliaryType), is(true));
    assertThat(auxiliaryType.getDeclaredConstructors().length, is(1));
    assertThat(auxiliaryType.getDeclaredMethods().length, is(2));
    assertThat(auxiliaryType.getDeclaredFields().length, is(proxyMethod.getParameters().size() + (proxyMethod.isStatic() ? 0 : 1)));
    int fieldIndex = 0;
    if (!proxyMethod.isStatic()) {
        assertThat(auxiliaryType.getDeclaredFields()[fieldIndex++].getType(), CoreMatchers.<Class<?>>is(proxyTarget));
    }
    for (Class<?> parameterType : proxyTarget.getDeclaredMethods()[0].getParameterTypes()) {
        assertThat(auxiliaryType.getDeclaredFields()[fieldIndex++].getType(), CoreMatchers.<Class<?>>is(parameterType));
    }
    return auxiliaryType;
}
Also used : MethodDescription(net.bytebuddy.description.method.MethodDescription) DynamicType(net.bytebuddy.dynamic.DynamicType) Callable(java.util.concurrent.Callable) TypeDescription(net.bytebuddy.description.type.TypeDescription)

Example 38 with DynamicType

use of net.bytebuddy.dynamic.DynamicType in project byte-buddy by raphw.

the class TrivialTypeTest method testEager.

@Test
public void testEager() throws Exception {
    when(classFileVersion.getMinorMajorVersion()).thenReturn(ClassFileVersion.JAVA_V5.getMinorMajorVersion());
    DynamicType dynamicType = TrivialType.SIGNATURE_RELEVANT.make(FOO, classFileVersion, methodAccessorFactory);
    assertThat(dynamicType.getTypeDescription().getName(), is(FOO));
    assertThat(dynamicType.getTypeDescription().getModifiers(), is(Opcodes.ACC_SYNTHETIC));
    assertThat(dynamicType.getTypeDescription().getDeclaredAnnotations().size(), is(1));
    assertThat(dynamicType.getTypeDescription().getDeclaredAnnotations().isAnnotationPresent(AuxiliaryType.SignatureRelevant.class), is(true));
    assertThat(dynamicType.getAuxiliaryTypes().size(), is(0));
    assertThat(dynamicType.getLoadedTypeInitializers().get(dynamicType.getTypeDescription()).isAlive(), is(false));
}
Also used : DynamicType(net.bytebuddy.dynamic.DynamicType) Test(org.junit.Test)

Aggregations

DynamicType (net.bytebuddy.dynamic.DynamicType)38 Test (org.junit.Test)33 ByteBuddy (net.bytebuddy.ByteBuddy)30 TypeDescription (net.bytebuddy.description.type.TypeDescription)23 Field (java.lang.reflect.Field)10 URLClassLoader (java.net.URLClassLoader)7 Type (java.lang.reflect.Type)5 AbstractDynamicTypeBuilderTest (net.bytebuddy.dynamic.AbstractDynamicTypeBuilderTest)5 GenericType (net.bytebuddy.test.scope.GenericType)5 TextConstant (net.bytebuddy.implementation.bytecode.constant.TextConstant)4 RandomString (net.bytebuddy.utility.RandomString)4 TargetType (net.bytebuddy.dynamic.TargetType)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 Callable (java.util.concurrent.Callable)2 Plugin (net.bytebuddy.build.Plugin)2 MethodDescription (net.bytebuddy.description.method.MethodDescription)2 LoadedTypeInitializer (net.bytebuddy.implementation.LoadedTypeInitializer)2 StubMethod (net.bytebuddy.implementation.StubMethod)2