Search in sources :

Example 16 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class CtMethodImpl method setFormalCtTypeParameters.

@Override
public <C extends CtFormalTypeDeclarer> C setFormalCtTypeParameters(List<CtTypeParameter> formalTypeParameters) {
    getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, TYPE_PARAMETER, this.formalCtTypeParameters, new ArrayList<>(this.formalCtTypeParameters));
    if (formalTypeParameters == null || formalTypeParameters.isEmpty()) {
        this.formalCtTypeParameters = CtElementImpl.emptyList();
        return (C) this;
    }
    if (this.formalCtTypeParameters == CtElementImpl.<CtTypeParameter>emptyList()) {
        this.formalCtTypeParameters = new ArrayList<>(TYPE_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
    }
    this.formalCtTypeParameters.clear();
    for (CtTypeParameter formalTypeParameter : formalTypeParameters) {
        addFormalCtTypeParameter(formalTypeParameter);
    }
    return (C) this;
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter)

Example 17 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class CtConstructorImpl method setFormalCtTypeParameters.

@Override
public <C extends CtFormalTypeDeclarer> C setFormalCtTypeParameters(List<CtTypeParameter> formalTypeParameters) {
    if (formalTypeParameters == null || formalTypeParameters.isEmpty()) {
        this.formalCtTypeParameters = CtElementImpl.emptyList();
        return (C) this;
    }
    if (this.formalCtTypeParameters == CtElementImpl.<CtTypeParameter>emptyList()) {
        this.formalCtTypeParameters = new ArrayList<>(TYPE_TYPE_PARAMETERS_CONTAINER_DEFAULT_CAPACITY);
    }
    getFactory().getEnvironment().getModelChangeListener().onListDeleteAll(this, TYPE_PARAMETER, this.formalCtTypeParameters, new ArrayList<>(this.formalCtTypeParameters));
    this.formalCtTypeParameters.clear();
    for (CtTypeParameter formalTypeParameter : formalTypeParameters) {
        addFormalCtTypeParameter(formalTypeParameter);
    }
    return (C) this;
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter)

Example 18 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class ClassTypingContext method resolveTypeParameters.

/**
 * resolve typeRefs declared in scope of declarer using actual type arguments registered in typeScopeToActualTypeArguments
 * @param typeRefs to be resolved type references
 * @return resolved type references - one for each `typeRefs`
 * @throws SpoonException if they cannot be resolved. It should not normally happen. If it happens then spoon AST model is probably not consistent.
 */
private List<CtTypeReference<?>> resolveTypeParameters(List<CtTypeReference<?>> typeRefs) {
    List<CtTypeReference<?>> result = new ArrayList<>(typeRefs.size());
    for (CtTypeReference<?> typeRef : typeRefs) {
        if (typeRef instanceof CtTypeParameterReference) {
            CtTypeParameterReference typeParamRef = (CtTypeParameterReference) typeRef;
            CtTypeParameter typeParam = typeParamRef.getDeclaration();
            CtFormalTypeDeclarer declarer = typeParam.getTypeParameterDeclarer();
            typeRef = resolveTypeParameter(declarer, typeParamRef, typeParam, typeRef);
        }
        result.add(typeRef);
    }
    return result;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtFormalTypeDeclarer(spoon.reflect.declaration.CtFormalTypeDeclarer) ArrayList(java.util.ArrayList)

Example 19 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class JavaReflectionTreeBuilderTest method testScannerGenericsInClass.

@Test
public void testScannerGenericsInClass() throws Exception {
    final CtType<ComparableComparatorBug> aType = new JavaReflectionTreeBuilder(createFactory()).scan(ComparableComparatorBug.class);
    assertNotNull(aType);
    // New type parameter declaration.
    assertEquals(1, aType.getFormalCtTypeParameters().size());
    CtTypeParameter ctTypeParameter = aType.getFormalCtTypeParameters().get(0);
    assertEquals("E extends java.lang.Comparable<? super E>", ctTypeParameter.toString());
    assertEquals(1, ctTypeParameter.getSuperclass().getActualTypeArguments().size());
    assertTrue(ctTypeParameter.getSuperclass().getActualTypeArguments().get(0) instanceof CtTypeParameterReference);
    assertEquals("? super E", ctTypeParameter.getSuperclass().getActualTypeArguments().get(0).toString());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) ComparableComparatorBug(spoon.test.generics.ComparableComparatorBug) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) Test(org.junit.Test)

Example 20 with CtTypeParameter

use of spoon.reflect.declaration.CtTypeParameter in project spoon by INRIA.

the class AnnotationTest method testUsageOfTypeAnnotationWithGenericTypesInClassDeclaration.

@Test
public void testUsageOfTypeAnnotationWithGenericTypesInClassDeclaration() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/annotation/testclasses/AnnotationsAppliedOnAnyTypeInAClass.java");
    launcher.buildModel();
    Factory factory = launcher.getFactory();
    final CtClass<?> ctClass = (CtClass<?>) factory.Type().get("spoon.test.annotation.testclasses.AnnotationsAppliedOnAnyTypeInAClass");
    final CtClass<?> genericClass = ctClass.getElements(new NamedElementFilter<>(CtClass.class, "DummyGenericClass")).get(0);
    // New type parameter declaration.
    final List<CtTypeParameter> typeParameters = genericClass.getFormalCtTypeParameters();
    assertEquals("Generic class has 2 generics parameters.", 2, typeParameters.size());
    assertEquals("First generic type must have type annotation", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T", typeParameters.get(0).toString());
    assertEquals("Second generic type must have type annotation", "@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "K", typeParameters.get(1).toString());
    final CtTypeReference<?> superInterface = genericClass.getSuperInterfaces().toArray(new CtTypeReference<?>[0])[0];
    final String expected = "spoon.test.annotation.testclasses.BasicAnnotation<@spoon.test.annotation.testclasses.TypeAnnotation" + System.lineSeparator() + "T>";
    assertEquals("Super interface has a generic type with type annotation", expected, superInterface.toString());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtTypeReference(spoon.reflect.reference.CtTypeReference) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Aggregations

CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)43 Test (org.junit.Test)25 MainTest (spoon.test.main.MainTest)12 Factory (spoon.reflect.factory.Factory)10 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)10 Launcher (spoon.Launcher)9 CtClass (spoon.reflect.declaration.CtClass)9 CtTypeReference (spoon.reflect.reference.CtTypeReference)8 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)7 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)7 ArrayList (java.util.ArrayList)6 CtMethod (spoon.reflect.declaration.CtMethod)6 CtType (spoon.reflect.declaration.CtType)5 LikeCtClass (spoon.test.generics.testclasses2.LikeCtClass)4 SpoonException (spoon.SpoonException)3 CtFormalTypeDeclarer (spoon.reflect.declaration.CtFormalTypeDeclarer)3 CtLambda (spoon.reflect.code.CtLambda)2 CtElement (spoon.reflect.declaration.CtElement)2 CtExecutable (spoon.reflect.declaration.CtExecutable)2 CtInterface (spoon.reflect.declaration.CtInterface)2