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