有没有办法在Java中获取字段的泛型类型?
我有以下对象变量:
protected ScheduleView<WantedClass> scheduleLine1;
protected ScheduleView<SomeOtherClass> scheduleLine2;
现在我尝试使用反射来获取ScheduleView
类型为WantedClass
作为泛型类型的所有对象变量:
Arrays.asList(this.getClass().getDeclaredFields()).stream().map(field -> {
ScheduleView<WantedClass> retValue = null;
System.out.println(field.getGenericType()); // prints control.ScheduleView<dto.WantedClass>
try {
if (field.getType() == ScheduleView.class) { // here I also want to check if the generic type is WantedClass
retValue = (ScheduleView<WantedClass>) field.get(this);
} else {
retValue = null;
}
} catch (IllegalAccessException e) {
retValue = null;
} finally {
return retValue;
}
}).filter(scheduleView -> scheduleView != null).forEach(scheduleView -> {
/* some more code */
});
问题是,如果泛型类型为WantedClass
,我还想检查if-Statement。我也尝试使用getGenericType()
方法,但似乎这样的事情是不可能的:
field.getGenericType() == ScheduleView<WantedClass>.class
那么有没有办法获得字段的泛型类型?
答案 0 :(得分:4)
您必须检查该字段是否为ParameterizedType
,然后检查getActualTypeArguments
的内容,例如
for (Field field : Ideone.class.getDeclaredFields()) {
Type type = field.getGenericType();
if (type instanceof ParameterizedType) {
ParameterizedType ptype = (ParameterizedType) type;
if (ptype.getRawType() == ScheduledView.class) {
if (ptype.getActualTypeArguments().length == 1
&& ptype.getActualTypeArguments()[0] == WantedClass.class) {
// Do whatever with the field.
System.out.println(field.getName());
}
}
}
}
答案 1 :(得分:2)
使用流来实现@Andy Turner的答案:
// For each declared field in the class...
Arrays.stream(Ideone.class.getDeclaredFields())
// select fields...
.filter(
// that have a generic type...
field -> Stream.of(field.getGenericType())
// that is an instance of ParametrizedType...
.filter(ParameterizedType.class::isInstance)
.map(ParameterizedType.class::cast)
// whose raw type is ScheduledView...
.filter(ptype -> ptype.getRawType().equals(ScheduledView.class))
// and whose actual type arguments...
.map(ParameterizedType::getActualTypeArguments)
.flatMap(Arrays::stream)
// include WantedClass...
.anyMatch(WantedClass.class::equals)
)
// then get their names...
.map(Field::getName)
// and print them.
.forEach(System.out::println)
;