Search in sources :

Example 6 with CtTypeParameterReference

use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.

the class GenericsTest method testTypeParameterDeclarer.

@Test
public void testTypeParameterDeclarer() throws Exception {
    // contract: one can lookup the declarer of a type parameter if it is in appropriate context (the declararer is in the parent hierarchy)
    CtClass<?> classThatDefinesANewTypeArgument = build("spoon.test.generics", "ClassThatDefinesANewTypeArgument");
    CtTypeParameter typeParam = classThatDefinesANewTypeArgument.getFormalCtTypeParameters().get(0);
    assertEquals("T", classThatDefinesANewTypeArgument.getFormalCtTypeParameters().get(0).getSimpleName());
    assertSame(classThatDefinesANewTypeArgument, typeParam.getTypeParameterDeclarer());
    CtTypeParameterReference typeParamReference = typeParam.getReference();
    assertSame(typeParam, typeParamReference.getDeclaration());
    // creating an appropriate context
    CtMethod m = classThatDefinesANewTypeArgument.getFactory().createMethod();
    m.setParent(classThatDefinesANewTypeArgument);
    // setting the return type of the method
    m.setType(typeParamReference);
    classThatDefinesANewTypeArgument.addMethod(m);
    // the final assertions
    assertSame(typeParam, typeParamReference.getDeclaration());
    assertSame(classThatDefinesANewTypeArgument, typeParamReference.getDeclaration().getParent());
    // now testing that the getDeclaration of a type parameter is actually a dynamic lookup
    CtClass<?> c2 = classThatDefinesANewTypeArgument.clone();
    c2.addMethod(m);
    assertSame(c2, typeParamReference.getDeclaration().getParent());
    // even if we rename it
    // renaming the reference
    typeParamReference.setSimpleName("R");
    // renaming the declaration
    c2.getFormalCtTypeParameters().get(0).setSimpleName("R");
    assertSame(c2, typeParamReference.getDeclaration().getParent());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtMethod(spoon.reflect.declaration.CtMethod) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 7 with CtTypeParameterReference

use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.

the class GenericsTest method testTypeParameterReference.

@Test
public void testTypeParameterReference() throws Exception {
    Factory factory = build(ClassThatBindsAGenericType.class, ClassThatDefinesANewTypeArgument.class);
    CtClass<?> classThatBindsAGenericType = factory.Class().get(ClassThatBindsAGenericType.class);
    CtClass<?> classThatDefinesANewTypeArgument = factory.Class().get(ClassThatDefinesANewTypeArgument.class);
    CtTypeReference<?> tr1 = classThatBindsAGenericType.getSuperclass();
    CtTypeReference<?> trExtends = tr1.getActualTypeArguments().get(0);
    CtTypeParameter tr2 = classThatDefinesANewTypeArgument.getFormalCtTypeParameters().get(0);
    CtTypeReference<?> tr3 = classThatDefinesANewTypeArgument.getMethodsByName("foo").get(0).getParameters().get(0).getReference().getType();
    // an bound type is not an TypeParameterRefernce
    assertTrue(!(trExtends instanceof CtTypeParameterReference));
    // a used type parameter T is a CtTypeParameterReference
    assertTrue(tr3 instanceof CtTypeParameterReference);
    assertEquals("File", trExtends.getSimpleName());
    assertEquals(java.io.File.class, trExtends.getActualClass());
    assertEquals("T", tr2.getSimpleName());
    assertEquals("T", tr3.getSimpleName());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 8 with CtTypeParameterReference

use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.

the class GenericsTest method checkFakeTpl.

private void checkFakeTpl(CtInterface<?> fakeTplItf) {
    assertNotNull(fakeTplItf);
    CtMethod<?> applyMethod = fakeTplItf.getMethodsByName("apply").get(0);
    CtTypeReference<?> returnType = applyMethod.getType();
    assertEquals("T", returnType.getSimpleName());
    assertTrue(returnType instanceof CtTypeParameterReference);
    assertEquals("CtElement", returnType.getSuperclass().getSimpleName());
    CtParameter<?> targetType = applyMethod.getParameters().get(0);
    List<CtTypeReference<?>> targetTypeArgument = targetType.getType().getActualTypeArguments();
    assertEquals(1, targetTypeArgument.size());
    assertTrue(targetTypeArgument.get(0) instanceof CtWildcardReference);
    CtMethod<?> testMethod = fakeTplItf.getMethodsByName("test").get(0);
    List<CtParameter<?>> parameters = testMethod.getParameters();
    assertEquals(3, parameters.size());
    CtParameter thirdParam = parameters.get(2);
    assertTrue(thirdParam.getType() instanceof CtTypeParameterReference);
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtWildcardReference(spoon.reflect.reference.CtWildcardReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtParameter(spoon.reflect.declaration.CtParameter)

Example 9 with CtTypeParameterReference

use of spoon.reflect.reference.CtTypeParameterReference in project spoon by INRIA.

the class AbstractTypingContext method adaptTypeParameterReferenceBoundingType.

private CtTypeReference<?> adaptTypeParameterReferenceBoundingType(CtTypeParameterReference typeParamRef, CtTypeReference<?> boundingType) {
    CtTypeParameterReference typeParamRefAdapted = typeParamRef.clone();
    typeParamRefAdapted.setParent(typeParamRef.getParent());
    typeParamRefAdapted.setBoundingType(boundingType.equals(boundingType.getFactory().Type().getDefaultBoundingType()) ? boundingType.getFactory().Type().getDefaultBoundingType() : adaptType(boundingType));
    return typeParamRefAdapted;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference)

Example 10 with CtTypeParameterReference

use of spoon.reflect.reference.CtTypeParameterReference 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)

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