簡體   English   中英

列表的 Java 反射 arrays

[英]Java reflection arrays of List

我正在嘗試實現一個 class,它可以將值初始化為任何 class 的 object。 簡單結構的初始化已經可以使用,但是我在嘗試初始化列表數組( 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 陣列:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM