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.DefaultApplierElement.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(super.visitTypeVariable(type, p), 1);
     }
 }
 
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

  • 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