|
| 1 | +## 反映泛型类型 |
| 2 | + |
| 3 | +反射库提供了一个 `Type` 接口来描述一个通用类型。 有一个类实现了这个接口和四个其他接口来扩展它,对应于五种不同的类型: |
| 4 | + |
| 5 | + - `Class` 类,表示原始类型或原始类型 |
| 6 | + |
| 7 | + - 接口 `ParameterizedType`,表示通用类或接口的参数类型的应用程序,您可以从中提取参数类型的数组 |
| 8 | + |
| 9 | + - `TypeVariable` 接口,代表一个类型变量,从中可以提取类型变量的边界 |
| 10 | + |
| 11 | + - `GenericArrayType` 接口,表示数组,您可以从中提取数组组件类型 |
| 12 | + |
| 13 | + - `WildcardType` 接口,表示通配符,您可以从中抽取通配符的下限或上限 |
| 14 | + |
| 15 | +通过在每个接口上执行一系列实例测试,您可以确定您拥有哪种类型,并打印或处理类型;我们将很快看到一个例子。 |
| 16 | + |
| 17 | +方法可用于将类的超类和超接口作为类型返回,并访问字段的泛型类型,构造函数的参数类型以及方法的参数和结果类型。 |
| 18 | + |
| 19 | +您还可以提取代表类或接口声明或泛型方法或构造函数的形式参数的类型变量。类型变量的类型需要一个参数,并写入 `TypeVariable<D>`,其中 `D` 表示声明类型变量的对象的类型。因此,类的类型变量具有类型 `TypeVariable<Class<?>>`,而泛型方法的类型变量具有类型 `TypeVariable<Method>`。可以说,类型参数是令人困惑的,并不是非常有用。由于它对6.6节中描述的问题负责,因此 `Sun` 可能会在将来删除它。 |
| 20 | + |
| 21 | +例 `7-5` 使用这些方法打印出与类关联的所有标题信息。这里有两个使用例子: |
| 22 | + |
| 23 | +```java |
| 24 | + % java ReflectionDemo java.util.AbstractList |
| 25 | + class java.util.AbstractList<E> |
| 26 | + extends java.util.AbstractCollection<E> |
| 27 | + implements java.util.List<E> |
| 28 | + |
| 29 | + % java ReflectionDemo java.lang.Enum |
| 30 | + class java.lang.Enum<E extends java.lang.Enum<E>> |
| 31 | + implements java.lang.Comparable<E>,java.io.Serializable |
| 32 | +``` |
| 33 | + |
| 34 | +例 `7-5` 中的代码冗长而直接。 它包含打印类的每个组件的方法:它的超类,它的接口,它的字段和它的方法。 代码的核心是 `printType` 方法,它使用级联的实例测试根据上述五种情况对类型进行分类。 |
| 35 | + |
| 36 | +例 `7-5`。 如何操作Type类型 |
| 37 | + |
| 38 | +```java |
| 39 | + import java.util.*; |
| 40 | + import java.lang.reflect.*; |
| 41 | + import java.io.*; |
| 42 | + class ReflectionDemo { |
| 43 | + private final static PrintStream out = System.out; |
| 44 | + public static void printSuperclass(Type sup) { |
| 45 | + if (sup != null && !sup.equals(Object.class)) { |
| 46 | + out.print("extends "); |
| 47 | + printType(sup); |
| 48 | + out.println(); |
| 49 | + } |
| 50 | + } |
| 51 | + public static void printInterfaces(Type[] impls) { |
| 52 | + if (impls != null && impls.length > 0) { |
| 53 | + out.print("implements "); |
| 54 | + int i = 0; |
| 55 | + for (Type impl : impls) { |
| 56 | + if (i++ > 0) out.print(","); |
| 57 | + printType(impl); |
| 58 | + } |
| 59 | + out.println(); |
| 60 | + } |
| 61 | + } |
| 62 | + public static void printTypeParameters(TypeVariable<?>[] vars) { |
| 63 | + if (vars != null && vars.length > 0) { |
| 64 | + out.print("<"); |
| 65 | + int i = 0; |
| 66 | + for (TypeVariable<?> var : vars) { |
| 67 | + if (i++ > 0) out.print(","); |
| 68 | + out.print(var.getName()); |
| 69 | + printBounds(var.getBounds()); |
| 70 | + } |
| 71 | + out.print(">"); |
| 72 | + } |
| 73 | + } |
| 74 | + public static void printBounds(Type[] bounds) { |
| 75 | + if (bounds != null && bounds.length > 0 && !(bounds.length == 1 && bounds[0] == Object.class)) { |
| 76 | + out.print(" extends "); |
| 77 | + int i = 0; |
| 78 | + for (Type bound : bounds) { |
| 79 | + if (i++ > 0) out.print("&"); |
| 80 | + printType(bound); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + public static void printParams(Type[] types) { |
| 85 | + if (types != null && types.length > 0) { |
| 86 | + out.print("<"); |
| 87 | + int i = 0; |
| 88 | + for (Type type : types) { |
| 89 | + if (i++ > 0) out.print(","); |
| 90 | + printType(type); |
| 91 | + } |
| 92 | + out.print(">"); |
| 93 | + } |
| 94 | + } |
| 95 | + public static void printType(Type type) { |
| 96 | + if (type instanceof Class) { |
| 97 | + Class<?> c = (Class)type; |
| 98 | + out.print(c.getName()); |
| 99 | + } else if (type instanceof ParameterizedType) { |
| 100 | + ParameterizedType p = (ParameterizedType)type; |
| 101 | + Class c = (Class)p.getRawType(); |
| 102 | + Type o = p.getOwnerType(); |
| 103 | + if (o != null) { printType(o); out.print("."); } |
| 104 | + out.print(c.getName()); |
| 105 | + printParams(p.getActualTypeArguments()); |
| 106 | + } else if (type instanceof TypeVariable<?>) { |
| 107 | + TypeVariable<?> v = (TypeVariable<?>)type; |
| 108 | + out.print(v.getName()); |
| 109 | + } else if (type instanceof GenericArrayType) { |
| 110 | + GenericArrayType a = (GenericArrayType)type; |
| 111 | + printType(a.getGenericComponentType()); |
| 112 | + out.print("[]"); |
| 113 | + } else if (type instanceof WildcardType) { |
| 114 | + WildcardType w = (WildcardType)type; |
| 115 | + Type[] upper = w.getUpperBounds(); |
| 116 | + Type[] lower = w.getLowerBounds(); |
| 117 | + if (upper.length == 1 && lower.length == 0) { |
| 118 | + out.print("? extends "); |
| 119 | + printType(upper[0]); |
| 120 | + } else if (upper.length == 0 && lower.length == 1) { |
| 121 | + out.print("? super "); |
| 122 | + printType(lower[0]); |
| 123 | + } else |
| 124 | + throw new AssertionError(); |
| 125 | + } |
| 126 | + } |
| 127 | + public static void printClass(Class c) { |
| 128 | + out.print("class "); |
| 129 | + out.print(c.getName()); |
| 130 | + printTypeParameters(c.getTypeParameters()); |
| 131 | + out.println(); |
| 132 | + printSuperclass(c.getGenericSuperclass()); |
| 133 | + printInterfaces(c.getGenericInterfaces()); |
| 134 | + } |
| 135 | + public static void main(String[] args) throws ClassNotFoundException { |
| 136 | + for (String name : args) { |
| 137 | + Class<?> c = Class.forName(name); |
| 138 | + printClass(c); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +``` |
| 143 | + |
| 144 | +如果 `Type` 接口有一个 `toGenericString` 方法,那么大部分代码都是不必要的。 `Sun` 正在考虑这一改变。 |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
0 commit comments