View Javadoc
1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.common.reflect;
16  
17  import com.google.common.collect.Sets;
18  import java.lang.reflect.GenericArrayType;
19  import java.lang.reflect.ParameterizedType;
20  import java.lang.reflect.Type;
21  import java.lang.reflect.TypeVariable;
22  import java.lang.reflect.WildcardType;
23  import java.util.Set;
24  import javax.annotation.concurrent.NotThreadSafe;
25  
26  /**
27   * Based on what a {@link Type} is, dispatch it to the corresponding {@code visit*} method. By
28   * default, no recursion is done for type arguments or type bounds. But subclasses can opt to do
29   * recursion by calling {@link #visit} for any {@code Type} while visitation is in progress. For
30   * example, this can be used to reject wildcards or type variables contained in a type as in:
31   *
32   * <pre>   {@code
33   *   new TypeVisitor() {
34   *     protected void visitParameterizedType(ParameterizedType t) {
35   *       visit(t.getOwnerType());
36   *       visit(t.getActualTypeArguments());
37   *     }
38   *     protected void visitGenericArrayType(GenericArrayType t) {
39   *       visit(t.getGenericComponentType());
40   *     }
41   *     protected void visitTypeVariable(TypeVariable<?> t) {
42   *       throw new IllegalArgumentException("Cannot contain type variable.");
43   *     }
44   *     protected void visitWildcardType(WildcardType t) {
45   *       throw new IllegalArgumentException("Cannot contain wildcard type.");
46   *     }
47   *   }.visit(type);}</pre>
48   *
49   * <p>One {@code Type} is visited at most once. The second time the same type is visited, it's
50   * ignored by {@link #visit}. This avoids infinite recursion caused by recursive type bounds.
51   *
52   * <p>This class is <em>not</em> thread safe.
53   *
54   * @author Ben Yu
55   */
56  @NotThreadSafe
57  abstract class TypeVisitor {
58  
59    private final Set<Type> visited = Sets.newHashSet();
60  
61    /**
62     * Visits the given types. Null types are ignored. This allows subclasses to call
63     * {@code visit(parameterizedType.getOwnerType())} safely without having to check nulls.
64     */
65    public final void visit(Type... types) {
66      for (Type type : types) {
67        if (type == null || !visited.add(type)) {
68          // null owner type, or already visited;
69          continue;
70        }
71        boolean succeeded = false;
72        try {
73          if (type instanceof TypeVariable) {
74            visitTypeVariable((TypeVariable<?>) type);
75          } else if (type instanceof WildcardType) {
76            visitWildcardType((WildcardType) type);
77          } else if (type instanceof ParameterizedType) {
78            visitParameterizedType((ParameterizedType) type);
79          } else if (type instanceof Class) {
80            visitClass((Class<?>) type);
81          } else if (type instanceof GenericArrayType) {
82            visitGenericArrayType((GenericArrayType) type);
83          } else {
84            throw new AssertionError("Unknown type: " + type);
85          }
86          succeeded = true;
87        } finally {
88          if (!succeeded) { // When the visitation failed, we don't want to ignore the second.
89            visited.remove(type);
90          }
91        }
92      }
93    }
94  
95    void visitClass(Class<?> t) {}
96  
97    void visitGenericArrayType(GenericArrayType t) {}
98  
99    void visitParameterizedType(ParameterizedType t) {}
100 
101   void visitTypeVariable(TypeVariable<?> t) {}
102 
103   void visitWildcardType(WildcardType t) {}
104 }