我有一個很長的故事。無法警告投射泛型類型?
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!更新。謝謝。 –