java - Java 反射:如何获取 Class 的实例来自 ParameterizedType.getActualTypeArguments() 数组?
问题描述
我想要做的是使用一个 Field 的通用 typearg
Type type = f.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
// ...
Class<?> typearg0 = ??? // ptype.getActualTypeArguments()[0]
}
(其中f
是 的一个实例java.lang.Field
)。我需要该实例稍后Class<?>
执行Foo.class.isAssignableFrom(typearg0)
检查,它仅将Class<T>
es 作为参数。同时,我也知道(或期望)说typearg0
是一个参数化类型,所以有一个矛盾:typearg0
不能Class<?>
同时ParameterizedType
,因为Class<?>
它本身就是一个类并且不实现ParameterizedType
。
我可以通过isAssignableFrom()
其他方式实现检查吗?或者我的目标通常是不可能实现的?
非常感谢!
编辑:
这是一个例子:
假设您期望一个类型为 Field 的字段Map<Foo, Set<Bar>>
,但您得到的只是一个java.lang.reflect.Field
实例。现在,您不希望它精确匹配Map<Foo, Set<Bar>>
,您只希望各自的类型是or的实例。这不是严格的规范检查,更像是“健全性检查”。我会尝试这样的检查:Map
Set
- 获取字段类型作为类 (
.getType()
) - 检查是否
Map
可以从字段类型分配 - 将字段类型作为(可能)参数化类型 (
.getGenericType()
) - 检查第一个通用参数是否
Foo
(这应该是完全匹配的) - 获取第二个泛型参数作为类
- 检查是否
Set
可以从此嵌套类型分配 - 检查嵌套类型本身是否为 a
ParameterizedType
并强制转换 - 检查嵌套类型的第一个泛型参数是
Bar
(这应该是完全匹配)
但是,我不知道如何达到 5 和/或 6。
(使用Java 11)
解决方案
就像您对 , 所做的一样Map
,Set
很可能是 的一个实例,ParameterizedType
因此您可以检查它然后强制转换它。
从那个ParameterizedType
(代表Set<Something>
),你可以getRawType
得到一个Set
. 那Type
也是 - 但这应该可以安全地转换为Class
. 万一不是,您可以使用后备并使用其名称加载类。
final Type valueType = mapType.getActualTypeArguments()[1];
final ParameterizedType pValueType = (ParameterizedType) valueType;
final Class<?> valueClass = pValueType.getRawType() instanceof Class ?
((Class<?>) pValueType.getRawType()) :
// Should be castable, but just in case it's not, fallback
getClass().getClassLoader().loadClass(
pValueType.getRawType().getTypeName()
);
if (Set.class.isAssignableFrom(valueClass)) { /* do something*/ }
这是一个完整的可运行示例。
class Foo {}
class Bar {}
interface MyValidMap extends Map<Foo, Set<Bar>> {}
public class Application {
public final HashMap<Foo, HashSet<Bar>> good = null;
public final Map<Foo, Set<Bar>> better = null;
public final String notAMap = null;
public final Map<String, Set<Bar>> badKey = null;
public final Map<Foo, List<String>> badValue = null;
public final Map<Foo, Set<String>> badSetElems = null;
public final MyValidMap noTypeParamMap = null;
public static void main(String[] args) throws Exception {
for (Field field : Application.class.getFields()) {
System.out.println(field.getName() + " - " + fieldMatches(field));
}
}
private static String fieldMatches(Field mapField) throws Exception {
if (!Map.class.isAssignableFrom(mapField.getType())) {
return "Field is not a Map";
}
if (!(mapField.getGenericType() instanceof ParameterizedType)) {
// We know it's a map, but it doesn't have type params. Probably something
// like this: class MyStringMap implements Map<String, String>. You can do
// something with getGenericInterfaces() but this seems so unlikely that
// it could just be ignored.
return "TODO";
}
final ParameterizedType mapType = (ParameterizedType) mapField.getGenericType();
final Type keyType = mapType.getActualTypeArguments()[0];
final Type valueType = mapType.getActualTypeArguments()[1];
if (Foo.class != keyType) {
return "Map's key type is not Foo";
}
if (!(valueType instanceof ParameterizedType)) {
// Same problem as above. May be a Set without type params
return "Map's value is (probably) not a Set";
}
final ParameterizedType pValueType = (ParameterizedType) valueType;
final Class<?> valueClass = pValueType.getRawType() instanceof Class ?
((Class<?>) pValueType.getRawType()) :
Application.class.getClassLoader().loadClass(
pValueType.getRawType().getTypeName()
);
if (!Set.class.isAssignableFrom(valueClass)) {
return "Map's value is not a Set";
}
final Type setElemType = pValueType.getActualTypeArguments()[0];
if (setElemType != Bar.class) {
return "Set's elements are not Bars";
}
return "Looks good";
}
}
输出:
good - Looks good
better - Looks good
notAMap - Field is not a Map
badKey - Map's key type is not Foo
badValue - Map's value is (probably) not a Set
badSetElems - Set's elements are not Bars
noTypeParamMap - TODO
推荐阅读
- javascript - 为什么我需要将对象传递给 Array.from?
- django - 配置不当:字段名称“titleweb_poster”对于模型“ContestDetail”无效
- python - 如何减慢 Pillow 中的图像显示速度
- google-cloud-firestore - Firestore rest api 在 arduino ide 中发出经过身份验证的请求
- python - 我如何通过 selenium 点击“javascript:void(0)”
- reactjs - 为什么我的日期输入让我回到反应前一天
- mysql - 我不明白为什么这两个带有 WHERE IN 的 SQL 查询会给出不同的结果
- c# - CefSettings 在 WPF c# 中给出错误或不工作
- python - 反复导入文件
- java - 使用 Spring MVC 时出现 404