我有一个很长的故事。无法警告投射泛型类型?
generics type declaration for reflection
无论如何,我有它调用使用反射的方法和返回通用结果的方法。
public static <V> JAXBElement<V> unmarshal(..., Class<V> valueType);
问题(?)是当我得到方法调用的结果。我必须投它。
final Object result = method.invoke(...);
我发现我可以投的result
到JAXBElement<V>
这样。
@SuppressWarnings("unchecked")
final JAXBElement<V> result = (JAXBElement<V>) method.invoke(...);
return result;
是否有其他方法可以做到这一点?我的意思是我得到了Class<V> valueType
使用。
如果不是,下面的语句有点cumbersome
?
// no warning, huh? :)
final JAXBElement<?> result = (JAXBElement<?>) method.invoke(...);
return new JAXBElement<V>(result.getName(), valueType, result.getScope(),
valueType.cast(result.getValue()));
谢谢。
Woops!更新。谢谢。 –