java - List的Java反射数组
问题描述
我正在尝试实现一个可以将值初始化为任何类的对象的类。简单结构的初始化已经可以使用,但是我在尝试初始化 Lists ( List<String>[]
) 数组时遇到了困难。
有没有办法找出数组的ParameterizedType
而不是Class
数组的getComponentType()
?
数组的创建:
if (cls.isArray()) {
Class<?> c = cls.getComponentType();
if (!c.isPrimitive()) {
Array array = new Array(c, this.sizeArrays);
for (int i = 0; i < this.sizeArrays; i++) {
array.set(i, init(c, c, this.sizeArrays, this.sizeCollection, this.recursionCount, this.values, this.ignoredClass));
}
return array.getArray();
}
类数组:
class Array<E> {
private final E[] objArray;
public final int length;
public Array(
Class<E> dataType,
int length
) {
//noinspection unchecked
this.objArray = (E[]) java.lang.reflect.Array.newInstance(dataType, length);
this.length = length;
}
void set(
int i,
E e
) {
objArray[i] = e;
}
E[] getArray() {
return objArray;
}
}
创建列表:
if (Collection.class.isAssignableFrom(cls)) {
ParameterizedType t = ((ParameterizedType) cls.getGenericSuperclass());
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
Collection collection;
if (List.class.isAssignableFrom(cls)) {
collection = new ArrayList(this.sizeCollection);
} else if (Set.class.isAssignableFrom(cls)) {
collection = new HashSet(this.sizeCollection);
} else if (Queue.class.isAssignableFrom(cls)) {
collection = new LinkedList();
} else {
collection = new ArrayList(this.sizeCollection);
}
for (int i = 0; i < this.sizeCollection; i++) {
collection.add(init((Class<?>) pt.getActualTypeArguments()[0], pt.getActualTypeArguments()[0], this.sizeArrays, this.sizeCollection, this.recursionCount, this.values, this.ignoredClass));
}
return collection;
}
}
解决方案
Type
描述 aList<String>[]
是 a ,而GenericArrayType
不是 a ParameterizedType
。
下面的代码将说明这一点。代码会直接转换,因为我们知道字段类型,实际代码当然会instanceof
在转换之前使用。
List<String>[] x;
public static void main(String[] args) throws Exception {
Field field = Test.class.getDeclaredField("x");
Class<?> fieldType = field.getType();
System.out.println(fieldType); // class [Ljava.util.List;
System.out.println(fieldType.isArray()); // true
System.out.println(fieldType.getComponentType()); // interface java.util.List
GenericArrayType arrayType = (GenericArrayType) field.getGenericType();
ParameterizedType compType = (ParameterizedType) arrayType.getGenericComponentType();
System.out.println(arrayType); // java.util.List<java.lang.String>[]
System.out.println(compType); // java.util.List<java.lang.String>
System.out.println(compType.getRawType()); // interface java.util.List
for (Type argType : compType.getActualTypeArguments())
System.out.println(" " + argType); // class java.lang.String
}
推荐阅读
- javascript - .addEventListener 不起作用,有什么想法吗?
- php - PHP/MySQL - 从后台运行操作/处理运行时间较长的方法
- ruby - 为什么 jekyll 服务因分页而失败?
- excel - 如何找到一行中最后一个非空单元格的列位置?
- linux - rsync 目录变成更扁平的结构
- ssl - hunchentoot 用 ssl 定义简单处理程序?
- angular - 如何使用 Angular 5 对 HTTP GET 响应进行排序以一次获取一个元素
- filter - Odoo 11 - 按公司过滤产品
- routes - 如何设置在查询字符串中采用多个参数的 Phoenix 路由
- python - 为什么我的 Python 程序在运行第一个循环后关闭?