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 java.lang.Object implements AnnotatedTypeVisitor<R,P>
AnAnnotatedTypeScanner
visits anAnnotatedTypeMirror
and all of its childAnnotatedTypeMirror
and preforms some function depending on the kind of type. (By contrast, aSimpleAnnotatedTypeScanner
scans anAnnotatedTypeMirror
and performs the same function regardless of the kind of type.) The function returns some value with typeR
and takes an argument of typeP
. If the function does not return any value, thenR
should beVoid
. If the function takes no additional argument, thenP
should beVoid
.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.
reduce(R, R)
method combines the results of visiting child types. It can be specified by passing aAnnotatedTypeScanner.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 passnull
as the last parameter) callvisit(AnnotatedTypeMirror)
. Both methods callreset()
.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); } }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 newAnnotatedTypeScanner
for each use. Instead, store anAnnotatedTypeScanner
as a field in theAnnotatedTypeFactory
orBaseTypeVisitor
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); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
AnnotatedTypeScanner.Reduce<R>
Reduces two results into a single result.
-
Field Summary
Fields Modifier and Type Field Description protected R
defaultResult
The result to return if no other result is provided.protected AnnotatedTypeScanner.Reduce<R>
reduceFunction
The reduce function to use.protected java.util.IdentityHashMap<AnnotatedTypeMirror,R>
visitedNodes
-
Constructor Summary
Constructors Modifier Constructor Description protected
AnnotatedTypeScanner()
Constructs an AnnotatedTypeScanner where the reduce function returns the first result if it is nonnull; otherwise the second result is returned.protected
AnnotatedTypeScanner(@Nullable AnnotatedTypeScanner.Reduce<R> reduceFunction)
Constructs an AnnotatedTypeScanner with the given reduce function.protected
AnnotatedTypeScanner(@Nullable AnnotatedTypeScanner.Reduce<R> reduceFunction, R defaultResult)
Constructs an AnnotatedTypeScanner with the given reduce function.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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected R
reduce(R r1, R r2)
Combinesr1
andr2
and returns the result.void
reset()
Reset the scanner to allow reuse of the same instance.protected R
scan(@Nullable java.lang.Iterable<? extends AnnotatedTypeMirror> types, P p)
Scan all the types and returns the reduced result.protected R
scan(AnnotatedTypeMirror type, P p)
Scantype
by callingtype.accept(this, p)
; this method may be overridden by subclasses.protected R
scanAndReduce(java.lang.Iterable<? extends AnnotatedTypeMirror> types, P p, R r)
protected R
scanAndReduce(AnnotatedTypeMirror type, P p, R r)
Scanstype
with the parameterp
and reduces the result withr
.R
visit(AnnotatedTypeMirror type)
Callsreset()
and then scanstype
using null as the parameter.R
visit(AnnotatedTypeMirror type, P p)
R
visitArray(AnnotatedTypeMirror.AnnotatedArrayType type, P p)
Visits an array type.R
visitDeclared(AnnotatedTypeMirror.AnnotatedDeclaredType type, P p)
Visits a declared type.R
visitExecutable(AnnotatedTypeMirror.AnnotatedExecutableType type, P p)
Visits an executable type.R
visitIntersection(AnnotatedTypeMirror.AnnotatedIntersectionType type, P p)
Visits an intersection type.R
visitNoType(AnnotatedTypeMirror.AnnotatedNoType type, P p)
Visits NoType type.R
visitNull(AnnotatedTypeMirror.AnnotatedNullType type, P p)
Visits anull
type.R
visitPrimitive(AnnotatedTypeMirror.AnnotatedPrimitiveType type, P p)
Visits a primitive type.R
visitTypeVariable(AnnotatedTypeMirror.AnnotatedTypeVariable type, P p)
Visits a type variable.R
visitUnion(AnnotatedTypeMirror.AnnotatedUnionType type, P p)
Visits an union type.R
visitWildcard(AnnotatedTypeMirror.AnnotatedWildcardType type, P p)
Visits a wildcard type.
-
-
-
Field Detail
-
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.
-
visitedNodes
protected final java.util.IdentityHashMap<AnnotatedTypeMirror,R> visitedNodes
-
-
Constructor Detail
-
AnnotatedTypeScanner
protected AnnotatedTypeScanner(@Nullable AnnotatedTypeScanner.Reduce<R> reduceFunction, R defaultResult)
Constructs an AnnotatedTypeScanner with the given reduce function. IfreduceFunction
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 resultsdefaultResult
- 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. IfreduceFunction
is null, then the reduce function returns the first result if it is nonnull; otherwise the second result is returned. The default result isnull
- 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 isnull
.
-
-
Method Detail
-
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)
Callsreset()
and then scanstype
using null as the parameter.- Specified by:
visit
in interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- type to scan- Returns:
- result of scanning
type
-
visit
public final R visit(AnnotatedTypeMirror type, P p)
- Specified by:
visit
in interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- a visitor-specified parameter- Returns:
- result of scanning
type
-
scan
protected R scan(AnnotatedTypeMirror type, P p)
Scantype
by callingtype.accept(this, p)
; this method may be overridden by subclasses.- Parameters:
type
- type to scanp
- the parameter to use- Returns:
- the result of visiting
type
-
scan
protected R scan(@Nullable java.lang.Iterable<? extends AnnotatedTypeMirror> types, P p)
Scan all the types and returns the reduced result.- Parameters:
types
- types to scanp
- the parameter to use- Returns:
- the reduced result of scanning all the types
-
scanAndReduce
protected R scanAndReduce(java.lang.Iterable<? extends AnnotatedTypeMirror> types, P p, R r)
-
scanAndReduce
protected R scanAndReduce(AnnotatedTypeMirror type, P p, R r)
Scanstype
with the parameterp
and reduces the result withr
.- Parameters:
type
- type to scanp
- parameter to use for when scanningtype
r
- result to combine with the result of scanningtype
- Returns:
- the combination of
r
with the result of scanningtype
-
reduce
protected R reduce(R r1, R r2)
Combinesr1
andr2
and returns the result. The default implementation returnsr1
if it is not null; otherwise, it returnsr2
.- Parameters:
r1
- a result of scan, nonnull ifdefaultResult
is nonnull and this method never returns nullr2
- a result of scan, nonnull ifdefaultResult
is nonnull and this method never returns null- Returns:
- the combination of
r1
andr2
-
visitDeclared
public R visitDeclared(AnnotatedTypeMirror.AnnotatedDeclaredType type, P p)
Description copied from interface:AnnotatedTypeVisitor
Visits a declared type.- Specified by:
visitDeclared
in interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- 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 interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- 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 interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- 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 interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- 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 interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- 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 interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- 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 interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitNull
public R visitNull(AnnotatedTypeMirror.AnnotatedNullType type, P p)
Description copied from interface:AnnotatedTypeVisitor
Visits anull
type.- Specified by:
visitNull
in interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- 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 interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- 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 interfaceAnnotatedTypeVisitor<R,P>
- Parameters:
type
- the type to visitp
- a visitor-specified parameter- Returns:
- a visitor-specified result
-
-