Search in sources :

Example 1 with CtTypeParameter

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

the class TypeFactory method createReference.

/**
 * @param includingFormalTypeParameter if true then references to formal type parameters
 * 	are added as actual type arguments of returned {@link CtTypeReference}
 */
public <T> CtTypeReference<T> createReference(CtType<T> type, boolean includingFormalTypeParameter) {
    CtTypeReference<T> ref = factory.Core().createTypeReference();
    if (type.getDeclaringType() != null) {
        ref.setDeclaringType(createReference(type.getDeclaringType(), includingFormalTypeParameter));
    } else if (type.getPackage() != null) {
        ref.setPackage(factory.Package().createReference(type.getPackage()));
    }
    ref.setSimpleName(type.getSimpleName());
    if (includingFormalTypeParameter) {
        for (CtTypeParameter formalTypeParam : type.getFormalCtTypeParameters()) {
            ref.addActualTypeArgument(formalTypeParam.getReference());
        }
    }
    return ref;
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter)

Example 2 with CtTypeParameter

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

the class JDTTreeBuilder method visitTypeParameter.

private boolean visitTypeParameter(TypeParameter typeParameter, Scope scope) {
    final CtTypeParameter typeParameterRef = factory.Core().createTypeParameter();
    typeParameterRef.setSimpleName(CharOperation.charToString(typeParameter.name));
    context.enter(typeParameterRef, typeParameter);
    return true;
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter)

Example 3 with CtTypeParameter

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

the class ParentExiter method scanCtElement.

@Override
public void scanCtElement(CtElement e) {
    if (child instanceof CtAnnotation && this.jdtTreeBuilder.getContextBuilder().annotationValueName.isEmpty()) {
        // we check if the current element can have the annotation attached
        CtAnnotatedElementType annotatedElementType = CtAnnotation.getAnnotatedElementTypeForCtElement(e);
        annotatedElementType = (e instanceof CtTypeParameter || e instanceof CtTypeParameterReference) ? CtAnnotatedElementType.TYPE_USE : annotatedElementType;
        // in case of noclasspath, we cannot be 100% sure, so we guess it must be attached...
        if (this.jdtTreeBuilder.getFactory().getEnvironment().getNoClasspath() || (annotatedElementType != null && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, annotatedElementType))) {
            e.addAnnotation((CtAnnotation<?>) child);
        }
        // in this case the annotation should be (also) attached to the type
        if (e instanceof CtTypedElement && JDTTreeBuilderQuery.hasAnnotationWithType((Annotation) childJDT, CtAnnotatedElementType.TYPE_USE)) {
            List<CtAnnotation> annotations = new ArrayList<>();
            if (!annotationsMap.containsKey(e)) {
                annotationsMap.put((CtTypedElement<?>) e, annotations);
            } else {
                annotations = annotationsMap.get(e);
            }
            annotations.add((CtAnnotation) child.clone());
            annotationsMap.put((CtTypedElement<?>) e, annotations);
        }
    }
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtAnnotatedElementType(spoon.reflect.declaration.CtAnnotatedElementType) ArrayList(java.util.ArrayList) CtTypedElement(spoon.reflect.declaration.CtTypedElement) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) CtAnnotation(spoon.reflect.declaration.CtAnnotation)

Example 4 with CtTypeParameter

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

the class DefaultCoreFactory method createTypeParameter.

@Override
public CtTypeParameter createTypeParameter() {
    CtTypeParameter e = new CtTypeParameterImpl();
    e.setFactory(getMainFactory());
    return e;
}
Also used : CtTypeParameterImpl(spoon.support.reflect.declaration.CtTypeParameterImpl) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter)

Example 5 with CtTypeParameter

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

the class CtTypeParameterTest method checkType.

private void checkType(CtType<?> type) throws NoSuchFieldException, SecurityException {
    List<CtTypeParameter> formalTypeParameters = type.getFormalCtTypeParameters();
    for (CtTypeParameter ctTypeParameter : formalTypeParameters) {
        checkTypeParamErasureOfType(ctTypeParameter, type.getActualClass());
    }
    for (CtTypeMember member : type.getTypeMembers()) {
        if (member instanceof CtFormalTypeDeclarer) {
            CtFormalTypeDeclarer ftDecl = (CtFormalTypeDeclarer) member;
            formalTypeParameters = ftDecl.getFormalCtTypeParameters();
            if (member instanceof CtExecutable<?>) {
                CtExecutable<?> exec = (CtExecutable<?>) member;
                for (CtTypeParameter ctTypeParameter : formalTypeParameters) {
                    checkTypeParamErasureOfExecutable(ctTypeParameter);
                }
                for (CtParameter<?> param : exec.getParameters()) {
                    checkParameterErasureOfExecutable(param);
                }
            } else if (member instanceof CtType<?>) {
                CtType<?> nestedType = (CtType<?>) member;
                // recursive call for nested type
                checkType(nestedType);
            }
        }
    }
}
Also used : CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtType(spoon.reflect.declaration.CtType) CtFormalTypeDeclarer(spoon.reflect.declaration.CtFormalTypeDeclarer) CtExecutable(spoon.reflect.declaration.CtExecutable)

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