Search in sources :

Example 66 with AnnotatedTypeMirror

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

the class BaseTypeVisitor method visitLambdaExpression.

@Override
public Void visitLambdaExpression(LambdaExpressionTree node, Void p) {
    Pair<AnnotatedDeclaredType, AnnotatedExecutableType> result = atypeFactory.getFnInterfaceFromTree(node);
    AnnotatedExecutableType functionType = result.second;
    if (node.getBody().getKind() != Tree.Kind.BLOCK) {
        // Check return type for single statement returns here.
        AnnotatedTypeMirror ret = functionType.getReturnType();
        if (ret.getKind() != TypeKind.VOID) {
            visitorState.setAssignmentContext(Pair.of((Tree) node, ret));
            commonAssignmentCheck(ret, (ExpressionTree) node.getBody(), "return.type.incompatible");
        }
    }
    // Check parameters
    for (int i = 0; i < functionType.getParameterTypes().size(); ++i) {
        AnnotatedTypeMirror lambdaParameter = atypeFactory.getAnnotatedType(node.getParameters().get(i));
        commonAssignmentCheck(lambdaParameter, functionType.getParameterTypes().get(i), node.getParameters().get(i), "lambda.param.type.incompatible");
    }
    return super.visitLambdaExpression(node, p);
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ThrowTree(com.sun.source.tree.ThrowTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ReturnTree(com.sun.source.tree.ReturnTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) JCTree(com.sun.tools.javac.tree.JCTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 67 with AnnotatedTypeMirror

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

the class UpperBoundTransfer method getUBQualifierForAddition.

/**
 * Returns the UBQualifier for a node, with additional refinement useful specifically for
 * integer addition, based on the information from subcheckers of the Index Checker.
 *
 * @param n the node
 * @param in dataflow analysis transfer input
 * @return the UBQualifier for {@code node}
 */
private UBQualifier getUBQualifierForAddition(Node n, TransferInput<CFValue, CFStore> in) {
    // The method takes the greatest lower bound of the qualifier returned by
    // getUBQualifier and a qualifier created from a SubstringIndexFor annotation, if such
    // annotation is present and the index is known to be non-negative.
    UBQualifier ubQualifier = getUBQualifier(n, in);
    Tree nodeTree = n.getTree();
    // Annotation from the Substring Index hierarchy
    AnnotatedTypeMirror substringIndexType = atypeFactory.getSubstringIndexAnnotatedTypeFactory().getAnnotatedType(nodeTree);
    AnnotationMirror substringIndexAnno = substringIndexType.getAnnotation(SubstringIndexFor.class);
    // Annotation from the Lower bound hierarchy
    AnnotatedTypeMirror lowerBoundType = atypeFactory.getLowerBoundAnnotatedTypeFactory().getAnnotatedType(nodeTree);
    // convert the SubstringIndexFor annotation to a upper bound qualifier.
    if (substringIndexAnno != null && (lowerBoundType.hasAnnotation(NonNegative.class) || lowerBoundType.hasAnnotation(Positive.class))) {
        UBQualifier substringIndexQualifier = UBQualifier.createUBQualifier(substringIndexAnno);
        ubQualifier = ubQualifier.glb(substringIndexQualifier);
    }
    return ubQualifier;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Positive(org.checkerframework.checker.index.qual.Positive) Tree(com.sun.source.tree.Tree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) NonNegative(org.checkerframework.checker.index.qual.NonNegative)

Example 68 with AnnotatedTypeMirror

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

the class UpperBoundVisitor method visitAccess.

/**
 * Checks if this array access is either using a variable that is less than the length of the
 * array, or using a constant less than the array's minlen. Issues an error if neither is true.
 */
private void visitAccess(ExpressionTree indexTree, ExpressionTree arrTree) {
    AnnotatedTypeMirror indexType = atypeFactory.getAnnotatedType(indexTree);
    String arrName = FlowExpressions.internalReprOf(this.atypeFactory, arrTree).toString();
    UBQualifier qualifier = UBQualifier.createUBQualifier(indexType, atypeFactory.UNKNOWN);
    if (qualifier.isLessThanLengthOf(arrName)) {
        return;
    }
    // Find max because it's important to determine whether the index is less than the
    // minimum length of the array. If it could be any of several values, only the max is of
    // interest.
    Long valMax = IndexUtil.getMaxValue(indexTree, atypeFactory.getValueAnnotatedTypeFactory());
    AnnotationMirror sameLenAnno = atypeFactory.sameLenAnnotationFromTree(arrTree);
    // Produce the full list of relevant names by checking the SameLen type.
    if (sameLenAnno != null && AnnotationUtils.areSameByClass(sameLenAnno, SameLen.class)) {
        if (AnnotationUtils.hasElementValue(sameLenAnno, "value")) {
            List<String> slNames = AnnotationUtils.getElementValueArray(sameLenAnno, "value", String.class, true);
            if (qualifier.isLessThanLengthOfAny(slNames)) {
                return;
            }
            for (String slName : slNames) {
                // Check if any of the arrays have a minlen that is greater than the
                // known constant value.
                int minlenSL = atypeFactory.getValueAnnotatedTypeFactory().getMinLenFromString(slName, arrTree, getCurrentPath());
                if (valMax != null && valMax < minlenSL) {
                    return;
                }
            }
        }
    }
    // Check against the minlen of the array itself.
    int minLen = IndexUtil.getMinLen(arrTree, atypeFactory.getValueAnnotatedTypeFactory());
    if (valMax != null && valMax < minLen) {
        return;
    }
    if (IndexUtil.getExactValue(indexTree, atypeFactory.getValueAnnotatedTypeFactory()) != null) {
        // Note that valMax is equal to the exact value in this case.
        checker.report(Result.failure(UPPER_BOUND_CONST, valMax, atypeFactory.getValueAnnotatedTypeFactory().getAnnotatedType(arrTree).toString(), valMax + 1, valMax + 1), indexTree);
    } else if (valMax != null && qualifier.isUnknown()) {
        checker.report(Result.failure(UPPER_BOUND_RANGE, atypeFactory.getValueAnnotatedTypeFactory().getAnnotatedType(indexTree).toString(), atypeFactory.getValueAnnotatedTypeFactory().getAnnotatedType(arrTree).toString(), arrName, arrName, valMax + 1), indexTree);
    } else {
        checker.report(Result.failure(UPPER_BOUND, indexType.toString(), arrName, arrName, arrName), indexTree);
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) SameLen(org.checkerframework.checker.index.qual.SameLen) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 69 with AnnotatedTypeMirror

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

the class InitializationAnnotatedTypeFactory method isInitializedForFrame.

public boolean isInitializedForFrame(AnnotatedTypeMirror type, TypeMirror frame) {
    AnnotationMirror initializationAnno = type.getEffectiveAnnotationInHierarchy(UNCLASSIFIED);
    TypeMirror typeFrame = getTypeFrameFromAnnotation(initializationAnno);
    Types types = processingEnv.getTypeUtils();
    return types.isSubtype(typeFrame, frame);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Types(javax.lang.model.util.Types) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror)

Example 70 with AnnotatedTypeMirror

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

the class InitializationAnnotatedTypeFactory method postAsMemberOf.

/**
 * {@inheritDoc}
 *
 * <p>In most cases, subclasses want to call this method first because it may clear all
 * annotations and use the hierarchy's root annotations.
 */
@Override
public void postAsMemberOf(AnnotatedTypeMirror type, AnnotatedTypeMirror owner, Element element) {
    super.postAsMemberOf(type, owner, element);
    if (element.getKind().isField()) {
        Collection<? extends AnnotationMirror> declaredFieldAnnotations = getDeclAnnotations(element);
        AnnotatedTypeMirror fieldAnnotations = getAnnotatedType(element);
        computeFieldAccessType(type, declaredFieldAnnotations, owner, fieldAnnotations, element);
    }
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)188 AnnotationMirror (javax.lang.model.element.AnnotationMirror)42 ExpressionTree (com.sun.source.tree.ExpressionTree)32 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)27 Tree (com.sun.source.tree.Tree)25 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)25 VariableTree (com.sun.source.tree.VariableTree)22 ArrayList (java.util.ArrayList)22 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)21 MethodTree (com.sun.source.tree.MethodTree)20 TypeVariable (javax.lang.model.type.TypeVariable)19 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)18 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)17 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)16 TypeMirror (javax.lang.model.type.TypeMirror)16 VariableElement (javax.lang.model.element.VariableElement)15 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)13 MemberSelectTree (com.sun.source.tree.MemberSelectTree)13 NewClassTree (com.sun.source.tree.NewClassTree)13 Element (javax.lang.model.element.Element)13