Search in sources :

Example 41 with ClassTree

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

the class CFGTranslationPhaseOne method visitBindingPattern17.

/**
 * Visit a BindingPatternTree
 *
 * @param bindingPatternTree a BindingPatternTree, typed as Tree to be backward-compatible
 * @param p parameter
 * @return the result of visiting the binding pattern tree
 */
public Node visitBindingPattern17(Tree bindingPatternTree, Void p) {
    ClassTree enclosingClass = TreePathUtil.enclosingClass(getCurrentPath());
    TypeElement classElem = TreeUtils.elementFromDeclaration(enclosingClass);
    Node receiver = new ImplicitThisNode(classElem.asType());
    VariableTree varTree = TreeUtils.bindingPatternTreeGetVariable(bindingPatternTree);
    LocalVariableNode varNode = new LocalVariableNode(varTree, receiver);
    extendWithNode(varNode);
    return varNode;
}
Also used : ImplicitThisNode(org.checkerframework.dataflow.cfg.node.ImplicitThisNode) TypeElement(javax.lang.model.element.TypeElement) NumericalMultiplicationNode(org.checkerframework.dataflow.cfg.node.NumericalMultiplicationNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) UnsignedRightShiftNode(org.checkerframework.dataflow.cfg.node.UnsignedRightShiftNode) LeftShiftNode(org.checkerframework.dataflow.cfg.node.LeftShiftNode) PrimitiveTypeNode(org.checkerframework.dataflow.cfg.node.PrimitiveTypeNode) FloatLiteralNode(org.checkerframework.dataflow.cfg.node.FloatLiteralNode) LessThanNode(org.checkerframework.dataflow.cfg.node.LessThanNode) BitwiseOrNode(org.checkerframework.dataflow.cfg.node.BitwiseOrNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) EqualToNode(org.checkerframework.dataflow.cfg.node.EqualToNode) NumericalPlusNode(org.checkerframework.dataflow.cfg.node.NumericalPlusNode) ConditionalAndNode(org.checkerframework.dataflow.cfg.node.ConditionalAndNode) VariableDeclarationNode(org.checkerframework.dataflow.cfg.node.VariableDeclarationNode) ClassDeclarationNode(org.checkerframework.dataflow.cfg.node.ClassDeclarationNode) IntegerDivisionNode(org.checkerframework.dataflow.cfg.node.IntegerDivisionNode) AssertionErrorNode(org.checkerframework.dataflow.cfg.node.AssertionErrorNode) InstanceOfNode(org.checkerframework.dataflow.cfg.node.InstanceOfNode) BooleanLiteralNode(org.checkerframework.dataflow.cfg.node.BooleanLiteralNode) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) NullLiteralNode(org.checkerframework.dataflow.cfg.node.NullLiteralNode) ArrayTypeNode(org.checkerframework.dataflow.cfg.node.ArrayTypeNode) LambdaResultExpressionNode(org.checkerframework.dataflow.cfg.node.LambdaResultExpressionNode) IntegerRemainderNode(org.checkerframework.dataflow.cfg.node.IntegerRemainderNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ConditionalOrNode(org.checkerframework.dataflow.cfg.node.ConditionalOrNode) NotEqualNode(org.checkerframework.dataflow.cfg.node.NotEqualNode) BitwiseXorNode(org.checkerframework.dataflow.cfg.node.BitwiseXorNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) StringConcatenateNode(org.checkerframework.dataflow.cfg.node.StringConcatenateNode) NullChkNode(org.checkerframework.dataflow.cfg.node.NullChkNode) CharacterLiteralNode(org.checkerframework.dataflow.cfg.node.CharacterLiteralNode) FloatingDivisionNode(org.checkerframework.dataflow.cfg.node.FloatingDivisionNode) FunctionalInterfaceNode(org.checkerframework.dataflow.cfg.node.FunctionalInterfaceNode) StringConcatenateAssignmentNode(org.checkerframework.dataflow.cfg.node.StringConcatenateAssignmentNode) TypeCastNode(org.checkerframework.dataflow.cfg.node.TypeCastNode) MethodAccessNode(org.checkerframework.dataflow.cfg.node.MethodAccessNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) LongLiteralNode(org.checkerframework.dataflow.cfg.node.LongLiteralNode) MarkerNode(org.checkerframework.dataflow.cfg.node.MarkerNode) ImplicitThisNode(org.checkerframework.dataflow.cfg.node.ImplicitThisNode) FloatingRemainderNode(org.checkerframework.dataflow.cfg.node.FloatingRemainderNode) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) PackageNameNode(org.checkerframework.dataflow.cfg.node.PackageNameNode) DoubleLiteralNode(org.checkerframework.dataflow.cfg.node.DoubleLiteralNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) IntegerLiteralNode(org.checkerframework.dataflow.cfg.node.IntegerLiteralNode) SignedRightShiftNode(org.checkerframework.dataflow.cfg.node.SignedRightShiftNode) ThrowNode(org.checkerframework.dataflow.cfg.node.ThrowNode) GreaterThanOrEqualNode(org.checkerframework.dataflow.cfg.node.GreaterThanOrEqualNode) StringLiteralNode(org.checkerframework.dataflow.cfg.node.StringLiteralNode) TernaryExpressionNode(org.checkerframework.dataflow.cfg.node.TernaryExpressionNode) BitwiseAndNode(org.checkerframework.dataflow.cfg.node.BitwiseAndNode) ParameterizedTypeNode(org.checkerframework.dataflow.cfg.node.ParameterizedTypeNode) CaseNode(org.checkerframework.dataflow.cfg.node.CaseNode) SwitchExpressionNode(org.checkerframework.dataflow.cfg.node.SwitchExpressionNode) NumericalAdditionNode(org.checkerframework.dataflow.cfg.node.NumericalAdditionNode) NumericalSubtractionNode(org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode) BitwiseComplementNode(org.checkerframework.dataflow.cfg.node.BitwiseComplementNode) ConditionalNotNode(org.checkerframework.dataflow.cfg.node.ConditionalNotNode) NumericalMinusNode(org.checkerframework.dataflow.cfg.node.NumericalMinusNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) GreaterThanNode(org.checkerframework.dataflow.cfg.node.GreaterThanNode) LessThanOrEqualNode(org.checkerframework.dataflow.cfg.node.LessThanOrEqualNode) SynchronizedNode(org.checkerframework.dataflow.cfg.node.SynchronizedNode) Node(org.checkerframework.dataflow.cfg.node.Node) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Example 42 with ClassTree

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

