Class NullnessVisitor

All Implemented Interfaces:
com.sun.source.tree.TreeVisitor<Void,Void>

The visitor for the nullness type-system.
  • Constructor Details

    • NullnessVisitor

      public NullnessVisitor(BaseTypeChecker checker)
      Create a new NullnessVisitor.
      Parameters:
      checker - the checker to which this visitor belongs
  • Method Details

    • createTypeFactory

      public NullnessAnnotatedTypeFactory createTypeFactory()
      Description copied from class: BaseTypeVisitor
      Constructs an instance of the appropriate type factory for the implemented type system.

      The default implementation uses the checker naming convention to create the appropriate type factory. If no factory is found, it returns BaseAnnotatedTypeFactory. It reflectively invokes the constructor that accepts this checker and compilation unit tree (in that order) as arguments.

      Subclasses have to override this method to create the appropriate visitor if they do not follow the checker naming convention.

      Overrides:
      createTypeFactory in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
      Returns:
      the appropriate type factory
    • isValidUse

      public boolean isValidUse(AnnotatedTypeMirror.AnnotatedPrimitiveType type, com.sun.source.tree.Tree tree)
      Description copied from class: BaseTypeVisitor
      Tests that the qualifiers present on the primitive type are valid.
      Overrides:
      isValidUse in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
      Parameters:
      type - the use of the primitive type
      tree - the tree where the type is used
      Returns:
      true if the type is a valid use of the primitive type
    • commonAssignmentCheck

      protected void commonAssignmentCheck(com.sun.source.tree.Tree varTree, com.sun.source.tree.ExpressionTree valueExp, @CompilerMessageKey String errorKey, Object... extraArgs)
      Description copied from class: BaseTypeVisitor
      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.
      Overrides:
      commonAssignmentCheck in class InitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue,NullnessStore>
      Parameters:
      varTree - the AST node for the lvalue (usually a variable)
      valueExp - the AST node for the rvalue (the new value)
      errorKey - the error message key to use if the check fails
      extraArgs - arguments to the error message key, before "found" and "expected" types
    • commonAssignmentCheck

      protected void commonAssignmentCheck(AnnotatedTypeMirror varType, com.sun.source.tree.ExpressionTree valueExp, @CompilerMessageKey String errorKey, Object... extraArgs)
      Description copied from class: BaseTypeVisitor
      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.
      Overrides:
      commonAssignmentCheck in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
      Parameters:
      varType - the annotated type for the lvalue (usually a variable)
      valueExp - the AST node for the rvalue (the new value)
      errorKey - the error message key to use if the check fails
      extraArgs - arguments to the error message key, before "found" and "expected" types
    • commonAssignmentCheck

      @FormatMethod protected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, com.sun.source.tree.Tree valueTree, @CompilerMessageKey String errorKey, Object... extraArgs)
      Description copied from class: BaseTypeVisitor
      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.
      Overrides:
      commonAssignmentCheck in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
      Parameters:
      varType - the annotated type of the variable
      valueType - the annotated type of the value
      valueTree - the location to use when reporting the error message
      errorKey - the error message key to use if the check fails
      extraArgs - arguments to the error message key, before "found" and "expected" types
    • visitMemberSelect

      public Void visitMemberSelect(com.sun.source.tree.MemberSelectTree node, Void p)
      Case 1: Check for null dereferencing.
      Specified by:
      visitMemberSelect in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitMemberSelect in class com.sun.source.util.TreeScanner<Void,Void>
    • visitEnhancedForLoop

      public Void visitEnhancedForLoop(com.sun.source.tree.EnhancedForLoopTree node, Void p)
      Case 2: Check for implicit .iterator call.
      Specified by:
      visitEnhancedForLoop in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitEnhancedForLoop in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
    • visitArrayAccess

      public Void visitArrayAccess(com.sun.source.tree.ArrayAccessTree node, Void p)
      Case 3: Check for array dereferencing.
      Specified by:
      visitArrayAccess in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitArrayAccess in class com.sun.source.util.TreeScanner<Void,Void>
    • visitNewArray

      public Void visitNewArray(com.sun.source.tree.NewArrayTree node, Void p)
      Specified by:
      visitNewArray in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitNewArray in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
    • checkThrownExpression

      protected void checkThrownExpression(com.sun.source.tree.ThrowTree node)
      Case 4: Check for thrown exception nullness.
      Overrides:
      checkThrownExpression in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
      Parameters:
      node - ThrowTree to check
    • visitSynchronized

      public Void visitSynchronized(com.sun.source.tree.SynchronizedTree node, Void p)
      Case 5: Check for synchronizing locks.
      Specified by:
      visitSynchronized in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitSynchronized in class com.sun.source.util.TreeScanner<Void,Void>
    • visitAssert

      public Void visitAssert(com.sun.source.tree.AssertTree node, Void p)
      Specified by:
      visitAssert in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitAssert in class com.sun.source.util.TreeScanner<Void,Void>
    • visitIf

      public Void visitIf(com.sun.source.tree.IfTree node, Void p)
      Specified by:
      visitIf in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitIf in class com.sun.source.util.TreeScanner<Void,Void>
    • visitInstanceOf

      public Void visitInstanceOf(com.sun.source.tree.InstanceOfTree tree, Void p)
      Specified by:
      visitInstanceOf in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitInstanceOf in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
    • checkForRedundantTests

      protected void checkForRedundantTests(com.sun.source.tree.BinaryTree node)
      Reports an error if a comparison of a @NonNull expression with the null literal is performed.
    • visitBinary

      public Void visitBinary(com.sun.source.tree.BinaryTree node, Void p)
      Case 6: Check for redundant nullness tests Case 7: unboxing case: primitive operations.
      Specified by:
      visitBinary in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitBinary in class com.sun.source.util.TreeScanner<Void,Void>
    • visitUnary

      public Void visitUnary(com.sun.source.tree.UnaryTree node, Void p)
      Case 7: unboxing case: primitive operation.
      Specified by:
      visitUnary in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitUnary in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
    • visitCompoundAssignment

      public Void visitCompoundAssignment(com.sun.source.tree.CompoundAssignmentTree node, Void p)
      Case 7: unboxing case: primitive operation.
      Specified by:
      visitCompoundAssignment in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitCompoundAssignment in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
    • visitTypeCast

      public Void visitTypeCast(com.sun.source.tree.TypeCastTree node, Void p)
      Case 7: unboxing case: casting to a primitive.
      Specified by:
      visitTypeCast in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitTypeCast in class InitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue,NullnessStore>
    • visitMethod

      public Void visitMethod(com.sun.source.tree.MethodTree node, Void p)
      Description copied from class: BaseTypeVisitor
      Checks that the method obeys override and subtype rules to all overridden methods. (Uses the pseudo-assignment logic to do so.)

      The override rule specifies that a method, m1, may override a method m2 only if:

      • m1 return type is a subtype of m2
      • m1 receiver type is a supertype of m2
      • m1 parameters are supertypes of corresponding m2 parameters
      Also, it issues a "missing.this" error for static method annotated receivers.
      Specified by:
      visitMethod in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitMethod in class InitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue,NullnessStore>
    • visitMethodInvocation

      public Void visitMethodInvocation(com.sun.source.tree.MethodInvocationTree node, Void p)
      Description copied from class: BaseTypeVisitor
      Performs a method invocation check.

      An invocation of a method, m, on the receiver, r is valid only if:

      • passed arguments are subtypes of corresponding m parameters
      • r is a subtype of m receiver type
      • if m is generic, passed type arguments are subtypes of m type variables
      Specified by:
      visitMethodInvocation in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitMethodInvocation in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
    • processClassTree

      public void processClassTree(com.sun.source.tree.ClassTree classTree)
      Description copied from class: BaseTypeVisitor
      Type-check classTree. Subclasses should override this method instead of BaseTypeVisitor.visitClass(ClassTree, Void).
      Overrides:
      processClassTree in class InitializationVisitor<NullnessAnnotatedTypeFactory,NullnessValue,NullnessStore>
      Parameters:
      classTree - class to check
    • checkMethodInvocability

      protected void checkMethodInvocability(AnnotatedTypeMirror.AnnotatedExecutableType method, com.sun.source.tree.MethodInvocationTree node)
      Description copied from class: BaseTypeVisitor
      Tests whether the method can be invoked using the receiver of the 'node' method invocation, and issues a "method.invocation" if the invocation is invalid.

      This implementation tests whether the receiver in the method invocation is a subtype of the method receiver type. This behavior can be specialized by overriding skipReceiverSubtypeCheck.

      Overrides:
      checkMethodInvocability in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
      Parameters:
      method - the type of the invoked method
      node - the method invocation node
    • visitSwitch

      public Void visitSwitch(com.sun.source.tree.SwitchTree node, Void p)
      Specified by:
      visitSwitch in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitSwitch in class com.sun.source.util.TreeScanner<Void,Void>
    • visitForLoop

      public Void visitForLoop(com.sun.source.tree.ForLoopTree node, Void p)
      Specified by:
      visitForLoop in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitForLoop in class com.sun.source.util.TreeScanner<Void,Void>
    • visitNewClass

      public Void visitNewClass(com.sun.source.tree.NewClassTree node, Void p)
      Description copied from class: BaseTypeVisitor
      Performs a new class invocation check.

      An invocation of a constructor, c, is valid only if:

      • passed arguments are subtypes of corresponding c parameters
      • if c is generic, passed type arguments are subtypes of c type variables
      Specified by:
      visitNewClass in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitNewClass in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
    • visitWhileLoop

      public Void visitWhileLoop(com.sun.source.tree.WhileLoopTree node, Void p)
      Specified by:
      visitWhileLoop in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitWhileLoop in class com.sun.source.util.TreeScanner<Void,Void>
    • visitDoWhileLoop

      public Void visitDoWhileLoop(com.sun.source.tree.DoWhileLoopTree node, Void p)
      Specified by:
      visitDoWhileLoop in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitDoWhileLoop in class com.sun.source.util.TreeScanner<Void,Void>
    • visitConditionalExpression

      public Void visitConditionalExpression(com.sun.source.tree.ConditionalExpressionTree node, Void p)
      Description copied from class: BaseTypeVisitor
      If the computation of the type of the ConditionalExpressionTree in org.checkerframework.framework.type.TypeFromTree.TypeFromExpression.visitConditionalExpression(ConditionalExpressionTree, AnnotatedTypeFactory) is correct, the following checks are redundant. However, let's add another failsafe guard and do the checks.
      Specified by:
      visitConditionalExpression in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitConditionalExpression in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
    • checkExceptionParameter

      protected void checkExceptionParameter(com.sun.source.tree.CatchTree node)
      Description copied from class: BaseTypeVisitor
      Issue error if the exception parameter is not a supertype of the annotation specified by BaseTypeVisitor.getExceptionParameterLowerBoundAnnotations(), which is top by default.

      Subclasses may override this method to change the behavior of this check. Subclasses wishing to enforce that exception parameter be annotated with other annotations can just override BaseTypeVisitor.getExceptionParameterLowerBoundAnnotations().

      Overrides:
      checkExceptionParameter in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
      Parameters:
      node - CatchTree to check
    • visitAnnotation

      public Void visitAnnotation(com.sun.source.tree.AnnotationTree node, Void p)
      Description copied from class: BaseTypeVisitor
      Ensure that the annotation arguments comply to their declarations. This needs some special casing, as annotation arguments form special trees.
      Specified by:
      visitAnnotation in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitAnnotation in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
    • visitAnnotatedType

      public void visitAnnotatedType(@Nullable List<? extends com.sun.source.tree.AnnotationTree> annoTrees, com.sun.source.tree.Tree typeTree)
      Description copied from class: BaseTypeVisitor
      Checks an annotated type. Invoked by BaseTypeVisitor.visitAnnotatedType(AnnotatedTypeTree, Void), BaseTypeVisitor.visitVariable(com.sun.source.tree.VariableTree, java.lang.Void), and BaseTypeVisitor.visitMethod(com.sun.source.tree.MethodTree, java.lang.Void). Exists to prevent code duplication among the three. Checking in visitVariable and visitMethod is needed because there isn't an AnnotatedTypeTree within a variable declaration or for a method return type -- all the annotations are attached to the VariableTree or MethodTree, respectively.
      Overrides:
      visitAnnotatedType in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>
      Parameters:
      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.
      typeTree - the type that any type annotations in annoTrees apply to
    • createTypeValidator

      protected TypeValidator createTypeValidator()
      Overrides:
      createTypeValidator in class BaseTypeVisitor<NullnessAnnotatedTypeFactory>