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.public abstract class AnnotatedTypeScanner<R,P> extends Object implements AnnotatedTypeVisitor<R,P>
AnnotatedTypeScanner
visits an AnnotatedTypeMirror
and all of its child AnnotatedTypeMirror
and preforms some function depending on the kind of type. A SimpleAnnotatedTypeScanner
scans an AnnotatedTypeMirror
and preforms 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 not arguments, then P
should be Void
.
The default implementation of the visitAnnotatedTypeMirror methods will determine a result as follows:
defaultResult
is returned.
reduce(R, R)
method.
reduce(R, R)
method combines the results of visiting child types. It can be specified by
passing an AnnotatedTypeScanner.Reduce
object to one of the constructors or by overriding the method directly.
If it is not otherwise specified, the 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);
}
}
Below is an example of how to use CountTypeVariable
void method(AnnotatedTypeMirror type) {
int count = new CountTypeVariable().visit(type);
}
Modifier and Type | Class and Description |
---|---|
static interface |
AnnotatedTypeScanner.Reduce<R>
Reduces two results into a single result.
|
Modifier and Type | Field and 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 Map<AnnotatedTypeMirror,R> |
visitedNodes |
Modifier | Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
protected R |
reduce(R r1,
R r2)
Combines
r1 and r2 and returns the result. |
void |
reset()
Reset the scanner to allow reuse of the same instance.
|
protected R |
scan(AnnotatedTypeMirror type,
P p)
Scan
type by calling type.accept(this, p) ; this method may be overridden by
subclasses. |
protected R |
scan(@Nullable Iterable<? extends AnnotatedTypeMirror> types,
P p)
Scan all the types and returns the reduced result.
|
protected R |
scanAndReduce(AnnotatedTypeMirror type,
P p,
R r)
Scans
type with the parameter p and reduces the result with r . |
protected R |
scanAndReduce(Iterable<? extends AnnotatedTypeMirror> types,
P p,
R r) |
R |
visit(AnnotatedTypeMirror type)
Calls
reset() and then scans type 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 a
null 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.
|
protected final AnnotatedTypeScanner.Reduce<R> reduceFunction
protected final R defaultResult
protected final Map<AnnotatedTypeMirror,R> visitedNodes
protected AnnotatedTypeScanner(@Nullable AnnotatedTypeScanner.Reduce<R> reduceFunction, R defaultResult)
reduceFunction
is null, then the reduce function returns the first result if it is nonnull; otherwise the
second result is returned.reduceFunction
- function used to combine two resultsdefaultResult
- the result to return if a visit type method is not overridden; it should
be immutableprotected AnnotatedTypeScanner(@Nullable AnnotatedTypeScanner.Reduce<R> reduceFunction)
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
reduceFunction
- function used to combine two resultsprotected AnnotatedTypeScanner(R defaultResult)
defaultResult
- the result to return if a visit type method is not overridden; it should
be immutableprotected AnnotatedTypeScanner()
null
.public void reset()
public final R visit(AnnotatedTypeMirror type)
reset()
and then scans type
using null as the parameter.visit
in interface AnnotatedTypeVisitor<R,P>
type
- type to scantype
public final R visit(AnnotatedTypeMirror type, P p)
visit
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parametertype
protected R scan(AnnotatedTypeMirror type, P p)
type
by calling type.accept(this, p)
; this method may be overridden by
subclasses.type
- type to scanp
- the parameter to usetype
protected R scan(@Nullable Iterable<? extends AnnotatedTypeMirror> types, P p)
types
- types to scanp
- the parameter to useprotected R scanAndReduce(Iterable<? extends AnnotatedTypeMirror> types, P p, R r)
protected R scanAndReduce(AnnotatedTypeMirror type, P p, R r)
type
with the parameter p
and reduces the result with r
.type
- type to scanp
- parameter to use for when scanning type
r
- result to combine with the result of scanning type
r
with the result of scanning type
protected R reduce(R r1, R r2)
r1
and r2
and returns the result. The default implementation returns
r1
if it is not null; otherwise, it returns r2
.r1
- a result of scan, nonnull if defaultResult
is nonnull and this method
never returns nullr2
- a result of scan, nonnull if defaultResult
is nonnull and this method
never returns nullr1
and r2
public R visitDeclared(AnnotatedTypeMirror.AnnotatedDeclaredType type, P p)
AnnotatedTypeVisitor
visitDeclared
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parameterpublic R visitIntersection(AnnotatedTypeMirror.AnnotatedIntersectionType type, P p)
AnnotatedTypeVisitor
visitIntersection
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parameterpublic R visitUnion(AnnotatedTypeMirror.AnnotatedUnionType type, P p)
AnnotatedTypeVisitor
visitUnion
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parameterpublic R visitArray(AnnotatedTypeMirror.AnnotatedArrayType type, P p)
AnnotatedTypeVisitor
visitArray
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parameterpublic R visitExecutable(AnnotatedTypeMirror.AnnotatedExecutableType type, P p)
AnnotatedTypeVisitor
visitExecutable
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parameterpublic R visitTypeVariable(AnnotatedTypeMirror.AnnotatedTypeVariable type, P p)
AnnotatedTypeVisitor
visitTypeVariable
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parameterpublic R visitNoType(AnnotatedTypeMirror.AnnotatedNoType type, P p)
AnnotatedTypeVisitor
visitNoType
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parameterpublic R visitNull(AnnotatedTypeMirror.AnnotatedNullType type, P p)
AnnotatedTypeVisitor
null
type.visitNull
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parameterpublic R visitPrimitive(AnnotatedTypeMirror.AnnotatedPrimitiveType type, P p)
AnnotatedTypeVisitor
visitPrimitive
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parameterpublic R visitWildcard(AnnotatedTypeMirror.AnnotatedWildcardType type, P p)
AnnotatedTypeVisitor
visitWildcard
in interface AnnotatedTypeVisitor<R,P>
type
- the type to visitp
- a visitor-specified parameter