Class MustCallVisitor

All Implemented Interfaces:
TreeVisitor<Void,Void>

public class MustCallVisitor extends BaseTypeVisitor<MustCallAnnotatedTypeFactory>
The visitor for the Must Call Checker. This visitor is similar to BaseTypeVisitor, but overrides methods that don't work well with the MustCall type hierarchy because it doesn't use the top type as the default type.
  • Constructor Details

    • MustCallVisitor

      public MustCallVisitor(BaseTypeChecker checker)
      Creates a new MustCallVisitor.
      Parameters:
      checker - the type-checker associated with this visitor
  • Method Details

    • visitReturn

      public Void visitReturn(ReturnTree tree, Void p)
      Description copied from class: BaseTypeVisitor
      Checks that the type of the return expression is a subtype of the enclosing method required return type. If not, it issues a "return.type.incompatible" error.
      Specified by:
      visitReturn in interface TreeVisitor<Void,Void>
      Overrides:
      visitReturn in class BaseTypeVisitor<MustCallAnnotatedTypeFactory>
    • visitAssignment

      public Void visitAssignment(AssignmentTree tree, Void p)
      Description copied from class: BaseTypeVisitor
      Performs two checks: subtyping and assignability checks, using BaseTypeVisitor.commonAssignmentCheck(Tree, ExpressionTree, String, Object[]).

      If the subtype check fails, it issues an "assignment.type.incompatible" error.

      Specified by:
      visitAssignment in interface TreeVisitor<Void,Void>
      Overrides:
      visitAssignment in class BaseTypeVisitor<MustCallAnnotatedTypeFactory>
    • validateType

      protected boolean validateType(Tree tree, AnnotatedTypeMirror type)
      Description copied from class: BaseTypeVisitor
      Returns true if the type and corresponding type tree is a valid type, and emits an error if that is not the case (e.g. '@Mutable String'). If the tree is a method or constructor, tests the return type.

      A type is invalid if it is ill-formed, for example if it has two qualifiers of one hierarchy or a qualifier that the type system forbids at that location. The checks that compare such a type with another, such as the assignment, return, enhanced for, new, and cast checks, are skipped: comparing an ill-formed type is not meaningful, and some comparisons assume one qualifier per hierarchy. An override that returns false must therefore have reported an error. A type argument that is outside the bounds of its type parameter is reported, but leaves the type valid: the type is well-formed, so the other checks still apply to it.

      Overrides:
      validateType in class BaseTypeVisitor<MustCallAnnotatedTypeFactory>
      Parameters:
      tree - the type tree supplied by the user
      type - the type corresponding to tree
      Returns:
      true if the type is valid
    • isValidUse

      public boolean isValidUse(AnnotatedTypeMirror.AnnotatedDeclaredType declarationType, AnnotatedTypeMirror.AnnotatedDeclaredType useType, Tree tree)
      Description copied from class: BaseTypeVisitor
      Tests that the qualifiers present on useType are valid qualifiers, given the qualifiers on the declaration of the type, declarationType.

      The check is shallow, as it does not descend into generic or array types (i.e. only performing the validity check on the raw type or outermost array dimension). BaseTypeVisitor.validateTypeOf(Tree) would call this for each type argument or array dimension separately.

      In most cases, useType simply needs to be a subtype of declarationType. If a type system makes exceptions to this rule, its implementation should override this method.

      This method is not called if BaseTypeValidator.shouldCheckTopLevelDeclaredOrPrimitiveType(AnnotatedTypeMirror, Tree) returns false -- by default, it is not called on the top level for locals and expressions. To enforce a type validity property everywhere, override methods such as BaseTypeValidator.visitDeclared(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType, com.sun.source.tree.Tree) rather than this method.

      Overrides:
      isValidUse in class BaseTypeVisitor<MustCallAnnotatedTypeFactory>
      Parameters:
      declarationType - the type of the class (TypeElement)
      useType - the use of the class (instance type)
      tree - the tree where the type is used
      Returns:
      true if the useType is a valid use of elemType
    • skipReceiverSubtypeCheck

      protected boolean skipReceiverSubtypeCheck(MethodInvocationTree tree, AnnotatedTypeMirror methodDefinitionReceiver, AnnotatedTypeMirror methodCallReceiver)
      Description copied from class: BaseTypeVisitor
      Returns true if to skip subtype checks on the receiver when checking method invocability. A visitor may, for example, allow a method to be invoked even if the receivers are siblings in a hierarchy, provided that some other condition (implemented by the visitor) is satisfied.
      Overrides:
      skipReceiverSubtypeCheck in class BaseTypeVisitor<MustCallAnnotatedTypeFactory>
      Parameters:
      tree - the method invocation tree
      methodDefinitionReceiver - the ATM of the receiver of the method definition
      methodCallReceiver - the ATM of the receiver of the method call
      Returns:
      true if to skip subtype checks on the receiver
    • checkConstructorResult

      protected void checkConstructorResult(AnnotatedTypeMirror.AnnotatedExecutableType constructorType, MethodTree constructorTree)
      This method typically issues a warning if the result type of the constructor is not top, because in top-default type systems that indicates a potential problem. The Must Call Checker does not need this warning, because it expects the type of all constructors to be MustCall({}) (by default) or some other MustCall type, not the top type.

      Instead, this method checks that the result type of a constructor is a supertype of the declared type on the class, if one exists.

      Overrides:
      checkConstructorResult in class BaseTypeVisitor<MustCallAnnotatedTypeFactory>
      Parameters:
      constructorType - an AnnotatedExecutableType for the constructor
      constructorTree - the declaration of the constructor
    • getExceptionParameterLowerBoundAnnotations

      protected AnnotationMirrorSet getExceptionParameterLowerBoundAnnotations()
      Change the default for exception parameter lower bounds to bottom (the default), to prevent false positives. This is unsound; see the discussion on https://github.com/typetools/checker-framework/issues/3839.

      TODO: change checking of throws clauses to require that the thrown exception is @MustCall({}). This would probably eliminate most of the same false positives, without adding undue false positives.

      Overrides:
      getExceptionParameterLowerBoundAnnotations in class BaseTypeVisitor<MustCallAnnotatedTypeFactory>
      Returns:
      a set containing only the @MustCall({}) annotation
    • visitAnnotation

      public Void visitAnnotation(AnnotationTree tree, Void p)
      Does not issue any warnings.

      This implementation prevents recursing into annotation arguments. Annotation arguments are literals, which don't have must-call obligations.

      Annotation arguments are treated as return locations for the purposes of defaulting, rather than parameter locations. This causes them to default incorrectly when the annotation is defined in bytecode. See https://github.com/typetools/checker-framework/issues/3178 for an explanation of why this is necessary to avoid false positives.

      Specified by:
      visitAnnotation in interface TreeVisitor<Void,Void>
      Overrides:
      visitAnnotation in class BaseTypeVisitor<MustCallAnnotatedTypeFactory>