@NotThreadSafe
abstract class TypeVisitor
extends java.lang.Object
Type
is, dispatch it to the corresponding visit*
method. By
default, no recursion is done for type arguments or type bounds. But subclasses can opt to do
recursion by calling visit(java.lang.reflect.Type...)
for any Type
while visitation is in progress. For
example, this can be used to reject wildcards or type variables contained in a type as in:
new TypeVisitor() {
protected void visitParameterizedType(ParameterizedType t) {
visit(t.getOwnerType());
visit(t.getActualTypeArguments());
}
protected void visitGenericArrayType(GenericArrayType t) {
visit(t.getGenericComponentType());
}
protected void visitTypeVariable(TypeVariable<?> t) {
throw new IllegalArgumentException("Cannot contain type variable.");
}
protected void visitWildcardType(WildcardType t) {
throw new IllegalArgumentException("Cannot contain wildcard type.");
}
}.visit(type);
One Type
is visited at most once. The second time the same type is visited, it's
ignored by visit(java.lang.reflect.Type...)
. This avoids infinite recursion caused by recursive type bounds.
This class is not thread safe.
Modifier and Type | Field and Description |
---|---|
private java.util.Set<java.lang.reflect.Type> |
visited |
Constructor and Description |
---|
TypeVisitor() |
Modifier and Type | Method and Description |
---|---|
void |
visit(java.lang.reflect.Type... types)
Visits the given types.
|
(package private) void |
visitClass(java.lang.Class<?> t) |
(package private) void |
visitGenericArrayType(java.lang.reflect.GenericArrayType t) |
(package private) void |
visitParameterizedType(java.lang.reflect.ParameterizedType t) |
(package private) void |
visitTypeVariable(java.lang.reflect.TypeVariable<?> t) |
(package private) void |
visitWildcardType(java.lang.reflect.WildcardType t) |
public final void visit(java.lang.reflect.Type... types)
visit(parameterizedType.getOwnerType())
safely without having to check nulls.void visitClass(java.lang.Class<?> t)
void visitGenericArrayType(java.lang.reflect.GenericArrayType t)
void visitParameterizedType(java.lang.reflect.ParameterizedType t)
void visitTypeVariable(java.lang.reflect.TypeVariable<?> t)
void visitWildcardType(java.lang.reflect.WildcardType t)