Search in sources :

Example 16 with CtTypeParameterReference

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());
    }
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference)

Example 17 with CtTypeParameterReference

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;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference)

Example 18 with CtTypeParameterReference

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());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) Tacos(spoon.test.generics.testclasses.Tacos) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 19 with CtTypeParameterReference

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);
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtInvocation(spoon.reflect.code.CtInvocation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 20 with CtTypeParameterReference

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());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) ArrayList(java.util.ArrayList) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Aggregations

CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)32 CtTypeReference (spoon.reflect.reference.CtTypeReference)11 Test (org.junit.Test)10 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)10 ArrayList (java.util.ArrayList)6 Factory (spoon.reflect.factory.Factory)4 Launcher (spoon.Launcher)3 SpoonException (spoon.SpoonException)3 CtWildcardReference (spoon.reflect.reference.CtWildcardReference)3 Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)2 TypeVariableBinding (org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding)2 WildcardBinding (org.eclipse.jdt.internal.compiler.lookup.WildcardBinding)2 CtMethod (spoon.reflect.declaration.CtMethod)2 CtType (spoon.reflect.declaration.CtType)2 CtArrayTypeReference (spoon.reflect.reference.CtArrayTypeReference)2 CtIntersectionTypeReference (spoon.reflect.reference.CtIntersectionTypeReference)2 CtScanner (spoon.reflect.visitor.CtScanner)2 MainTest (spoon.test.main.MainTest)2 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)2 List (java.util.List)1