Class AnnotatedTypeScanner<R,P>

java.lang.Object
org.checkerframework.framework.type.visitor.AnnotatedTypeScanner<R,P>
Type Parameters:
R - the return type of this visitor's methods. Use Void for visitors that do not need to return results.
P - the type of the additional parameter to this visitor's methods. Use Void for visitors that do not need an additional parameter.
All Implemented Interfaces:
AnnotatedTypeVisitor<R,P>
Direct Known Subclasses:
BaseTypeValidator, DoubleAnnotatedTypeScanner, QualifierDefaults.DefaultApplierElementImpl, SimpleAnnotatedTypeScanner, TypeAnnotator

public abstract class AnnotatedTypeScanner<R,P> extends Object implements AnnotatedTypeVisitor<R,P>
An AnnotatedTypeScanner visits an AnnotatedTypeMirror and all of its child AnnotatedTypeMirrors and performs some function depending on the kind of type. (By contrast, a SimpleAnnotatedTypeScanner scans an AnnotatedTypeMirror and performs the same function regardless of the kind of type.) The function returns some value with type R and takes an argument of type P. If the function does not return any value, then R should be Void. If the function takes no additional argument, then P should be Void.

The default implementation of the visitAnnotatedTypeMirror methods will determine a result as follows:

  • If the type being visited has no children, the defaultResult is returned.
  • If the type being visited has one child, the result of visiting the child type is returned.
  • If the type being visited has more than one child, the result is determined by visiting each child in turn, and then combining the result of each with the cumulative result so far, as determined by the reduce(R, R) method.
The reduce(R, R) method combines the results of visiting child types. It can be specified by passing a AnnotatedTypeScanner.Reduce object to one of the constructors or by overriding the method directly. If it is not otherwise specified, then reduce returns the first result if it is not null; otherwise, the second result is returned. If the default result is nonnull and reduce never returns null, then both parameters passed to reduce will be nonnull.

When overriding a visitAnnotatedTypeMirror method, the returned expression should be reduce(super.visitAnnotatedTypeMirror(type, parameter), result) so that the whole type is scanned.

To begin scanning a type call visit(AnnotatedTypeMirror, Object) or (to pass null as the last parameter) call visit(AnnotatedTypeMirror). Both methods call reset().

Here is an example of a scanner that counts the number of AnnotatedTypeMirror.AnnotatedTypeVariable in an AnnotatedTypeMirror.

 class CountTypeVariable extends AnnotatedTypeScanner<Integer, Void> {
    public CountTypeVariable() {
        super(Integer::sum, 0);
    }

    @Override
     public Integer visitTypeVariable(AnnotatedTypeVariable type, Void p) {
         return reduce(1, super.visitTypeVariable(type, p));
     }
 }
 
An AnnotatedTypeScanner keeps a map of visited types, in order to prevent infinite recursion on recursive types. Because of this map, you should not create a new AnnotatedTypeScanner for each use. Instead, store an AnnotatedTypeScanner as a field in the AnnotatedTypeFactory or BaseTypeVisitor of the checker.

