Search in sources :

Example 21 with AnnotatedArrayType

use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType 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 varType the annotated type of 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(AnnotatedTypeMirror varType, ExpressionTree valueExp, @CompilerMessageKey String errorKey) {
    if (shouldSkipUses(valueExp)) {
        return;
    }
    if (valueExp.getKind() == Tree.Kind.MEMBER_REFERENCE || valueExp.getKind() == Tree.Kind.LAMBDA_EXPRESSION) {
        // and do not need to be checked again as arguments.
        return;
    }
    if (varType.getKind() == TypeKind.ARRAY && valueExp instanceof NewArrayTree && ((NewArrayTree) valueExp).getType() == null) {
        AnnotatedTypeMirror compType = ((AnnotatedArrayType) varType).getComponentType();
        NewArrayTree arrayTree = (NewArrayTree) valueExp;
        assert arrayTree.getInitializers() != null : "array initializers are not expected to be null in: " + valueExp;
        checkArrayInitialization(compType, arrayTree.getInitializers());
    }
    if (!validateTypeOf(valueExp)) {
        return;
    }
    AnnotatedTypeMirror valueType = atypeFactory.getAnnotatedType(valueExp);
    assert valueType != null : "null type for expression: " + valueExp;
    commonAssignmentCheck(varType, valueType, valueExp, errorKey);
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) NewArrayTree(com.sun.source.tree.NewArrayTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 22 with AnnotatedArrayType

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

the class BaseTypeVisitor method typeCheckVectorCopyIntoArgument.

/**
 * Type checks the method arguments of {@code Vector.copyInto()}.
 *
 * <p>The Checker Framework special-cases the method invocation, as its type safety cannot be
 * expressed by Java's type system.
 *
 * <p>For a Vector {@code v} of type {@code Vector<E>}, the method invocation {@code
 * v.copyInto(arr)} is type-safe iff {@code arr} is an array of type {@code T[]}, where {@code
 * T} is a subtype of {@code E}.
 *
 * <p>In other words, this method checks that the type argument of the receiver method is a
 * subtype of the component type of the passed array argument.
 *
 * @param node a method invocation of {@code Vector.copyInto()}
 * @param params the types of the parameters of {@code Vectory.copyInto()}
 */
protected void typeCheckVectorCopyIntoArgument(MethodInvocationTree node, List<? extends AnnotatedTypeMirror> params) {
    assert params.size() == 1 : "invalid no. of parameters " + params + " found for method invocation " + node;
    assert node.getArguments().size() == 1 : "invalid no. of arguments in method invocation " + node;
    AnnotatedTypeMirror passed = atypeFactory.getAnnotatedType(node.getArguments().get(0));
    AnnotatedArrayType passedAsArray = (AnnotatedArrayType) passed;
    AnnotatedTypeMirror receiver = atypeFactory.getReceiverType(node);
    if (vectorType == null) {
        vectorType = atypeFactory.fromElement(elements.getTypeElement("java.util.Vector"));
    }
    AnnotatedDeclaredType receiverAsVector = AnnotatedTypes.asSuper(atypeFactory, receiver, vectorType);
    if (receiverAsVector.getTypeArguments().isEmpty()) {
        return;
    }
    AnnotatedTypeMirror argComponent = passedAsArray.getComponentType();
    AnnotatedTypeMirror vectorTypeArg = receiverAsVector.getTypeArguments().get(0);
    Tree errorLocation = node.getArguments().get(0);
    if (TypesUtils.isErasedSubtype(vectorTypeArg.getUnderlyingType(), argComponent.getUnderlyingType(), types)) {
        commonAssignmentCheck(argComponent, vectorTypeArg, errorLocation, "vector.copyinto.type.incompatible");
    } else {
        checker.report(Result.failure("vector.copyinto.type.incompatible", vectorTypeArg, argComponent), errorLocation);
    }
}
Also used : AnnotatedArrayType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType) 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 23 with AnnotatedArrayType

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

the class DefaultReflectionResolver method resolveConstructorCall.

/**
 * Resolves a call to {@link Constructor#newInstance(Object...)}.
 *
 * @param factory the {@link AnnotatedTypeFactory} of the underlying type system
 * @param tree the method invocation tree (representing a constructor call) that has to be
 *     resolved
 * @param origResult the original result from {@code factory.methodFromUse}.
 */
private Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> resolveConstructorCall(AnnotatedTypeFactory factory, MethodInvocationTree tree, Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> origResult) {
    debugReflection("Try to resolve reflective constructor call: " + tree);
    List<JCNewClass> possibleConstructors = resolveReflectiveConstructor(tree, factory);
    // Reflective constructor could not be resolved
    if (possibleConstructors.size() == 0) {
        return origResult;
    }
    Set<? extends AnnotationMirror> returnLub = null;
    Set<? extends AnnotationMirror> paramsGlb = null;
    // parameter types
    for (JCNewClass resolvedTree : possibleConstructors) {
        debugReflection("Resolved constructor invocation: " + resolvedTree);
        if (!checkNewClassArguments(resolvedTree)) {
            debugReflection("Spoofed tree's arguments did not match declaration" + resolvedTree.toString());
            // in QualifierPolymorphism.PolyCollector.visitArray(...)
            continue;
        }
        Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> resolvedResult = factory.constructorFromUse(resolvedTree);
        // Lub return types
        returnLub = lub(returnLub, resolvedResult.first.getReturnType().getAnnotations(), factory);
        // Glb parameter types
        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, parameter type) annotations and set
         * lub/glb annotations from resolved constructors.
         */
    // return value
    origResult.first.getReturnType().clearAnnotations();
    origResult.first.getReturnType().addAnnotations(returnLub);
    // parameter types
    if (paramsGlb != null) {
        AnnotatedArrayType origArrayType = (AnnotatedArrayType) origResult.first.getParameterTypes().get(0);
        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) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) List(java.util.List) ArrayList(java.util.ArrayList) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)23 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)15 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)6 ExpressionTree (com.sun.source.tree.ExpressionTree)4 NewArrayTree (com.sun.source.tree.NewArrayTree)4 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)4 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)3 Tree (com.sun.source.tree.Tree)3 TypeCastTree (com.sun.source.tree.TypeCastTree)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AnnotationMirror (javax.lang.model.element.AnnotationMirror)3 AnnotatedWildcardType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedWildcardType)3 AnnotationTree (com.sun.source.tree.AnnotationTree)2 AssignmentTree (com.sun.source.tree.AssignmentTree)2 ClassTree (com.sun.source.tree.ClassTree)2 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)2 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)2 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)2 IdentifierTree (com.sun.source.tree.IdentifierTree)2