如何通过反射来继承参数化的字段类?

时间:2018-07-16 12:42:20

标签: java inheritance reflection

class A extends B<C> {
   ...
}

class B<T extends E> {
   ...
   public D<T> field;
}

如何从“场”反射中获得“ C”类?

现在,我可以将类“ B”的字段作为“ A”超类的字段获取,好像它们不知道从“ A”传递哪个类型参数,并且仅显示类型“ E”。

2 个答案:

答案 0 :(得分:3)

即使A用具体类型B扩展了C,也不会改变您从反射中获得的B中字段的类型。

但是,您可以手动将字段的type变量映射到具体类型。首先,为TypeVariable类构建A到具体类型的映射,然后使用该映射将B中的类型变量转换为具体类型。

Class<?> cls = A.class; // using `A` as an example
Class<?> sup = cls.getSuperclass();

 // TypeVariables of `B`
TypeVariable<?>[] tv = sup.getTypeParameters();
 // Concrete types used to extend with in `A`
Type[] actual = ((ParameterizedType) cls.getGenericSuperclass()).getActualTypeArguments();

Map<TypeVariable<?>, Type> mappings // map one to the other
    = IntStream.range(0, tv.length)
        .boxed()
        .collect(Collectors.toMap(i -> tv[i], i -> actual[i]));


for(Field f : sup.getDeclaredFields()) {
    Type t1 = f.getGenericType();
    System.out.println(t1); // prints `D<T>`
    Type t2 = concretify(t1, mappings);
    System.out.println(t2); // prints `D<C>`
}
public static Type concretify(Type from, Map<TypeVariable<?>, Type> mappings) {
    if(from instanceof ParameterizedType) {
        ParameterizedType pt = (ParameterizedType) from;

        Type[] ts = pt.getActualTypeArguments();
        Type[] result = new Type[ts.length];
        for (int i = 0; i < ts.length; i++) {
            result[i] = mappings.getOrDefault(ts[i], ts[i]);
        }

        return new ParameterizedType() { // some ParameterizedType implementation

            @Override
            public Type getRawType() {
                return pt.getRawType();
            }

            @Override
            public Type getOwnerType() {
                return pt.getOwnerType();
            }

            @Override
            public Type[] getActualTypeArguments() {
                return result;
            }

            @Override
            public String toString() {
                return String.format("%s<%s>", getRawType().getTypeName(),
                    Arrays.stream(getActualTypeArguments())
                        .map(Type::getTypeName)
                        .collect(Collectors.joining(",")));
            }
        };
    }
    return from;
}

答案 1 :(得分:0)

我对@JornVernee想法的实现。

用法:

getFieldsTypesMap(A)

代码:

private static Map<Field, Type> getFieldsTypesMap(Class<?> aClass, Type[] actualTypeParameters) {
    Map<Field, Type> fieldTypeMap = new HashMap<>();
    TypeVariable<? extends Class<?>>[] typeParameters = aClass.getTypeParameters();

    Map<TypeVariable<?>, Type> mappings = new HashMap<>();

    if (typeParameters.length > 0 && actualTypeParameters != null) {
        mappings = IntStream.range(0, typeParameters.length)
                .boxed()
                .collect(Collectors.toMap(i -> typeParameters[i], i -> actualTypeParameters[i]));
    }
    for (Field f : aClass.getDeclaredFields()) {
        if (Modifier.isStatic(f.getModifiers())) {
            continue;
        }
        Type fieldType = getRealFieldType(f.getGenericType(), mappings);
        fieldTypeMap.put(f, fieldType);
    }

    Class<?> superclass = aClass.getSuperclass();
    Type genericSuperclass = aClass.getGenericSuperclass();
    if (genericSuperclass instanceof ParameterizedType) {
        Type[] actual = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
        Type[] remapped = new Type[actual.length];
        for (int i = 0; i < actual.length; i++) {
            remapped[i] = getRealFieldType(actual[i], mappings);
        }
        fieldTypeMap.putAll(getFieldsTypesMap(superclass, remapped));
    } else if (superclass != null) {
        fieldTypeMap.putAll(getFieldsTypesMap(superclass, null));
    }
    return fieldTypeMap;
}

private static Type getRealFieldType(Type from, Map<TypeVariable<?>, Type> mappings) {
    if(from instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) from;

        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        Type[] result = new Type[actualTypeArguments.length];
        for (int i = 0; i < actualTypeArguments.length; i++) {
            result[i] = getRealFieldType(mappings.getOrDefault(actualTypeArguments[i], actualTypeArguments[i]), mappings);
        }

        return new ParameterizedType() {

            @Override
            public Type getRawType() {
                return parameterizedType.getRawType();
            }

            @Override
            public Type getOwnerType() {
                return parameterizedType.getOwnerType();
            }

            @Override
            public Type[] getActualTypeArguments() {
                return result;
            }

            @Override
            public String toString() {
                return String.format("%s<%s>", getRawType().getTypeName(),
                        Arrays.stream(getActualTypeArguments())
                                .map(Type::getTypeName)
                                .collect(Collectors.joining(",")));
            }
        };
    }
    return mappings.getOrDefault(from, from);
}