Java 什么可能是ParameteredType的实例?
阅读了接口文档后,我认为ParameterizedType实例的示例可以是任何参数化类型,例如我的代码中的Java 什么可能是ParameteredType的实例?,java,generics,reflection,Java,Generics,Reflection,阅读了接口文档后,我认为ParameterizedType实例的示例可以是任何参数化类型,例如我的代码中的col: public class a0 { public static void main(String[] args) { Collection<String> col = new ArrayList<String>(); col.add("a"); col.add("b"); co
col
:
public class a0 {
public static void main(String[] args) {
Collection<String> col = new ArrayList<String>();
col.add("a");
col.add("b");
col.add("c");
assert col instanceof ParameterizedType; // line No. 10
}
}
因此,什么可能是ParameteredType的实例
我想知道这一点,因为我试图理解一个更伟大的程序,其中有这样一个片段:
public static void printType(Type type) {
...
if (type instanceof ParameterizedType) {
... }
}
但我不知道
if
语句中的条件何时为真。aparameteredType
是表示参数化类型的反射类型。参数化类型本身不是该类的实例
类似地,实例用于表示特定类型的类成员字段。这并不意味着这样一个字段将是field
的实例
参数化类型的示例如下所示:
Type[] genericInterfaces = col.getClass().getGenericInterfaces();
Type type = genericInterfaces[0];
assert type instanceof ParameterizedType;
Type[] genericInterfaces = col.getClass().getGenericInterfaces();
Type type = genericInterfaces[0];
assert type instanceof ParameterizedType;