the class CFGTranslationPhaseOne method findOwner.

/**
 * Find nearest owner element (Method or Class) which holds current tree.
 *
 * @return nearest owner element of current tree
 */
private Element findOwner() {
    Tree enclosingMethodOrLambda = TreePathUtil.enclosingMethodOrLambda(getCurrentPath());
    if (enclosingMethodOrLambda != null) {
        if (enclosingMethodOrLambda.getKind() == Kind.METHOD) {
            return TreeUtils.elementFromDeclaration((MethodTree) enclosingMethodOrLambda);
        } else {
            // The current path is in a lambda tree.  In this case the owner is either a method or
            // an initializer block.
            LambdaExpressionTree lambdaTree = (LambdaExpressionTree) enclosingMethodOrLambda;
            if (!lambdaTree.getParameters().isEmpty()) {
                // If there is a lambda parameter, use the same owner.
                return TreeUtils.elementFromDeclaration(lambdaTree.getParameters().get(0)).getEnclosingElement();
            }
            // If there are no lambda parameters then if the lambda is enclosed in a method, that's the
            // owner.
            MethodTree enclosingMethod = TreePathUtil.enclosingMethod(getCurrentPath());
            if (enclosingMethod != null) {
                return TreeUtils.elementFromDeclaration(enclosingMethod);
            }
            // If the lambda is not enclosed in a method, then the owner should be a constructor. javac
            // seems to use the last constructor in the list. (If the lambda is in an initializer of a
            // static field then the owner should be a static initializer block, but there doesn't seem
            // to be a way to get a reference to the static initializer element.)
            ClassTree enclosingClass = TreePathUtil.enclosingClass(getCurrentPath());
            TypeElement typeElement = TreeUtils.elementFromDeclaration(enclosingClass);
            ExecutableElement constructor = null;
            for (Element enclosing : typeElement.getEnclosedElements()) {
                if (enclosing.getKind() == ElementKind.CONSTRUCTOR) {
                    constructor = (ExecutableElement) enclosing;
                }
            }
            return constructor;
        }
    } else {
        ClassTree enclosingClass = TreePathUtil.enclosingClass(getCurrentPath());
        return TreeUtils.elementFromDeclaration(enclosingClass);
    }
}
Also used : LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) MethodTree(com.sun.source.tree.MethodTree) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) ForLoopTree(com.sun.source.tree.ForLoopTree) InstanceOfTree(com.sun.source.tree.InstanceOfTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ConditionalExpressionTree(com.sun.source.tree.ConditionalExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ThrowTree(com.sun.source.tree.ThrowTree) BlockTree(com.sun.source.tree.BlockTree) EnhancedForLoopTree(com.sun.source.tree.EnhancedForLoopTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayTypeTree(com.sun.source.tree.ArrayTypeTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) 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) BreakTree(com.sun.source.tree.BreakTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) WildcardTree(com.sun.source.tree.WildcardTree) UnionTypeTree(com.sun.source.tree.UnionTypeTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) AnnotatedTypeTree(com.sun.source.tree.AnnotatedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) NewArrayTree(com.sun.source.tree.NewArrayTree) ContinueTree(com.sun.source.tree.ContinueTree) CaseTree(com.sun.source.tree.CaseTree) CompilationUnitTree(com.sun.source.tree.CompilationUnitTree) SwitchTree(com.sun.source.tree.SwitchTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) AssertTree(com.sun.source.tree.AssertTree) StatementTree(com.sun.source.tree.StatementTree) ModifiersTree(com.sun.source.tree.ModifiersTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) AnnotationTree(com.sun.source.tree.AnnotationTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) EmptyStatementTree(com.sun.source.tree.EmptyStatementTree) ClassTree(com.sun.source.tree.ClassTree) IfTree(com.sun.source.tree.IfTree) MemberReferenceTree(com.sun.source.tree.MemberReferenceTree) ErroneousTree(com.sun.source.tree.ErroneousTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) TryTree(com.sun.source.tree.TryTree)

Example 43 with ClassTree

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

the class BaseTypeVisitor method checkFieldInvariantDeclarations.

/**
 * Check that the field invariant declaration annotations meet the following requirements:
 *
 * <ol>
 *   <!-- The item numbering is referred to in the body of the method.-->
 *   <li value="1">If the superclass of {@code classTree} has a field invariant, then the field
 *       invariant for {@code classTree} must include all the fields in the superclass invariant
 *       and those fields' annotations must be a subtype (or equal) to the annotations for those
 *       fields in the superclass.
 *   <li value="2">The fields in the invariant must be a.) final and b.) declared in a superclass
 *       of {@code classTree}.
 *   <li value="3">The qualifier for each field must be a subtype of the annotation on the
 *       declaration of that field.
 *   <li value="4">The field invariant has an equal number of fields and qualifiers, or it has one
 *       qualifier and at least one field.
 * </ol>
 *
 * @param classTree class that might have a field invariant
 * @checker_framework.manual #field-invariants Field invariants
 */
protected void checkFieldInvariantDeclarations(ClassTree classTree) {
    TypeElement elt = TreeUtils.elementFromDeclaration(classTree);
    FieldInvariants invariants = atypeFactory.getFieldInvariants(elt);
    if (invariants == null) {
        // No invariants to check
        return;
    }
    // Where to issue an error, if any.
    Tree errorTree = atypeFactory.getFieldInvariantAnnotationTree(classTree.getModifiers().getAnnotations());
    if (errorTree == null) {
        // If the annotation was inherited, then there is no annotation tree, so issue the
        // error on the class.
        errorTree = classTree;
    }
    // Checks #4 (see method Javadoc)
    if (!invariants.isWellFormed()) {
        checker.reportError(errorTree, "field.invariant.not.wellformed");
        return;
    }
    TypeMirror superClass = elt.getSuperclass();
    List<String> fieldsNotFound = new ArrayList<>(invariants.getFields());
    Set<VariableElement> fieldElts = ElementUtils.findFieldsInTypeOrSuperType(superClass, fieldsNotFound);
    // Checks that fields are declared in super class. (#2b)
    if (!fieldsNotFound.isEmpty()) {
        String notFoundString = String.join(", ", fieldsNotFound);
        checker.reportError(errorTree, "field.invariant.not.found", notFoundString);
    }
    FieldInvariants superInvar = atypeFactory.getFieldInvariants(TypesUtils.getTypeElement(superClass));
    if (superInvar != null) {
        // Checks #3 (see method Javadoc)
        DiagMessage superError = invariants.isSuperInvariant(superInvar, atypeFactory);
        if (superError != null) {
            checker.report(errorTree, superError);
        }
    }
    List<String> notFinal = new ArrayList<>();
    for (VariableElement field : fieldElts) {
        String fieldName = field.getSimpleName().toString();
        if (!ElementUtils.isFinal(field)) {
            notFinal.add(fieldName);
        }
        AnnotatedTypeMirror type = atypeFactory.getAnnotatedType(field);
        List<AnnotationMirror> annos = invariants.getQualifiersFor(field.getSimpleName());
        for (AnnotationMirror invariantAnno : annos) {
            AnnotationMirror declaredAnno = type.getEffectiveAnnotationInHierarchy(invariantAnno);
            if (declaredAnno == null) {
                // invariant anno isn't in this hierarchy
                continue;
            }
            if (!atypeFactory.getQualifierHierarchy().isSubtype(invariantAnno, declaredAnno)) {
                // Checks #3
                checker.reportError(errorTree, "field.invariant.not.subtype", fieldName, invariantAnno, declaredAnno);
            }
        }
    }
    // Checks #2a
    if (!notFinal.isEmpty()) {
        String notFinalString = String.join(", ", notFinal);
        checker.reportError(errorTree, "field.invariant.not.final", notFinalString);
    }
}
Also used : FieldInvariants(org.checkerframework.framework.util.FieldInvariants) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) DiagMessage(org.checkerframework.framework.source.DiagMessage) 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)