Below is an example of how to use CountTypeVariable.


 private final CountTypeVariable countTypeVariable = new CountTypeVariable();

 void method(AnnotatedTypeMirror type) {
     int count = countTypeVariable.visit(type);
 }
 
  • Field Details

    • reduceFunction

      protected final AnnotatedTypeScanner.Reduce<R> reduceFunction
      The reduce function to use.
    • defaultResult

      protected final R defaultResult
      The result to return if no other result is provided. It should be immutable.
    • VISITED_NODES_INITIAL_CAPACITY

      public static final int VISITED_NODES_INITIAL_CAPACITY
      The IdentityHashMap expectedMaxSize (expected entry count, not a table size) for the visitedNodes map and the per-visit map in AnnotatedTypeCopier. A value of 8 makes the first markVisited(org.checkerframework.framework.type.AnnotatedTypeMirror, R) call allocate a 32-slot Object[] that holds 10 entries before its first resize. Most scans visit only 1 to 3 nodes, so the map rarely grows; these per-scan arrays are the framework's largest transient Object[] allocation source, so keeping them small reduces GC pressure.
      See Also:
  • Constructor Details

    • AnnotatedTypeScanner

      protected AnnotatedTypeScanner(@Nullable AnnotatedTypeScanner.Reduce<R> reduceFunction, R defaultResult)
      Constructs an AnnotatedTypeScanner with the given reduce function. If reduceFunction is null, then the reduce function returns the first result if it is nonnull; otherwise the second result is returned.
      Parameters:
      reduceFunction - function used to combine two results
      defaultResult - the result to return if a visit type method is not overridden; it should be immutable
    • AnnotatedTypeScanner

      protected AnnotatedTypeScanner(@Nullable AnnotatedTypeScanner.Reduce<R> reduceFunction)
      Constructs an AnnotatedTypeScanner with the given reduce function. If reduceFunction is null, then the reduce function returns the first result if it is nonnull; otherwise the second result is returned. The default result is null
      Parameters:
      reduceFunction - function used to combine two results
    • AnnotatedTypeScanner

      protected AnnotatedTypeScanner(R defaultResult)
      Constructs an AnnotatedTypeScanner where the reduce function returns the first result if it is nonnull; otherwise the second result is returned.
      Parameters:
      defaultResult - the result to return if a visit type method is not overridden; it should be immutable
    • AnnotatedTypeScanner

      protected AnnotatedTypeScanner()
      Constructs an AnnotatedTypeScanner where the reduce function returns the first result if it is nonnull; otherwise the second result is returned. The default result is null.
  • Method Details

    • hasVisited

      protected final boolean hasVisited(AnnotatedTypeMirror type)
      Returns true if type has already been visited (possibly with a null result).
      Parameters:
      type - the type to look up
      Returns:
      true if type has already been visited
    • getVisited

      protected final R getVisited(AnnotatedTypeMirror type)
      Returns the stored result for type, or null. A null return is ambiguous: it means either "not visited" or "visited with a null result". When that distinction matters (a scanner that stores null as an in-progress sentinel), guard with hasVisited(org.checkerframework.framework.type.AnnotatedTypeMirror) first.
      Parameters:
      type - the type to look up
      Returns:
      the result stored for type, or null if absent or stored as null
    • markVisited

      protected final void markVisited(AnnotatedTypeMirror type, R result)
      Records type as visited with the given result, allocating the map on first use.
      Parameters:
      type - the type to record as visited
      result - the result to store for type
    • reset

      public void reset()
      Reset the scanner to allow reuse of the same instance. Subclasses should override this method to clear their additional state; they must call the super implementation.
    • visit

      public final R visit(AnnotatedTypeMirror type)
      Calls reset() and then scans type using null as the parameter.
      Specified by:
      visit in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - type to scan
      Returns:
      result of scanning type
    • visit

      public final R visit(AnnotatedTypeMirror type, P p)
      Calls reset() and then scans type using p as the parameter.
      Specified by:
      visit in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      result of scanning type
    • scan

      protected R scan(AnnotatedTypeMirror type, P p)
      Scan type by calling type.accept(this, p); this method may be overridden by subclasses.
      Parameters:
      type - type to scan
      p - the parameter to use
      Returns:
      the result of visiting type
    • scan

      protected R scan(@Nullable List<? extends AnnotatedTypeMirror> types, P p)
      Scans all the types in the list and returns the reduced result. Uses index-based access to avoid allocating an iterator for the (typically unmodifiable) list.
      Parameters:
      types - types to scan; may be null
      p - the parameter to use
      Returns:
      the reduced result of scanning all the types
    • scanAndReduce

      protected R scanAndReduce(@Nullable List<? extends AnnotatedTypeMirror> types, P p, R r)
      Scans all types in types with parameter p and reduces the result with r. Uses index-based access to avoid allocating an iterator.
      Parameters:
      types - types to scan; may be null
      p - parameter to use when scanning
      r - result to combine with the result of scanning types
      Returns:
      the combination of r with the result of scanning all types
    • scanAndReduce

      protected R scanAndReduce(AnnotatedTypeMirror type, P p, R r)
      Scans type with the parameter p and reduces the result with r.
      Parameters:
      type - type to scan
      p - parameter to use for when scanning type
      r - result to combine with the result of scanning type
      Returns:
      the combination of r with the result of scanning type
    • reduce

      protected R reduce(R add, R acc)
      Combines add and acc and returns the result. The default implementation returns add if it is not null; otherwise, it returns acc.
      Parameters:
      add - a result of scan, nonnull if defaultResult is nonnull and this method never returns null
      acc - a result of scan, nonnull if defaultResult is nonnull and this method never returns null
      Returns:
      the combination of add and acc
    • visitDeclared

      public R visitDeclared(AnnotatedTypeMirror.AnnotatedDeclaredType type, P p)
      Description copied from interface: AnnotatedTypeVisitor
      Visits a declared type.
      Specified by:
      visitDeclared in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      a visitor-specified result
    • visitIntersection

      public R visitIntersection(AnnotatedTypeMirror.AnnotatedIntersectionType type, P p)
      Description copied from interface: AnnotatedTypeVisitor
      Visits an intersection type.
      Specified by:
      visitIntersection in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      a visitor-specified result
    • visitUnion

      public R visitUnion(AnnotatedTypeMirror.AnnotatedUnionType type, P p)
      Description copied from interface: AnnotatedTypeVisitor
      Visits an union type.
      Specified by:
      visitUnion in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      a visitor-specified result
    • visitArray

      public R visitArray(AnnotatedTypeMirror.AnnotatedArrayType type, P p)
      Description copied from interface: AnnotatedTypeVisitor
      Visits an array type.
      Specified by:
      visitArray in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      a visitor-specified result
    • visitExecutable

      public R visitExecutable(AnnotatedTypeMirror.AnnotatedExecutableType type, P p)
      Description copied from interface: AnnotatedTypeVisitor
      Visits an executable type.
      Specified by:
      visitExecutable in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      a visitor-specified result
    • visitTypeVariable

      public R visitTypeVariable(AnnotatedTypeMirror.AnnotatedTypeVariable type, P p)
      Description copied from interface: AnnotatedTypeVisitor
      Visits a type variable.
      Specified by:
      visitTypeVariable in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      a visitor-specified result
    • visitNoType

      public R visitNoType(AnnotatedTypeMirror.AnnotatedNoType type, P p)
      Description copied from interface: AnnotatedTypeVisitor
      Visits NoType type.
      Specified by:
      visitNoType in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      a visitor-specified result
    • visitNull

      public R visitNull(AnnotatedTypeMirror.AnnotatedNullType type, P p)
      Description copied from interface: AnnotatedTypeVisitor
      Visits a null type.
      Specified by:
      visitNull in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      a visitor-specified result
    • visitPrimitive

      public R visitPrimitive(AnnotatedTypeMirror.AnnotatedPrimitiveType type, P p)
      Description copied from interface: AnnotatedTypeVisitor
      Visits a primitive type.
      Specified by:
      visitPrimitive in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      a visitor-specified result
    • visitWildcard

      public R visitWildcard(AnnotatedTypeMirror.AnnotatedWildcardType type, P p)
      Description copied from interface: AnnotatedTypeVisitor
      Visits a wildcard type.
      Specified by:
      visitWildcard in interface AnnotatedTypeVisitor<R,P>
      Parameters:
      type - the type to visit
      p - a visitor-specified parameter
      Returns:
      a visitor-specified result