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
An
AnnotatedTypeScanner
visits an AnnotatedTypeMirror
and all of its child AnnotatedTypeMirror
s 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.
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);
}
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Reduces two results into a single result. -
Field Summary
Modifier and TypeFieldDescriptionprotected final R
The result to return if no other result is provided.protected final AnnotatedTypeScanner.Reduce<R>
The reduce function to use.protected final IdentityHashMap<AnnotatedTypeMirror,
R> -
Constructor Summary
ModifierConstructorDescriptionprotected
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
Modifier and TypeMethodDescriptionprotected R
Combinesr1
andr2
and returns the result.void
reset()
Reset the scanner to allow reuse of the same instance.protected R
scan
(@Nullable 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
(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
.final R
visit
(AnnotatedTypeMirror type) Callsreset()
and then scanstype
using null as the parameter.final R
visit
(AnnotatedTypeMirror type, P p) Visits an array type.Visits a declared type.Visits an executable type.Visits an intersection type.Visits NoType type.Visits anull
type.Visits a primitive type.Visits a type variable.Visits an union type.Visits a wildcard type.
-
Field Details
-
reduceFunction
The reduce function to use. -
defaultResult
The result to return if no other result is provided. It should be immutable. -
visitedNodes
-
-
Constructor Details
-
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
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
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 Details
-
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
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
- Specified by:
visit
in interfaceAnnotatedTypeVisitor<R,
P> - Parameters:
type
- the type to visitp
- a visitor-specified parameter- Returns:
- result of scanning
type
-
scan
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
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
-
scanAndReduce
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
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
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
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
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
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
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
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
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
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
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
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
-