Class ValueVisitor

All Implemented Interfaces:
TreeVisitor<Void,Void>

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

  • Method Details

    • shouldCheckVarargs

      protected boolean shouldCheckVarargs(Tree tree)
      Description copied from class: BaseTypeVisitor
      Returns true if the varargs array created for the given varargs invocation should be checked against the formal varargs parameter type.

      The default implementation returns false if zero varargs actual arguments were passed at the call site, because no user arguments were passed to populate the varargs array. Checkers that enforce container array annotations on implicit empty arrays (such as the Value Checker enforcing @MinLen) may override this method to return true even when zero varargs actual arguments were passed.

      Overrides:
      shouldCheckVarargs in class BaseTypeVisitor<ValueAnnotatedTypeFactory>
      Parameters:
      tree - the invocation tree
      Returns:
      true if the varargs array should be checked
    • commonAssignmentCheck

      protected boolean commonAssignmentCheck(AnnotatedTypeMirror varType, 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
      Returns:
      true if the check succeeds, false if an error message was issued
    • commonAssignmentCheck

      @FormatMethod protected boolean commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, 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
      Returns:
      true if the check succeeds, false if an error message was issued
    • checkOverride

      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(AnnotationTree tree, 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 TreeVisitor<Void,Void>
      Overrides:
      visitAnnotation in class BaseTypeVisitor<ValueAnnotatedTypeFactory>
    • visitTypeCast

      public Void visitTypeCast(TypeCastTree tree, Void p)
      Specified by:
      visitTypeCast in interface TreeVisitor<Void,Void>
      Overrides:
      visitTypeCast in class BaseTypeVisitor<ValueAnnotatedTypeFactory>
    • isTypeCastSafe

      protected boolean isTypeCastSafe(AnnotatedTypeMirror castType, AnnotatedTypeMirror exprType)
      Description copied from class: BaseTypeVisitor
      Returns true if the cast is safe.

      Only primary qualifiers are checked unless the command line option "checkCastElementType" is supplied.

      Overrides:
      isTypeCastSafe in class BaseTypeVisitor<ValueAnnotatedTypeFactory>
      Parameters:
      castType - annotated type of the cast
      exprType - annotated type of the casted expression
      Returns:
      true if the type cast is safe, false otherwise
    • validateType

      public boolean validateType(Tree tree, AnnotatedTypeMirror type)
      Issue an error if IntRange(from=x, to=y) has x>y. Note that additional checks are performed in visitAnnotation(AnnotationTree, Void).

      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<ValueAnnotatedTypeFactory>
      Parameters:
      tree - the type tree supplied by the user
      type - the type corresponding to tree
      Returns:
      true if the type is valid
    • processMethodTree

      public void processMethodTree(String className, MethodTree tree)
      Description copied from class: BaseTypeVisitor
      Type-check methodTree. Subclasses should override this method instead of BaseTypeVisitor.visitMethod(MethodTree, Void).
      Overrides:
      processMethodTree in class BaseTypeVisitor<ValueAnnotatedTypeFactory>
      Parameters:
      className - the class that contains the method, for diagnostics only
      tree - the method to type-check