Search in sources :

Example 1 with ParameterizedTypeTree

use of com.sun.source.tree.ParameterizedTypeTree in project error-prone by google.

the class IterablePathParameter method matchVariable.

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
    Type type = ASTHelpers.getType(tree);
    VarSymbol symbol = ASTHelpers.getSymbol(tree);
    if (type == null || symbol == null) {
        return NO_MATCH;
    }
    if (symbol.getKind() != ElementKind.PARAMETER) {
        return NO_MATCH;
    }
    if (!isSameType(type, state.getSymtab().iterableType, state)) {
        return NO_MATCH;
    }
    if (type.getTypeArguments().isEmpty()) {
        return NO_MATCH;
    }
    if (!isSameType(wildBound(getOnlyElement(type.getTypeArguments())), state.getTypeFromString(Path.class.getName()), state)) {
        return NO_MATCH;
    }
    Description.Builder description = buildDescription(tree);
    if (tree.getType() instanceof ParameterizedTypeTree) {
        description.addFix(SuggestedFix.builder().addImport("java.util.Collection").replace(((ParameterizedTypeTree) tree.getType()).getType(), "Collection").build());
    }
    return description.build();
}
Also used : Path(java.nio.file.Path) WildcardType(com.sun.tools.javac.code.Type.WildcardType) ASTHelpers.isSameType(com.google.errorprone.util.ASTHelpers.isSameType) Type(com.sun.tools.javac.code.Type) Description(com.google.errorprone.matchers.Description) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 2 with ParameterizedTypeTree

use of com.sun.source.tree.ParameterizedTypeTree in project checker-framework by typetools.

the class BaseTypeVisitor method warnAboutIrrelevantJavaTypes.

/**
 * Warns if a type annotation is written on a Java type that is not listed in
 * the @RelevantJavaTypes annotation.
 *
 * @param annoTrees annotations written before a variable/method declaration, if this type is from
 *     one; null otherwise. This might contain type annotations that the Java parser attached to
 *     the declaration rather than to the type.
 * @param typeTree the type that any type annotations in annoTrees apply to
 */
public void warnAboutIrrelevantJavaTypes(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree) {
    if (!shouldWarnAboutIrrelevantJavaTypes()) {
        return;
    }
    Tree t = typeTree;
    while (true) {
        switch(t.getKind()) {
            // Recurse for compound types whose top level is not at the far left.
            case ARRAY_TYPE:
                t = ((ArrayTypeTree) t).getType();
                continue;
            case MEMBER_SELECT:
                t = ((MemberSelectTree) t).getExpression();
                continue;
            case PARAMETERIZED_TYPE:
                t = ((ParameterizedTypeTree) t).getType();
                continue;
            // Base cases
            case PRIMITIVE_TYPE:
            case IDENTIFIER:
                List<AnnotationTree> supportedAnnoTrees = supportedAnnoTrees(annoTrees);
                if (!supportedAnnoTrees.isEmpty() && !atypeFactory.isRelevant(TreeUtils.typeOf(t))) {
                    checker.reportError(t, "anno.on.irrelevant", supportedAnnoTrees, t);
                }
                return;
            case ANNOTATED_TYPE:
                AnnotatedTypeTree at = (AnnotatedTypeTree) t;
                ExpressionTree underlying = at.getUnderlyingType();
                List<AnnotationTree> annos = supportedAnnoTrees(at.getAnnotations());
                if (!annos.isEmpty() && !atypeFactory.isRelevant(TreeUtils.typeOf(underlying))) {
                    checker.reportError(t, "anno.on.irrelevant", annos, underlying);
                }
                return;
            default:
                return;
        }
    }
}
Also used : AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) 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) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) 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) IntersectionTypeTree(com.sun.source.tree.IntersectionTypeTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) ModifiersTree(com.sun.source.tree.ModifiersTree) 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) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotationTree(com.sun.source.tree.AnnotationTree)

Example 3 with ParameterizedTypeTree

use of com.sun.source.tree.ParameterizedTypeTree in project checker-framework by typetools.

