为什么不使用 java.util.List 检查 java.lang.reflect.Field 的实例

Gaa*_*kar 1 java reflection

我想检查字段类型是否为列表,但这给出了错误:

Incompatible conditional operand types Class<capture#5-of ?> and 
     List
- Incompatible conditional operand types Class<capture#6-of ?> and 
     List
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

private void convert(Class<?> load) {

        Field[] fields = load.getDeclaredFields();
        int i = 0;
        for (Field field : fields) {
            Class<?> type = field.getType();
            if (type instanceof java.util.List) {
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 6

private void convert(Class<?> load) {

    Field[] fields = load.getDeclaredFields();
    int i = 0;
    for (Field field : fields) {
        Class<?> type = field.getType();
        if (java.util.List.class.isAssignableFrom(type)) {
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

instanceof 运算符仅用于实例!