use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class RedefinitionDynamicTypeBuilderTest method testObjectProperties.
@Test
@SuppressWarnings("unchecked")
public void testObjectProperties() throws Exception {
ObjectPropertyAssertion.of(RedefinitionDynamicTypeBuilder.class).create(new ObjectPropertyAssertion.Creator<List<?>>() {
@Override
public List<?> create() {
TypeDescription typeDescription = mock(TypeDescription.class);
when(typeDescription.asErasure()).thenReturn(typeDescription);
return Collections.singletonList(typeDescription);
}
}).create(new ObjectPropertyAssertion.Creator<TypeDescription>() {
@Override
public TypeDescription create() {
TypeDescription rawTypeDescription = mock(TypeDescription.class);
when(rawTypeDescription.getDeclaredAnnotations()).thenReturn(new AnnotationList.Empty());
when(rawTypeDescription.getTypeVariables()).thenReturn(new TypeList.Generic.Empty());
TypeDescription.Generic typeDescription = mock(TypeDescription.Generic.class);
when(typeDescription.asErasure()).thenReturn(rawTypeDescription);
when(typeDescription.asGenericType()).thenReturn(typeDescription);
when(rawTypeDescription.getInterfaces()).thenReturn(new TypeList.Generic.Explicit(typeDescription));
when(rawTypeDescription.getDeclaredFields()).thenReturn(new FieldList.Empty<FieldDescription.InDefinedShape>());
when(rawTypeDescription.getDeclaredMethods()).thenReturn(new MethodList.Empty<MethodDescription.InDefinedShape>());
return rawTypeDescription;
}
}).apply();
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class ImplementationSpecialMethodInvocationSimpleTest method testEquality.
@Test
public void testEquality() throws Exception {
MethodDescription firstMethod = mock(MethodDescription.class), secondMethod = mock(MethodDescription.class);
MethodDescription.SignatureToken firstToken = mock(MethodDescription.SignatureToken.class), secondToken = mock(MethodDescription.SignatureToken.class);
when(firstMethod.asSignatureToken()).thenReturn(firstToken);
when(secondMethod.asSignatureToken()).thenReturn(secondToken);
TypeDescription firstType = mock(TypeDescription.class), secondType = mock(TypeDescription.class);
assertThat(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class)), is(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class))));
assertThat(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class)), not(new Implementation.SpecialMethodInvocation.Simple(secondMethod, firstType, mock(StackManipulation.class))));
assertThat(new Implementation.SpecialMethodInvocation.Simple(firstMethod, firstType, mock(StackManipulation.class)), not(new Implementation.SpecialMethodInvocation.Simple(firstMethod, secondType, mock(StackManipulation.class))));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testChainedInvocation.
@Test
@JavaVersionRule.Enforce(7)
public void testChainedInvocation() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
DynamicType.Loaded<SimpleWithArgument> dynamicType = new ByteBuddy().subclass(SimpleWithArgument.class).method(isDeclaredBy(SimpleWithArgument.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withArgument(0).andThen(FixedValue.value(BAZ))).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(0));
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(FOO), is(BAZ));
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testArgumentCannotAssignIllegalInstanceType.
@Test(expected = IllegalArgumentException.class)
@JavaVersionRule.Enforce(7)
public void testArgumentCannotAssignIllegalInstanceType() throws Exception {
Class<?> type = Class.forName(ARGUMENT_BOOTSTRAP);
TypeDescription typeDescription = new TypeDescription.ForLoadedType(type);
InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(QUX, String.class).withReference(new Object()).as(String.class);
}
use of net.bytebuddy.description.type.TypeDescription in project byte-buddy by raphw.
the class InvokeDynamicTest method testBootstrapOfMethodsWithParametersWrapperReference.
@Test
@JavaVersionRule.Enforce(7)
public void testBootstrapOfMethodsWithParametersWrapperReference() throws Exception {
TypeDescription typeDescription = new TypeDescription.ForLoadedType(Class.forName(ARGUMENT_BOOTSTRAP));
Object value = new Object();
DynamicType.Loaded<Simple> dynamicType = new ByteBuddy().subclass(Simple.class).method(isDeclaredBy(Simple.class)).intercept(InvokeDynamic.bootstrap(typeDescription.getDeclaredMethods().filter(named(BOOTSTRAP)).getOnly()).invoke(BAR, String.class).withReference(BOOLEAN, BYTE, SHORT, CHARACTER, INTEGER, LONG, FLOAT, DOUBLE, FOO, CLASS, makeEnum(), makeMethodType(CLASS)).withReference(makeMethodHandle()).as(// avoid direct method handle
JavaType.METHOD_HANDLE.load()).withReference(value)).make().load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER);
assertThat(dynamicType.getLoaded().getDeclaredFields().length, is(14));
assertThat(dynamicType.getLoaded().getDeclaredConstructor().newInstance().foo(), is("" + BOOLEAN + BYTE + SHORT + CHARACTER + INTEGER + LONG + FLOAT + DOUBLE + FOO + CLASS + makeEnum() + makeMethodType(CLASS) + makeMethodHandle() + value));
}
Aggregations