1 /* 2 * Copyright (C) 2013 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.reflect; 18 19 import junit.framework.TestCase; 20 21 import java.lang.reflect.GenericArrayType; 22 import java.lang.reflect.ParameterizedType; 23 import java.lang.reflect.Type; 24 import java.lang.reflect.TypeVariable; 25 import java.lang.reflect.WildcardType; 26 import java.util.ArrayList; 27 import java.util.EnumSet; 28 29 /** 30 * Tests of {@link TypeVisitor}. 31 * 32 * @author Ben Yu 33 */ 34 public class TypeVisitorTest extends TestCase { 35 36 public void testVisitNull() { 37 new BaseTypeVisitor().visit( 38 ((ParameterizedType) ArrayList.class.getGenericSuperclass()).getOwnerType()); 39 } 40 41 public void testVisitClass() { 42 assertVisited(String.class); 43 new BaseTypeVisitor() { 44 @Override void visitClass(Class<?> t) {} 45 }.visit(String.class); 46 } 47 48 public <T> void testVisitTypeVariable() { 49 Type type = new TypeCapture<T>() {}.capture(); 50 assertVisited(type); 51 new BaseTypeVisitor() { 52 @Override void visitTypeVariable(TypeVariable<?> t) {} 53 }.visit(type); 54 } 55 56 public void testVisitWildcardType() { 57 WildcardType type = Types.subtypeOf(String.class); 58 assertVisited(type); 59 new BaseTypeVisitor() { 60 @Override void visitWildcardType(WildcardType t) {} 61 }.visit(type); 62 } 63 64 public <T> void testVisitGenericArrayType() { 65 Type type = new TypeCapture<T[]>() {}.capture(); 66 assertVisited(type); 67 new BaseTypeVisitor() { 68 @Override void visitGenericArrayType(GenericArrayType t) {} 69 }.visit(type); 70 } 71 72 public <T> void testVisitParameterizedType() { 73 Type type = new TypeCapture<Iterable<T>>() {}.capture(); 74 assertVisited(type); 75 new BaseTypeVisitor() { 76 @Override void visitParameterizedType(ParameterizedType t) {} 77 }.visit(type); 78 } 79 80 public <E extends Enum<E>> void testVisitRecursiveTypeBounds() { 81 Type type = new TypeCapture<EnumSet<E>>() {}.capture(); 82 assertVisited(type); 83 new BaseTypeVisitor() { 84 @Override void visitParameterizedType(ParameterizedType t) { 85 visit(t.getActualTypeArguments()); 86 } 87 @Override void visitTypeVariable(TypeVariable<?> t) { 88 visit(t.getBounds()); 89 } 90 }.visit(type); 91 } 92 93 private static void assertVisited(Type type) { 94 TypeVisitor visitor = new BaseTypeVisitor(); 95 try { 96 visitor.visit(type); 97 fail("Type not visited"); 98 } catch (UnsupportedOperationException expected) {} 99 try { 100 visitor.visit(new Type[] {type}); 101 fail("Type not visited"); 102 } catch (UnsupportedOperationException expected) {} 103 } 104 105 private static class BaseTypeVisitor extends TypeVisitor { 106 @Override void visitTypeVariable(TypeVariable<?> t) { 107 throw new UnsupportedOperationException(); 108 } 109 110 @Override void visitWildcardType(WildcardType t) { 111 throw new UnsupportedOperationException(); 112 } 113 114 @Override void visitParameterizedType(ParameterizedType t) { 115 throw new UnsupportedOperationException(); 116 } 117 118 @Override void visitClass(Class<?> t) { 119 throw new UnsupportedOperationException(); 120 } 121 122 @Override void visitGenericArrayType(GenericArrayType t) { 123 throw new UnsupportedOperationException(); 124 } 125 } 126 } 127