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 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
defaultResultis 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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceReduces two results into a single result. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final RThe 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
ConstructorsModifierConstructorDescriptionprotectedConstructs an AnnotatedTypeScanner where the reduce function returns the first result if it is nonnull; otherwise the second result is returned.protectedAnnotatedTypeScanner(@Nullable AnnotatedTypeScanner.Reduce<R> reduceFunction) Constructs an AnnotatedTypeScanner with the given reduce function.protectedAnnotatedTypeScanner(@Nullable AnnotatedTypeScanner.Reduce<R> reduceFunction, R defaultResult) Constructs an AnnotatedTypeScanner with the given reduce function.protectedAnnotatedTypeScanner(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 RCombinesr1andr2and returns the result.voidreset()Reset the scanner to allow reuse of the same instance.protected Rscan(@Nullable Iterable<? extends AnnotatedTypeMirror> types, P p) Scan all the types and returns the reduced result.protected Rscan(AnnotatedTypeMirror type, P p) Scantypeby callingtype.accept(this, p); this method may be overridden by subclasses.protected RscanAndReduce(Iterable<? extends AnnotatedTypeMirror> types, P p, R r) protected RscanAndReduce(AnnotatedTypeMirror type, P p, R r) Scanstypewith the parameterpand reduces the result withr.final Rvisit(AnnotatedTypeMirror type) Callsreset()and then scanstypeusing null as the parameter.final Rvisit(AnnotatedTypeMirror type, P p) Visits an array type.Visits a declared type.Visits an executable type.Visits an intersection type.Visits NoType type.Visits anulltype.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. IfreduceFunctionis 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. IfreduceFunctionis 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 scanstypeusing null as the parameter.- Specified by:
visitin interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- type to scan- Returns:
- result of scanning
type
-
visit
- Specified by:
visitin interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- result of scanning
type
-
scan
Scantypeby 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
Scanstypewith the parameterpand reduces the result withr.- Parameters:
type- type to scanp- parameter to use for when scanningtyper- result to combine with the result of scanningtype- Returns:
- the combination of
rwith the result of scanningtype
-
reduce
Combinesr1andr2and returns the result. The default implementation returnsr1if it is not null; otherwise, it returnsr2.- Parameters:
r1- a result of scan, nonnull ifdefaultResultis nonnull and this method never returns nullr2- a result of scan, nonnull ifdefaultResultis nonnull and this method never returns null- Returns:
- the combination of
r1andr2
-
visitDeclared
Description copied from interface:AnnotatedTypeVisitorVisits a declared type.- Specified by:
visitDeclaredin interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitIntersection
Description copied from interface:AnnotatedTypeVisitorVisits an intersection type.- Specified by:
visitIntersectionin interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitUnion
Description copied from interface:AnnotatedTypeVisitorVisits an union type.- Specified by:
visitUnionin interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitArray
Description copied from interface:AnnotatedTypeVisitorVisits an array type.- Specified by:
visitArrayin interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitExecutable
Description copied from interface:AnnotatedTypeVisitorVisits an executable type.- Specified by:
visitExecutablein interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitTypeVariable
Description copied from interface:AnnotatedTypeVisitorVisits a type variable.- Specified by:
visitTypeVariablein interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitNoType
Description copied from interface:AnnotatedTypeVisitorVisits NoType type.- Specified by:
visitNoTypein interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitNull
Description copied from interface:AnnotatedTypeVisitorVisits anulltype.- Specified by:
visitNullin interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitPrimitive
Description copied from interface:AnnotatedTypeVisitorVisits a primitive type.- Specified by:
visitPrimitivein interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- a visitor-specified result
-
visitWildcard
Description copied from interface:AnnotatedTypeVisitorVisits a wildcard type.- Specified by:
visitWildcardin interfaceAnnotatedTypeVisitor<R,P> - Parameters:
type- the type to visitp- a visitor-specified parameter- Returns:
- a visitor-specified result
-