Example 44 with ClassTree

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

the class BaseTypeVisitor method checkQualifierParameter.

/**
 * Issues an error if {@code classTree} has polymorphic fields but is not annotated with
 * {@code @HasQualifierParameter}. Always issue a warning if the type of a static field is
 * annotated with a polymorphic qualifier.
 *
 * <p>Issues an error if {@code classTree} extends or implements a class/interface that has a
 * qualifier parameter, but this class does not.
 *
 * @param classTree the ClassTree to check for polymorphic fields
 */
protected void checkQualifierParameter(ClassTree classTree) {
    // Set of polymorphic qualifiers for hierarchies that do not have a qualifier parameter and
    // therefor cannot appear on a field.
    Set<AnnotationMirror> illegalOnFieldsPolyQual = AnnotationUtils.createAnnotationSet();
    // Set of polymorphic annotations for all hierarchies
    Set<AnnotationMirror> polys = AnnotationUtils.createAnnotationSet();
    TypeElement classElement = TreeUtils.elementFromDeclaration(classTree);
    for (AnnotationMirror top : atypeFactory.getQualifierHierarchy().getTopAnnotations()) {
        AnnotationMirror poly = atypeFactory.getQualifierHierarchy().getPolymorphicAnnotation(top);
        if (poly != null) {
            polys.add(poly);
        }
        if (atypeFactory.hasExplicitQualifierParameterInHierarchy(classElement, top) && atypeFactory.hasExplicitNoQualifierParameterInHierarchy(classElement, top)) {
            checker.reportError(classTree, "conflicting.qual.param", top);
        }
        if (atypeFactory.hasQualifierParameterInHierarchy(classElement, top)) {
            continue;
        }
        if (poly != null) {
            illegalOnFieldsPolyQual.add(poly);
        }
        Element extendsEle = TypesUtils.getTypeElement(classElement.getSuperclass());
        if (extendsEle != null && atypeFactory.hasQualifierParameterInHierarchy(extendsEle, top)) {
            checker.reportError(classTree, "missing.has.qual.param", top);
        } else {
            for (TypeMirror interfaceType : classElement.getInterfaces()) {
                Element interfaceEle = TypesUtils.getTypeElement(interfaceType);
                if (atypeFactory.hasQualifierParameterInHierarchy(interfaceEle, top)) {
                    checker.reportError(classTree, "missing.has.qual.param", top);
                    // only issue error once
                    break;
                }
            }
        }
    }
    for (Tree mem : classTree.getMembers()) {
        if (mem.getKind() == Tree.Kind.VARIABLE) {
            AnnotatedTypeMirror fieldType = atypeFactory.getAnnotatedType(mem);
            List<DiagMessage> hasIllegalPoly;
            if (ElementUtils.isStatic(TreeUtils.elementFromDeclaration((VariableTree) mem))) {
                // A polymorphic qualifier is not allowed on a static field even if the class
                // has a qualifier parameter.
                hasIllegalPoly = polyScanner.visit(fieldType, polys);
            } else {
                hasIllegalPoly = polyScanner.visit(fieldType, illegalOnFieldsPolyQual);
            }
            for (DiagMessage dm : hasIllegalPoly) {
                checker.report(mem, dm);
            }
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) DiagMessage(org.checkerframework.framework.source.DiagMessage) TypeElement(javax.lang.model.element.TypeElement) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableTree(com.sun.source.tree.VariableTree) 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) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 45 with ClassTree

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

the class UpperBoundVisitor method visitAnnotation.

/**
 * Warns about LTLengthOf annotations with arguments whose lengths do not match.
 */
@Override
public Void visitAnnotation(AnnotationTree node, Void p) {
    AnnotationMirror anno = TreeUtils.annotationFromAnnotationTree(node);
    if (atypeFactory.areSameByClass(anno, LTLengthOf.class)) {
        List<? extends ExpressionTree> args = node.getArguments();
        if (args.size() == 2) {
            // If offsets are provided, there must be the same number of them as there are arrays.
            List<String> sequences = AnnotationUtils.getElementValueArray(anno, atypeFactory.ltLengthOfValueElement, String.class);
            List<String> offsets = AnnotationUtils.getElementValueArray(anno, atypeFactory.ltLengthOfOffsetElement, String.class, Collections.emptyList());
            if (sequences.size() != offsets.size() && !offsets.isEmpty()) {
                checker.reportError(node, "different.length.sequences.offsets", sequences.size(), offsets.size());
                return null;
            }
        }
    } else if (atypeFactory.areSameByClass(anno, HasSubsequence.class)) {
        // Check that the arguments to a HasSubsequence annotation are valid JavaExpressions,
        // and issue an error if one of them is not.
        String seq = atypeFactory.hasSubsequenceSubsequenceValue(anno);
        String from = atypeFactory.hasSubsequenceFromValue(anno);
        String to = atypeFactory.hasSubsequenceToValue(anno);
        // check that each expression is parsable at the declaration of this class
        ClassTree enclosingClass = TreePathUtil.enclosingClass(getCurrentPath());
        checkEffectivelyFinalAndParsable(seq, enclosingClass, node);
        checkEffectivelyFinalAndParsable(from, enclosingClass, node);
        checkEffectivelyFinalAndParsable(to, enclosingClass, node);
    }
    return super.visitAnnotation(node, p);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ClassTree(com.sun.source.tree.ClassTree) HasSubsequence(org.checkerframework.checker.index.qual.HasSubsequence)

Aggregations

ClassTree (com.sun.source.tree.ClassTree)119 Tree (com.sun.source.tree.Tree)76 MethodTree (com.sun.source.tree.MethodTree)66 VariableTree (com.sun.source.tree.VariableTree)59 NewClassTree (com.sun.source.tree.NewClassTree)48 ExpressionTree (com.sun.source.tree.ExpressionTree)45 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)40 CompilationUnitTree (com.sun.source.tree.CompilationUnitTree)31 AnnotationTree (com.sun.source.tree.AnnotationTree)29 BlockTree (com.sun.source.tree.BlockTree)28 IdentifierTree (com.sun.source.tree.IdentifierTree)27 NewArrayTree (com.sun.source.tree.NewArrayTree)26 AssignmentTree (com.sun.source.tree.AssignmentTree)25 MemberSelectTree (com.sun.source.tree.MemberSelectTree)25 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)24 TypeElement (javax.lang.model.element.TypeElement)24 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)23 ArrayList (java.util.ArrayList)23 ReturnTree (com.sun.source.tree.ReturnTree)22 TreePath (com.sun.source.util.TreePath)22