Search in sources :

Example 1 with CtTypeParameterReference

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

the class TypeFactory method createTypeParameterReference.

/**
 * Creates a type parameter reference with no bounds.
 *
 * @param name
 * 		the name of the formal parameter
 */
public CtTypeParameterReference createTypeParameterReference(String name) {
    CtTypeParameterReference typeParam = factory.Core().createTypeParameterReference();
    typeParam.setSimpleName(name);
    return typeParam;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference)

Example 2 with CtTypeParameterReference

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

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

the class ReferenceBuilder method getTypeParameterReference.

CtTypeReference<Object> getTypeParameterReference(TypeBinding binding, TypeReference ref) {
    CtTypeReference<Object> ctRef = getTypeReference(binding);
    if (ctRef != null && isCorrectTypeReference(ref)) {
        if (!(ctRef instanceof CtTypeParameterReference)) {
            CtTypeParameterReference typeParameterRef = this.jdtTreeBuilder.getFactory().Core().createTypeParameterReference();
            typeParameterRef.setSimpleName(ctRef.getSimpleName());
            typeParameterRef.setDeclaringType(ctRef.getDeclaringType());
            typeParameterRef.setPackage(ctRef.getPackage());
            ctRef = typeParameterRef;
        }
        insertGenericTypesInNoClasspathFromJDTInSpoon(ref, ctRef);
        return ctRef;
    }
    return getTypeParameterReference(CharOperation.toString(ref.getParameterizedTypeName()));
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference)

Example 4 with CtTypeParameterReference

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

the class ReferenceBuilder method buildTypeReferenceInternal.

private <T> CtTypeReference<T> buildTypeReferenceInternal(CtTypeReference<T> typeReference, TypeReference type, Scope scope) {
    if (type == null) {
        return null;
    }
    CtTypeReference<?> currentReference = typeReference;
    for (int position = type.getTypeName().length - 1; position >= 0; position--) {
        if (currentReference == null) {
            break;
        }
        this.jdtTreeBuilder.getContextBuilder().enter(currentReference, type);
        if (type.annotations != null && type.annotations.length - 1 <= position && type.annotations[position] != null && type.annotations[position].length > 0) {
            for (Annotation annotation : type.annotations[position]) {
                if (scope instanceof ClassScope) {
                    annotation.traverse(this.jdtTreeBuilder, (ClassScope) scope);
                } else if (scope instanceof BlockScope) {
                    annotation.traverse(this.jdtTreeBuilder, (BlockScope) scope);
                } else {
                    annotation.traverse(this.jdtTreeBuilder, (BlockScope) null);
                }
            }
        }
        if (type.getTypeArguments() != null && type.getTypeArguments().length - 1 <= position && type.getTypeArguments()[position] != null && type.getTypeArguments()[position].length > 0) {
            currentReference.getActualTypeArguments().clear();
            for (TypeReference typeArgument : type.getTypeArguments()[position]) {
                if (typeArgument instanceof Wildcard || typeArgument.resolvedType instanceof WildcardBinding || typeArgument.resolvedType instanceof TypeVariableBinding) {
                    currentReference.addActualTypeArgument(buildTypeParameterReference(typeArgument, scope));
                } else {
                    currentReference.addActualTypeArgument(buildTypeReference(typeArgument, scope));
                }
            }
        } else if ((type instanceof ParameterizedSingleTypeReference || type instanceof ParameterizedQualifiedTypeReference) && !isTypeArgumentExplicit(type.getTypeArguments())) {
            for (CtTypeReference<?> actualTypeArgument : currentReference.getActualTypeArguments()) {
                actualTypeArgument.setImplicit(true);
                if (actualTypeArgument instanceof CtArrayTypeReference) {
                    ((CtArrayTypeReference) actualTypeArgument).getComponentType().setImplicit(true);
                }
            }
        }
        if (type instanceof Wildcard && typeReference instanceof CtTypeParameterReference) {
            ((CtTypeParameterReference) typeReference).setBoundingType(buildTypeReference(((Wildcard) type).bound, scope));
        }
        this.jdtTreeBuilder.getContextBuilder().exit(type);
        currentReference = currentReference.getDeclaringType();
    }
    return typeReference;
}
Also used : TypeVariableBinding(org.eclipse.jdt.internal.compiler.lookup.TypeVariableBinding) WildcardBinding(org.eclipse.jdt.internal.compiler.lookup.WildcardBinding) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) ClassScope(org.eclipse.jdt.internal.compiler.lookup.ClassScope) CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) Wildcard(org.eclipse.jdt.internal.compiler.ast.Wildcard) CtTypeReference(spoon.reflect.reference.CtTypeReference) BlockScope(org.eclipse.jdt.internal.compiler.lookup.BlockScope) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ParameterizedSingleTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) ParameterizedQualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtArrayTypeReference(spoon.reflect.reference.CtArrayTypeReference)

Example 5 with CtTypeParameterReference

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

the class ConstructorTest method assertIntersectionTypeInConstructor.

private void assertIntersectionTypeInConstructor(CtTypeReference<?> boundingType1) {
    assertTrue(boundingType1 instanceof CtIntersectionTypeReference);
    CtIntersectionTypeReference<?> boundingType = boundingType1.asCtIntersectionTypeReference();
    final List<CtTypeReference<?>> bounds = boundingType.getBounds().stream().collect(Collectors.toList());
    CtTypeReference<?> genericTacos = bounds.get(0);
    assertEquals("Tacos", genericTacos.getSimpleName());
    assertEquals(1, genericTacos.getAnnotations().size());
    assertEquals(1, genericTacos.getActualTypeArguments().size());
    CtTypeParameterReference wildcard = (CtTypeParameterReference) genericTacos.getActualTypeArguments().get(0);
    assertEquals("?", wildcard.getSimpleName());
    assertEquals(1, wildcard.getAnnotations().size());
    assertEquals("C", wildcard.getBoundingType().getSimpleName());
    assertEquals(1, wildcard.getBoundingType().getAnnotations().size());
    assertEquals("Serializable", bounds.get(1).getSimpleName());
    assertEquals(1, bounds.get(1).getAnnotations().size());
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtIntersectionTypeReference(spoon.reflect.reference.CtIntersectionTypeReference)

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