Search in sources :

Example 16 with AnnotatedTypeMirror

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

the class BaseTypeVisitor method commonAssignmentCheck.

/**
 * Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and
 * emits an error message (through the compiler's messaging interface) if it is not valid.
 *
 * @param varTree the AST node for the lvalue (usually a variable)
 * @param valueExp the AST node for the rvalue (the new value)
 * @param errorKey the error message to use if the check fails (must be a compiler message key,
 *     see {@link org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey})
 */
protected void commonAssignmentCheck(Tree varTree, ExpressionTree valueExp, @CompilerMessageKey String errorKey) {
    AnnotatedTypeMirror var = atypeFactory.getAnnotatedTypeLhs(varTree);
    assert var != null : "no variable found for tree: " + varTree;
    if (!validateType(varTree, var)) {
        return;
    }
    checkAssignability(var, varTree);
    commonAssignmentCheck(var, valueExp, errorKey);
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 17 with AnnotatedTypeMirror

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

the class BaseTypeVisitor method checkTypecastSafety.

protected void checkTypecastSafety(TypeCastTree node, Void p) {
    if (!checker.getLintOption("cast:unsafe", true)) {
        return;
    }
    AnnotatedTypeMirror castType = atypeFactory.getAnnotatedType(node);
    AnnotatedTypeMirror exprType = atypeFactory.getAnnotatedType(node.getExpression());
    // the input types to be subtypes according to Java
    if (!isTypeCastSafe(castType, exprType)) {
        checker.report(Result.warning("cast.unsafe", exprType.toString(true), castType.toString(true)), node);
    }
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 18 with AnnotatedTypeMirror

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

the class DefaultReflectionResolver method resolveMethodCall.

/**
 * Resolves a call to {@link Method#invoke(Object, Object...)}.
 *
 * @param factory the {@link AnnotatedTypeFactory} of the underlying type system
 * @param tree the method invocation tree that has to be resolved
 * @param origResult the original result from {@code factory.methodFromUse}.
 */
private Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> resolveMethodCall(AnnotatedTypeFactory factory, MethodInvocationTree tree, Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> origResult) {
    debugReflection("Try to resolve reflective method call: " + tree);
    List<MethodInvocationTree> possibleMethods = resolveReflectiveMethod(tree, factory);
    // Reflective method could not be resolved
    if (possibleMethods.size() == 0) {
        return origResult;
    }
    Set<? extends AnnotationMirror> returnLub = null;
    Set<? extends AnnotationMirror> receiverGlb = null;
    Set<? extends AnnotationMirror> paramsGlb = null;
    // and parameter types
    for (MethodInvocationTree resolvedTree : possibleMethods) {
        debugReflection("Resolved method invocation: " + resolvedTree);
        if (!checkMethodAgruments(resolvedTree)) {
            debugReflection("Spoofed tree's arguments did not match declaration" + resolvedTree.toString());
            // in QualifierPolymorphism.PolyCollector.visitArray(...)
            continue;
        }
        Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> resolvedResult = factory.methodFromUse(resolvedTree);
        // Lub return types
        returnLub = lub(returnLub, resolvedResult.first.getReturnType().getAnnotations(), factory);
        // Check for static methods whose receiver is null
        if (resolvedResult.first.getReceiverType() == null) {
            // If the method is static the first argument to Method.invoke isn't used,
            // so assume top.
            receiverGlb = glb(receiverGlb, factory.getQualifierHierarchy().getTopAnnotations(), factory);
        } else {
            receiverGlb = glb(receiverGlb, resolvedResult.first.getReceiverType().getAnnotations(), factory);
        }
        // the types of different formal parameters.
        for (AnnotatedTypeMirror mirror : resolvedResult.first.getParameterTypes()) {
            paramsGlb = glb(paramsGlb, mirror.getAnnotations(), factory);
        }
    }
    if (returnLub == null) {
        // None of the spoofed tree's arguments matched the declared method
        return origResult;
    }
    /*
         * Clear all original (return, receiver, parameter type) annotations and
         * set lub/glb annotations from resolved method(s)
         */
    // return value
    origResult.first.getReturnType().clearAnnotations();
    origResult.first.getReturnType().addAnnotations(returnLub);
    // receiver type
    origResult.first.getParameterTypes().get(0).clearAnnotations();
    origResult.first.getParameterTypes().get(0).addAnnotations(receiverGlb);
    // parameter types
    if (paramsGlb != null) {
        AnnotatedArrayType origArrayType = (AnnotatedArrayType) origResult.first.getParameterTypes().get(1);
        origArrayType.getComponentType().clearAnnotations();
        origArrayType.getComponentType().addAnnotations(paramsGlb);
    }
    debugReflection("Resolved annotations: " + origResult.first);
    return origResult;
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) List(java.util.List) ArrayList(java.util.ArrayList) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 19 with AnnotatedTypeMirror

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

the class PropagationTreeAnnotator method visitBinary.

@Override
public Void visitBinary(BinaryTree node, AnnotatedTypeMirror type) {
    if (hasPrimaryAnnotationInAllHierarchies(type)) {
        // expensive.
        return null;
    }
    AnnotatedTypeMirror a = atypeFactory.getAnnotatedType(node.getLeftOperand());
    AnnotatedTypeMirror b = atypeFactory.getAnnotatedType(node.getRightOperand());
    Set<? extends AnnotationMirror> lubs = qualHierarchy.leastUpperBounds(a.getEffectiveAnnotations(), b.getEffectiveAnnotations());
    type.addMissingAnnotations(lubs);
    return null;
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 20 with AnnotatedTypeMirror

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

the class PropagationTypeAnnotator method getTypeParamFromEnclosingClass.

/**
 * Search parent's type arguments for wildcard. Using the index of wildcard, find the
 * corresponding type parameter element and return it. Returns null if the wildcard is the
 * result of substitution and therefore not in the list of type arguments.
 */
private Element getTypeParamFromEnclosingClass(final AnnotatedWildcardType wildcard, final AnnotatedDeclaredType parent) {
    Integer wildcardIndex = null;
    int currentIndex = 0;
    for (AnnotatedTypeMirror typeArg : parent.getTypeArguments()) {
        // which they should have been replaced by capture
        if (typeArg == wildcard) {
            wildcardIndex = currentIndex;
            break;
        }
        currentIndex += 1;
    }
    if (wildcardIndex != null) {
        final TypeElement typeElement = (TypeElement) typeFactory.getProcessingEnv().getTypeUtils().asElement(parent.getUnderlyingType());
        return typeElement.getTypeParameters().get(wildcardIndex);
    }
    return null;
}
Also used : TypeElement(javax.lang.model.element.TypeElement) 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