Search in sources :

Example 11 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class DependentTypesHelper method checkTypeVariables.

private void checkTypeVariables(MethodTree node, AnnotatedExecutableType methodType) {
    Element ele = TreeUtils.elementFromDeclaration(node);
    TypeMirror enclosingType = ElementUtils.enclosingClass(ele).asType();
    FlowExpressionContext context = FlowExpressionContext.buildContextForMethodDeclaration(node, enclosingType, factory.getContext());
    for (int i = 0; i < methodType.getTypeVariables().size(); i++) {
        AnnotatedTypeMirror atm = methodType.getTypeVariables().get(i);
        standardizeDoNotUseLocals(context, factory.getPath(node), atm);
        checkType(atm, node.getTypeParameters().get(i));
    }
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) FlowExpressionContext(org.checkerframework.framework.util.FlowExpressionParseUtil.FlowExpressionContext) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 12 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class DependentTypesHelper method checkMethod.

/**
 * Checks all expressions in the method declaration AnnotatedTypeMirror to see if the expression
 * string is an error string as specified by DependentTypesError#isExpressionError. If the
 * annotated type has any errors, a flowexpr.parse.error is issued.
 *
 * @param methodTree method to check
 * @param type annotated type of the method
 */
public void checkMethod(MethodTree methodTree, AnnotatedExecutableType type) {
    // Parameters and receivers are checked by visitVariable
    // So only type parameters and return type need to be checked here.
    checkTypeVariables(methodTree, type);
    // Check return type
    if (type.getReturnType().getKind() != TypeKind.VOID) {
        AnnotatedTypeMirror returnType = factory.getMethodReturnType(methodTree);
        checkType(returnType, methodTree.getReturnType());
    }
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 13 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class ElementAnnotationUtil method getLocationTypeADT.

/**
 * Given a TypePath into a declared type, return the component type that is located at the end
 * of the TypePath
 *
 * @param type a type containing the type specified by location
 * @param location a type path into type
 * @return the type specified by location
 */
private static AnnotatedTypeMirror getLocationTypeADT(AnnotatedDeclaredType type, List<TypeAnnotationPosition.TypePathEntry> location) {
    // List order by outer most type to inner most type.
    ArrayDeque<AnnotatedDeclaredType> outerToInner = new ArrayDeque<>();
    AnnotatedDeclaredType enclosing = type;
    while (enclosing != null) {
        outerToInner.addFirst(enclosing);
        enclosing = enclosing.getEnclosingType();
    }
    // Create a linked list of the location, so removing the first element is easier.
    // Also, the `tail` operation wouldn't work with a Deque.
    @SuppressWarnings("JdkObsolete") LinkedList<TypePathEntry> tailOfLocations = new LinkedList<>(location);
    boolean error = false;
    while (!tailOfLocations.isEmpty()) {
        TypePathEntry currentLocation = tailOfLocations.removeFirst();
        switch(currentLocation.tag) {
            case INNER_TYPE:
                outerToInner.removeFirst();
                break;
            case TYPE_ARGUMENT:
                AnnotatedDeclaredType innerType = outerToInner.getFirst();
                if (currentLocation.arg < innerType.getTypeArguments().size()) {
                    AnnotatedTypeMirror typeArg = innerType.getTypeArguments().get(currentLocation.arg);
                    return getTypeAtLocation(typeArg, tailOfLocations);
                } else {
                    error = true;
                    break;
                }
            default:
                error = true;
        }
        if (error) {
            break;
        }
    }
    if (outerToInner.isEmpty() || error) {
        ErrorReporter.errorAbort("ElementAnnotationUtil.getLocationTypeADT: invalid location %s for type: %s", location, type);
    }
    return outerToInner.getFirst();
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) ArrayDeque(java.util.ArrayDeque) LinkedList(java.util.LinkedList) TypePathEntry(com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntry)

Example 14 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class TypeVarUseApplier method extractAndApply.

/**
 * Applies the bound annotations from the declaration of the type parameter and then applies the
 * explicit annotations written on the type variable
 */
public void extractAndApply() {
    ElementAnnotationUtil.addAnnotationsFromElement(typeVariable, useElem.getAnnotationMirrors());
    // apply declaration annotations
    ElementAnnotationApplier.apply(typeVariable, declarationElem, typeFactory);
    final List<Attribute.TypeCompound> annotations = getAnnotations(useElem, declarationElem);
    final List<Attribute.TypeCompound> typeVarAnnotations;
    if (arrayType != null) {
        // if the outer-most type is an array type then we want to ensure the outer annotations
        // are not applied as the type variables primary annotation
        typeVarAnnotations = removeComponentAnnotations(arrayType, annotations);
        ElementAnnotationUtil.annotateViaTypeAnnoPosition(arrayType, annotations);
    } else {
        typeVarAnnotations = annotations;
    }
    for (final Attribute.TypeCompound annotation : typeVarAnnotations) {
        typeVariable.removeAnnotationInHierarchy(annotation);
        typeVariable.addAnnotation(annotation);
        final List<? extends AnnotatedTypeMirror> upperBounds;
        if (typeVariable.getUpperBound() instanceof AnnotatedIntersectionType) {
            upperBounds = typeVariable.getUpperBound().directSuperTypes();
        } else {
            upperBounds = Arrays.asList(typeVariable.getUpperBound());
        }
        // TODO: to all of them?  Que dealio?  What should we do?
        for (final AnnotatedTypeMirror bound : upperBounds) {
            bound.removeAnnotationInHierarchy(annotation);
            bound.addAnnotation(annotation);
        }
    }
}
Also used : AnnotatedIntersectionType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedIntersectionType) Attribute(com.sun.tools.javac.code.Attribute) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 15 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class BaseTypeVisitor method checkTypecastRedundancy.

protected void checkTypecastRedundancy(TypeCastTree node, Void p) {
    if (!checker.getLintOption("cast:redundant", false)) {
        return;
    }
    AnnotatedTypeMirror castType = atypeFactory.getAnnotatedType(node);
    AnnotatedTypeMirror exprType = atypeFactory.getAnnotatedType(node.getExpression());
    if (castType.equals(exprType)) {
        checker.report(Result.warning("cast.redundant", castType), node);
    }
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)299 AnnotationMirror (javax.lang.model.element.AnnotationMirror)84 ExpressionTree (com.sun.source.tree.ExpressionTree)53 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)44 ExecutableElement (javax.lang.model.element.ExecutableElement)41 Tree (com.sun.source.tree.Tree)40 TypeMirror (javax.lang.model.type.TypeMirror)38 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)38 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)38 MethodTree (com.sun.source.tree.MethodTree)34 ArrayList (java.util.ArrayList)34 BugInCF (org.checkerframework.javacutil.BugInCF)32 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)30 TypeElement (javax.lang.model.element.TypeElement)29 VariableElement (javax.lang.model.element.VariableElement)29 VariableTree (com.sun.source.tree.VariableTree)28 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)27 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)26 ClassTree (com.sun.source.tree.ClassTree)25 Map (java.util.Map)24