use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class DefaultJavaPrettyPrinter method visitCtTypeParameter.
@Override
public void visitCtTypeParameter(CtTypeParameter typeParameter) {
CtTypeParameterReference ref = typeParameter.getReference();
if (ref.isImplicit()) {
return;
}
elementPrinterHelper.writeAnnotations(ref);
if (printQualified(ref)) {
elementPrinterHelper.writeQualifiedName(ref.getQualifiedName());
} else {
printer.writeIdentifier(ref.getSimpleName());
}
if (!ref.isDefaultBoundingType() || !ref.getBoundingType().isImplicit()) {
if (ref.isUpper()) {
printer.writeSpace().writeKeyword("extends").writeSpace();
} else {
printer.writeSpace().writeKeyword("super").writeSpace();
}
scan(ref.getBoundingType());
}
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class TypeFactory method createReference.
/**
* Create a reference to a simple type
*/
public CtTypeParameterReference createReference(CtTypeParameter type) {
CtTypeParameterReference ref = factory.Core().createTypeParameterReference();
if (type.getSuperclass() != null) {
ref.setBoundingType(type.getSuperclass().clone());
}
for (CtAnnotation<? extends Annotation> ctAnnotation : type.getAnnotations()) {
ref.addAnnotation(ctAnnotation.clone());
}
ref.setSimpleName(type.getSimpleName());
ref.setParent(type);
return ref;
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class GenericsTest method testTypeParameterReferenceAsActualTypeArgument.
@Test
public void testTypeParameterReferenceAsActualTypeArgument() throws Exception {
CtType<Tacos> aTacos = buildNoClasspath(ClassThatDefinesANewTypeArgument.class).Type().get(ClassThatDefinesANewTypeArgument.class);
CtTypeReference<?> typeRef = aTacos.getReference();
assertSame(aTacos, typeRef.getDeclaration());
CtTypeParameter typeParam = aTacos.getFormalCtTypeParameters().get(0);
CtTypeParameterReference typeParamRef = typeParam.getReference();
assertSame(typeParam, typeParamRef.getDeclaration());
assertEquals("spoon.test.generics.ClassThatDefinesANewTypeArgument", typeRef.toString());
// creating a reference to "ClassThatDefinesANewTypeArgument<T>"
// this assignment changes parent of typeParamRef to TYPEREF
typeRef.addActualTypeArgument(typeParamRef);
assertEquals("spoon.test.generics.ClassThatDefinesANewTypeArgument<T>", typeRef.toString());
// this does not change the declaration
assertSame(aTacos, typeRef.getDeclaration());
// stored typeParamRef is same like the added one, no clone - OK
assertSame(typeParamRef, typeRef.getActualTypeArguments().get(0));
// typeParamRef has got new parent
assertSame(typeRef, typeParamRef.getParent());
// null because without context
assertEquals(null, typeParamRef.getDeclaration());
assertEquals(typeParam, typeParamRef.getTypeParameterDeclaration());
typeParamRef.setSimpleName("Y");
assertEquals(typeParam, typeParamRef.getTypeParameterDeclaration());
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class TypeReferenceTest method testRecursiveTypeReference.
@Test
public void testRecursiveTypeReference() throws Exception {
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/reference/testclasses/Tacos.java");
launcher.setSourceOutputDirectory("./target/spoon-test");
launcher.run();
final CtInvocation<?> inv = Query.getElements(launcher.getFactory(), new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return !element.getExecutable().isConstructor() && super.matches(element);
}
}).get(0);
assertNotNull(inv.getExecutable());
final CtTypeReference<?> returnType = inv.getExecutable().getType();
assertNotNull(returnType);
assertEquals(1, returnType.getActualTypeArguments().size());
final CtTypeParameterReference genericType = (CtTypeParameterReference) returnType.getActualTypeArguments().get(0);
assertNotNull(genericType);
assertNotNull(genericType.getBoundingType());
CtTypeReference<?> extendsGeneric = genericType.getBoundingType();
assertNotNull(extendsGeneric);
assertEquals(1, extendsGeneric.getActualTypeArguments().size());
CtTypeParameterReference genericExtends = (CtTypeParameterReference) extendsGeneric.getActualTypeArguments().get(0);
assertNotNull(genericExtends);
assertNotNull(genericExtends.getBoundingType());
assertTrue(genericExtends.getBoundingType() instanceof CtTypeReference);
}
use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.
the class TypeReferenceTest method testImproveAPIActualTypeReference.
@Test
public void testImproveAPIActualTypeReference() throws Exception {
final Factory factory = createFactory();
List<CtTypeParameterReference> typeParameterReferences = new ArrayList<>();
typeParameterReferences.add(factory.Type().createTypeParameterReference("Foo"));
final CtTypeReference<Object> typeReference = factory.Core().createTypeReference();
typeReference.setActualTypeArguments(typeParameterReferences);
assertEquals(1, typeReference.getActualTypeArguments().size());
}
Aggregations