Search in sources :

Example 81 with AnnotatedTypeMirror

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

the class NullnessAnnotatedTypeFactory method adaptGetClassReturnTypeToReceiver.

@Override
public void adaptGetClassReturnTypeToReceiver(final AnnotatedExecutableType getClassType, final AnnotatedTypeMirror receiverType) {
    super.adaptGetClassReturnTypeToReceiver(getClassType, receiverType);
    // Make the wildcard always @NonNull, regardless of the declared type.
    final AnnotatedDeclaredType returnAdt = (AnnotatedDeclaredType) getClassType.getReturnType();
    final List<AnnotatedTypeMirror> typeArgs = returnAdt.getTypeArguments();
    final AnnotatedWildcardType classWildcardArg = (AnnotatedWildcardType) typeArgs.get(0);
    classWildcardArg.getExtendsBoundField().replaceAnnotation(NONNULL);
}
Also used : AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) AnnotatedWildcardType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 82 with AnnotatedTypeMirror

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

the class NullnessVisitor method checkForRedundantTests.

/**
 * Reports an error if a comparison of a @NonNull expression with the null literal is performed.
 */
protected void checkForRedundantTests(BinaryTree node) {
    final ExpressionTree leftOp = node.getLeftOperand();
    final ExpressionTree rightOp = node.getRightOperand();
    // respect command-line option
    if (!checker.getLintOption(AbstractNullnessChecker.LINT_REDUNDANTNULLCOMPARISON, AbstractNullnessChecker.LINT_DEFAULT_REDUNDANTNULLCOMPARISON)) {
        return;
    }
    // equality tests
    if ((node.getKind() == Tree.Kind.EQUAL_TO || node.getKind() == Tree.Kind.NOT_EQUAL_TO)) {
        AnnotatedTypeMirror left = atypeFactory.getAnnotatedType(leftOp);
        AnnotatedTypeMirror right = atypeFactory.getAnnotatedType(rightOp);
        if (leftOp.getKind() == Tree.Kind.NULL_LITERAL && right.hasEffectiveAnnotation(NONNULL))
            checker.report(Result.warning(KNOWN_NONNULL, rightOp.toString()), node);
        else if (rightOp.getKind() == Tree.Kind.NULL_LITERAL && left.hasEffectiveAnnotation(NONNULL))
            checker.report(Result.warning(KNOWN_NONNULL, leftOp.toString()), node);
    }
}
Also used : ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 83 with AnnotatedTypeMirror

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

the class NullnessVisitor method checkMethodInvocability.

@Override
protected void checkMethodInvocability(AnnotatedExecutableType method, MethodInvocationTree node) {
    if (!TreeUtils.isSelfAccess(node) && // Static methods don't have a receiver
    method.getReceiverType() != null) {
        // TODO: should all or some constructors be excluded?
        // method.getElement().getKind() != ElementKind.CONSTRUCTOR) {
        Set<AnnotationMirror> recvAnnos = atypeFactory.getReceiverType(node).getAnnotations();
        AnnotatedTypeMirror methodReceiver = method.getReceiverType().getErased();
        AnnotatedTypeMirror treeReceiver = methodReceiver.shallowCopy(false);
        AnnotatedTypeMirror rcv = atypeFactory.getReceiverType(node);
        treeReceiver.addAnnotations(rcv.getEffectiveAnnotations());
        // "dereference.of.nullable" message).
        if (treeReceiver.hasAnnotation(NULLABLE) || recvAnnos.contains(MONOTONIC_NONNULL)) {
            return;
        }
    }
    super.checkMethodInvocability(method, node);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 84 with AnnotatedTypeMirror

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

the class SystemGetPropertyHandler method handle.

public void handle(MethodInvocationTree tree, AnnotatedExecutableType method) {
    if (TreeUtils.isMethodInvocation(tree, systemGetProperty, env)) {
        List<? extends ExpressionTree> args = tree.getArguments();
        assert args.size() == 1;
        ExpressionTree arg = args.get(0);
        if (arg.getKind() == Tree.Kind.STRING_LITERAL) {
            String literal = (String) ((LiteralTree) arg).getValue();
            if (systemProperties.contains(literal)) {
                AnnotatedTypeMirror type = method.getReturnType();
                type.replaceAnnotation(factory.NONNULL);
            }
        }
    }
}
Also used : ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 85 with AnnotatedTypeMirror

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

the class UnitsRelationsTools method removePrefix.

/**
 * Removes the Prefix value from an Annotated Type, by constructing and returning a copy of the
 * Annotated Type without the prefix
 *
 * @param elements the Element Utilities from a checker's processing environment, typically
 *     obtained by calling env.getElementUtils() in init() of a Units Relations implementation
 * @param annoType an AnnotatedTypeMirror representing a Units Annotated Type
 * @return a copy of the Annotated Type without the prefix
 */
public static AnnotatedTypeMirror removePrefix(@Nullable final Elements elements, @Nullable final AnnotatedTypeMirror annoType) {
    // deep copy the Annotated Type Mirror without any of the Annotations
    AnnotatedTypeMirror result = annoType.deepCopy(false);
    // get all of the original Annotations in the Annotated Type
    Set<AnnotationMirror> annos = annoType.getAnnotations();
    // does
    for (AnnotationMirror anno : annos) {
        // try to clean the Annotation Mirror of the Prefix
        AnnotationMirror cleanedMirror = removePrefix(elements, anno);
        // if successful, add the cleaned annotation to the deep copy
        if (cleanedMirror != null) {
            result.addAnnotation(cleanedMirror);
        } else // if unsuccessful, add the original annotation
        {
            result.addAnnotation(anno);
        }
    }
    return result;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) 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