Class TypeVisitor

java.lang.Object
com.google.common.reflect.TypeVisitor
Direct Known Subclasses:
TypeResolver.TypeMappingIntrospector

abstract class TypeVisitor extends Object
Based on what a 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.

  • Field Details

    • visited

      private final Set<Type> visited
  • Constructor Details

    • TypeVisitor

      TypeVisitor()
  • Method Details

    • visit

      public final void visit(Type... types)
      Visits the given types. Null types are ignored. This allows subclasses to call visit(parameterizedType.getOwnerType()) safely without having to check nulls.
    • visitClass

      void visitClass(Class<?> t)
    • visitGenericArrayType

      void visitGenericArrayType(GenericArrayType t)
    • visitParameterizedType

      void visitParameterizedType(ParameterizedType t)
    • visitTypeVariable

      void visitTypeVariable(TypeVariable<?> t)
    • visitWildcardType

      void visitWildcardType(WildcardType t)