List的Java反射数组



我正在尝试实现一个可以将值初始化为任何类的对象的类。简单结构的初始化已经起作用了,但我在尝试初始化列表数组(List<String>[](时遇到了困难。

有没有办法通过getComponentType()找到数组的ParameterizedType而不仅仅是Class

创建阵列:

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;
}
}

描述List<String>[]TypeGenericArrayType,而不是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
}

最新更新