Search in sources :

Example 26 with CtTypeParameterReference

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

the class JavaReflectionTreeBuilder method visitTypeParameterReference.

@Override
public <T extends GenericDeclaration> void visitTypeParameterReference(TypeVariable<T> parameter) {
    final CtTypeParameterReference typeParameterReference = factory.Core().createTypeParameterReference();
    typeParameterReference.setSimpleName(parameter.getName());
    enter(new TypeReferenceRuntimeBuilderContext(typeParameterReference));
    super.visitTypeParameterReference(parameter);
    exit();
    contexts.peek().addTypeName(typeParameterReference);
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) TypeReferenceRuntimeBuilderContext(spoon.support.visitor.java.internal.TypeReferenceRuntimeBuilderContext)

Example 27 with CtTypeParameterReference

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

the class TemplateMatcher method getTemplateTypeParameters.

private List<CtTypeReference<?>> getTemplateTypeParameters(final CtClass<? extends Template<?>> templateType) {
    final List<CtTypeReference<?>> ts = new ArrayList<>();
    final Collection<String> c = Parameters.getNames(templateType);
    new CtScanner() {

        @Override
        public void visitCtTypeParameterReference(CtTypeParameterReference reference) {
            if (c.contains(reference.getSimpleName())) {
                ts.add(reference);
            }
        }

        @Override
        public <T> void visitCtTypeReference(CtTypeReference<T> reference) {
            if (c.contains(reference.getSimpleName())) {
                ts.add(reference);
            }
        }
    }.scan(templateType);
    return ts;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) ArrayList(java.util.ArrayList) CtScanner(spoon.reflect.visitor.CtScanner)

Example 28 with CtTypeParameterReference

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

the class CtTypeImpl method isSameParameter.

private boolean isSameParameter(CtMethod<?> method, CtTypeReference<?> ctParameterType, CtTypeReference<?> expectedType) {
    if (expectedType instanceof CtTypeParameterReference) {
        /*
			 * the expectedType is a generic parameter whose declaration should be searched in scope of method
			 * (not in scope of it's parent, where it can found another/wrong type parameter declaration of same name.
			 */
        CtTypeParameterReference tpr = (CtTypeParameterReference) expectedType;
        expectedType = tpr.clone();
        expectedType.setParent(method);
        if (expectedType.getDeclaration() == null) {
            return false;
        }
    }
    if (expectedType instanceof CtTypeParameterReference && ctParameterType instanceof CtTypeParameterReference) {
        // both types are generic
        if (!ctParameterType.equals(expectedType)) {
            return false;
        }
    } else if (expectedType instanceof CtTypeParameterReference) {
        // expectedType type is generic, ctParameterType is real type
        if (!expectedType.getTypeErasure().getQualifiedName().equals(ctParameterType.getQualifiedName())) {
            return false;
        }
    } else if (ctParameterType instanceof CtTypeParameterReference) {
        // ctParameterType is generic, expectedType type is real type
        CtTypeParameter declaration = (CtTypeParameter) ctParameterType.getDeclaration();
        if (declaration != null && declaration.getSuperclass() instanceof CtIntersectionTypeReference) {
            for (CtTypeReference<?> ctTypeReference : declaration.getSuperclass().asCtIntersectionTypeReference().getBounds()) {
                if (ctTypeReference.equals(expectedType)) {
                    return true;
                }
            }
        } else if (declaration != null && declaration.getSuperclass() != null) {
            return declaration.getSuperclass().equals(expectedType);
        } else {
            return getFactory().Type().objectType().equals(expectedType);
        }
    } else if (!expectedType.getQualifiedName().equals(ctParameterType.getQualifiedName())) {
        // both are real types
        return false;
    }
    return true;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtIntersectionTypeReference(spoon.reflect.reference.CtIntersectionTypeReference)

Example 29 with CtTypeParameterReference

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

the class AbstractTypingContext method adaptType.

@Override
public CtTypeReference<?> adaptType(CtTypeInformation type) {
    CtTypeReference<?> result;
    boolean isCopy = false;
    if (type instanceof CtTypeReference<?>) {
        if (type instanceof CtTypeParameterReference) {
            return adaptTypeParameterReference((CtTypeParameterReference) type);
        }
        result = (CtTypeReference<?>) type;
    } else {
        if (type instanceof CtTypeParameter) {
            return adaptTypeParameter((CtTypeParameter) type);
        }
        CtType<?> t = (CtType<?>) type;
        result = t.getFactory().Type().createReference(t, true);
        isCopy = true;
    }
    if (result.getActualTypeArguments().size() > 0) {
        // we have to adapt actual type arguments recursive too
        if (isCopy == false) {
            CtElement parent = result.getParent();
            result = result.clone();
            result.setParent(parent);
            List<CtTypeReference<?>> actTypeArgs = new ArrayList<>(result.getActualTypeArguments());
            for (int i = 0; i < actTypeArgs.size(); i++) {
                CtTypeReference adaptedTypeArgs = adaptType(actTypeArgs.get(i));
                // for some type argument we might return null to avoid recursive calls
                if (adaptedTypeArgs != null) {
                    actTypeArgs.set(i, adaptedTypeArgs.clone());
                }
            }
            result.setActualTypeArguments(actTypeArgs);
        }
    }
    return result;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList)

Example 30 with CtTypeParameterReference

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

the class MethodTypingContext method adaptTypeForNewMethod.

private CtTypeReference<?> adaptTypeForNewMethod(CtTypeReference<?> typeRef) {
    if (typeRef == null) {
        return null;
    }
    if (typeRef instanceof CtTypeParameterReference) {
        CtTypeParameterReference typeParamRef = (CtTypeParameterReference) typeRef;
        CtTypeParameter typeParam = typeParamRef.getDeclaration();
        if (typeParam == null) {
            throw new SpoonException("Declaration of the CtTypeParameter should not be null.");
        }
        if (typeParam.getTypeParameterDeclarer() instanceof CtExecutable) {
            // the parameter is declared in scope of Method or Constructor
            return typeRef.clone();
        }
    }
    // it is not type reference of scopeMethod. Adapt it using classTypingContext
    return classTypingContext.adaptType(typeRef);
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) SpoonException(spoon.SpoonException) CtExecutable(spoon.reflect.declaration.CtExecutable)

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