the class NullnessVisitor method visitAnnotatedType.

@Override
public void visitAnnotatedType(@Nullable List<? extends AnnotationTree> annoTrees, Tree typeTree) {
    // Look for a MEMBER_SELECT or PRIMITIVE within the type.
    Tree t = typeTree;
    while (t != null) {
        switch(t.getKind()) {
            case MEMBER_SELECT:
                Tree expr = ((MemberSelectTree) t).getExpression();
                if (atypeFactory.containsNullnessAnnotation(annoTrees, expr)) {
                    checker.reportError(expr, "nullness.on.outer");
                }
                t = null;
                break;
            case PRIMITIVE_TYPE:
                if (atypeFactory.containsNullnessAnnotation(annoTrees, t)) {
                    checker.reportError(t, "nullness.on.primitive");
                }
                t = null;
                break;
            case ANNOTATED_TYPE:
                AnnotatedTypeTree at = ((AnnotatedTypeTree) t);
                Tree underlying = at.getUnderlyingType();
                if (underlying.getKind() == Tree.Kind.PRIMITIVE_TYPE) {
                    if (atypeFactory.containsNullnessAnnotation(null, at)) {
                        checker.reportError(t, "nullness.on.primitive");
                    }
                    t = null;
                } else {
                    t = underlying;
                }
                break;
            case ARRAY_TYPE:
                t = ((ArrayTypeTree) t).getType();
                break;
            case PARAMETERIZED_TYPE:
                t = ((ParameterizedTypeTree) t).getType();
                break;
            default:
                t = null;
                break;
        }
    }
    super.visitAnnotatedType(annoTrees, typeTree);
}
Also used : AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) TypeCastTree(com.sun.source.tree.TypeCastTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ForLoopTree(com.sun.source.tree.ForLoopTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) SwitchTree(com.sun.source.tree.SwitchTree) ThrowTree(com.sun.source.tree.ThrowTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) UnaryTree(com.sun.source.tree.UnaryTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) ExpressionTree(com.sun.source.tree.ExpressionTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree)

Example 4 with ParameterizedTypeTree

use of com.sun.source.tree.ParameterizedTypeTree in project checker-framework by typetools.

the class BaseTypeValidator method visitDeclared.

@Override
public Void visitDeclared(AnnotatedDeclaredType type, Tree tree) {
    if (visitedNodes.containsKey(type)) {
        return visitedNodes.get(type);
    }
    final boolean skipChecks = checker.shouldSkipUses(type.getUnderlyingType().asElement());
    if (checkTopLevelDeclaredOrPrimitiveType && !skipChecks) {
        // Ensure that type use is a subtype of the element type
        // isValidUse determines the erasure of the types.
        Set<AnnotationMirror> bounds = atypeFactory.getTypeDeclarationBounds(type.getUnderlyingType());
        AnnotatedDeclaredType elemType = type.deepCopy();
        elemType.clearPrimaryAnnotations();
        elemType.addAnnotations(bounds);
        if (!visitor.isValidUse(elemType, type, tree)) {
            reportInvalidAnnotationsOnUse(type, tree);
        }
    }
    // Set checkTopLevelDeclaredType to true, because the next time visitDeclared is called,
    // the type isn't the top level, so always do the check.
    checkTopLevelDeclaredOrPrimitiveType = true;
    if (TreeUtils.isClassTree(tree)) {
        visitedNodes.put(type, null);
        visitClassTypeParameters(type, (ClassTree) tree);
        return null;
    }
    /*
     * Try to reconstruct the ParameterizedTypeTree from the given tree.
     * TODO: there has to be a nicer way to do this...
     */
    Pair<ParameterizedTypeTree, AnnotatedDeclaredType> p = extractParameterizedTypeTree(tree, type);
    ParameterizedTypeTree typeArgTree = p.first;
    type = p.second;
    if (typeArgTree == null) {
        return super.visitDeclared(type, tree);
    }
    // else
    // We put this here because we don't want to put it in visitedNodes before calling
    // super (in the else branch) because that would cause the super implementation
    // to detect that we've already visited type and to immediately return.
    visitedNodes.put(type, null);
    // We have a ParameterizedTypeTree -> visit it.
    visitParameterizedType(type, typeArgTree);
    /*
     * Instead of calling super with the unchanged "tree", adapt the
     * second argument to be the corresponding type argument tree. This
     * ensures that the first and second parameter to this method always
     * correspond. visitDeclared is the only method that had this
     * problem.
     */
    List<? extends AnnotatedTypeMirror> tatypes = type.getTypeArguments();
    if (tatypes == null) {
        return null;
    }
    // May be zero for a "diamond" (inferred type args in constructor invocation).
    int numTypeArgs = typeArgTree.getTypeArguments().size();
    if (numTypeArgs != 0) {
        // but I didn't manage to reduce it to a test case.
        assert tatypes.size() <= numTypeArgs || skipChecks : "size mismatch for type arguments: " + type + " and " + typeArgTree;
        for (int i = 0; i < tatypes.size(); ++i) {
            scan(tatypes.get(i), typeArgTree.getTypeArguments().get(i));
        }
    }
    return null;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)

Example 5 with ParameterizedTypeTree

use of com.sun.source.tree.ParameterizedTypeTree in project checker-framework by typetools.

the class TypeFromTypeTreeVisitor method visitParameterizedType.

@Override
public AnnotatedTypeMirror visitParameterizedType(ParameterizedTypeTree node, AnnotatedTypeFactory f) {
    ClassSymbol baseType = (ClassSymbol) TreeUtils.elementFromTree(node.getType());
    updateWildcardBounds(node.getTypeArguments(), baseType.getTypeParameters());
    List<AnnotatedTypeMirror> args = CollectionsPlume.mapList((Tree t) -> visit(t, f), node.getTypeArguments());
    // use creator?
    AnnotatedTypeMirror result = f.type(node);
    AnnotatedTypeMirror atype = visit(node.getType(), f);
    result.addAnnotations(atype.getAnnotations());
    // diamond which should be inferred.
    if (result instanceof AnnotatedDeclaredType && !args.isEmpty()) {
        assert result instanceof AnnotatedDeclaredType : node + " --> " + result;
        ((AnnotatedDeclaredType) result).setTypeArguments(args);
    }
    return result;
}
Also used : ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) AnnotatedDeclaredType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) MethodTree(com.sun.source.tree.MethodTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) TypeParameterTree(com.sun.source.tree.TypeParameterTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) IntersectionTypeTree(com.sun.source.tree.IntersectionTypeTree) WildcardTree(com.sun.source.tree.WildcardTree) UnionTypeTree(com.sun.source.tree.UnionTypeTree)

Aggregations

ParameterizedTypeTree (com.sun.source.tree.ParameterizedTypeTree)7 AnnotatedTypeTree (com.sun.source.tree.AnnotatedTypeTree)4 ClassTree (com.sun.source.tree.ClassTree)4 ExpressionTree (com.sun.source.tree.ExpressionTree)4 IdentifierTree (com.sun.source.tree.IdentifierTree)4 MethodTree (com.sun.source.tree.MethodTree)4 Tree (com.sun.source.tree.Tree)4 ArrayTypeTree (com.sun.source.tree.ArrayTypeTree)3 MemberSelectTree (com.sun.source.tree.MemberSelectTree)3 NewClassTree (com.sun.source.tree.NewClassTree)3 VariableTree (com.sun.source.tree.VariableTree)3 AnnotationTree (com.sun.source.tree.AnnotationTree)2 CatchTree (com.sun.source.tree.CatchTree)2 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)2 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)2 EnhancedForLoopTree (com.sun.source.tree.EnhancedForLoopTree)2 InstanceOfTree (com.sun.source.tree.InstanceOfTree)2 IntersectionTypeTree (com.sun.source.tree.IntersectionTypeTree)2 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)2 NewArrayTree (com.sun.source.tree.NewArrayTree)2