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 for any Type while visitation is in progress. For example, this can be
| 56 | |
| 57 | |
| 58 | @NotThreadSafe |
| 59 | abstract class TypeVisitor { |
| 60 | private final Set<Type> visited = Sets.newHashSet(); |
| 61 | |
| 62 | /** |
| 63 | * Visits the given types. Null types are ignored. This allows subclasses to call |
| 64 | * {@code visit(parameterizedType.getOwnerType())} safely without having to check nulls. |
| 65 | */ |
| 66 | |
| 67 | |
| 68 | public final void visit(Type... types) { |
| 69 | for (Type type : types) { |
| 70 | if (type == null || !visited.add(type)) { |
| 71 | // null owner type, or already visited; |
| 72 | continue; |
| 73 | } |
| 74 | boolean succeeded = false; |
| 75 | try { |
| 76 | if (type instanceof TypeVariable) { |
| 77 | visitTypeVariable((TypeVariable<?>) type); |
| 78 | } else if (type instanceof WildcardType) { |
| 79 | visitWildcardType((WildcardType) type); |
| 80 | } else if (type instanceof ParameterizedType) { |
| 81 | visitParameterizedType((ParameterizedType) type); |
| 82 | } else if (type instanceof Class) { |
| 83 | visitClass((Class<?>) type); |
| 84 | } else if (type instanceof GenericArrayType) { |
| 85 | visitGenericArrayType((GenericArrayType) type); |
| 86 | } else { |
| 87 | throw new AssertionError("Unknown type: " + type); |
| 88 | } |
| 89 | succeeded = true; |
| 90 | } finally { |
| 91 | if (!succeeded) { // When the visitation failed, we don't want to ignore the second. |
| 92 | visited.remove(type); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void visitClass(Class<?> t) {} |
| 100 | |
| 101 | |
| 102 | void visitGenericArrayType(GenericArrayType t) {} |
| 103 | |
| 104 | |
| 105 | void visitParameterizedType(ParameterizedType t) {} |
| 106 | |
| 107 | |
| 108 | void visitTypeVariable(TypeVariable<?> t) {} |
| 109 | |
| 110 | |
| 111 | void visitWildcardType(WildcardType t) {} |
| 112 | } |
nothing calls this directly
no test coverage detected