Class ValueVisitor

java.lang.Object
com.sun.source.util.TreeScanner<R,P>
com.sun.source.util.TreePathScanner<R,P>
All Implemented Interfaces:
com.sun.source.tree.TreeVisitor<Void,Void>

public class ValueVisitor extends BaseTypeVisitor<ValueAnnotatedTypeFactory>
Visitor for the Constant Value type system.
  • Constructor Details

  • Method Details

    • commonAssignmentCheck

      protected void commonAssignmentCheck(AnnotatedTypeMirror varType, com.sun.source.tree.ExpressionTree valueExp, @CompilerMessageKey String errorKey, Object... extraArgs)
      ValueVisitor overrides this method so that it does not have to check variables annotated with the IntRangeFromPositive annotation, the IntRangeFromNonNegative annotation, or the IntRangeFromGTENegativeOne annotation. This annotation is only introduced by the Index Checker's lower bound annotations. It is safe to defer checking of these values to the Index Checker because this is only introduced for explicitly-written org.checkerframework.checker.index.qual.Positive, explicitly-written org.checkerframework.checker.index.qual.NonNegative, and explicitly-written org.checkerframework.checker.index.qual.GTENegativeOne annotations, which must be checked by the Lower Bound Checker.
      Overrides:
      commonAssignmentCheck in class BaseTypeVisitor<ValueAnnotatedTypeFactory>
      Parameters:
      varType - the annotated type of 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<ValueAnnotatedTypeFactory>
      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
    • checkOverride

      protected boolean checkOverride(com.sun.source.tree.MethodTree overriderTree, AnnotatedTypeMirror.AnnotatedExecutableType overrider, AnnotatedTypeMirror.AnnotatedDeclaredType overridingType, AnnotatedTypeMirror.AnnotatedExecutableType overridden, AnnotatedTypeMirror.AnnotatedDeclaredType overriddenType)
      Return types for methods that are annotated with @IntRangeFromX annotations need to be replaced with @UnknownVal. See the documentation on commonAssignmentCheck.

      A separate override is necessary because checkOverride doesn't actually use the commonAssignmentCheck.

      Overrides:
      checkOverride in class BaseTypeVisitor<ValueAnnotatedTypeFactory>
      Parameters:
      overriderTree - declaration tree of overriding method
      overrider - type of the overriding method
      overridingType - type of overriding class
      overridden - type of overridden method
      overriddenType - type of overridden class
      Returns:
      true if the override is allowed
      See Also:
    • createTypeFactory

      protected ValueAnnotatedTypeFactory 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<ValueAnnotatedTypeFactory>
      Returns:
      the appropriate type factory
    • visitAnnotation

      public Void visitAnnotation(com.sun.source.tree.AnnotationTree node, Void p)
      Warns about malformed constant-value annotations.

      Issues an error if any @IntRange annotation has its 'from' value greater than 'to' value.

      Issues an error if any constant-value annotation has no arguments.

      Issues a warning if any constant-value annotation has > MAX_VALUES arguments.

      Issues a warning if any @ArrayLen/@ArrayLenRange annotations contain a negative array length.

      Issues a warning if any @MatchesRegex or @DoesNotMatchRegex annotation contains an invalid regular expression.

      Specified by:
      visitAnnotation in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitAnnotation in class BaseTypeVisitor<ValueAnnotatedTypeFactory>
    • visitTypeCast

      public Void visitTypeCast(com.sun.source.tree.TypeCastTree node, Void p)
      Specified by:
      visitTypeCast in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitTypeCast in class BaseTypeVisitor<ValueAnnotatedTypeFactory>
    • validateType

      public boolean validateType(com.sun.source.tree.Tree tree, AnnotatedTypeMirror type)
      Overridden to issue errors at the appropriate place if an IntRange or ArrayLenRange annotation has from > to. from > to either indicates a user error when writing an annotation or an error in the checker's implementation, as from should always be <= to. Note that additional checks are performed in visitAnnotation(AnnotationTree, Void).
      Overrides:
      validateType in class BaseTypeVisitor<ValueAnnotatedTypeFactory>
      Parameters:
      tree - the type tree supplied by the user
      type - the type corresponding to tree
      Returns:
      true if the type is valid
      See Also:
    • 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 BaseTypeVisitor<ValueAnnotatedTypeFactory>