I have a long story.
generics type declaration for reflection
Anyway, I have a method which invokes a method using reflection and returns a generic result.
public static <V> JAXBElement<V> unmarshal(..., Class<V> valueType);
The problem(?) is when I got the result of method invocation. I have to cast it.
final Object result = method.invoke(...);
I found I can cast the result
into JAXBElement<V>
like this.
@SuppressWarnings("unchecked")
final JAXBElement<V> result = (JAXBElement<V>) method.invoke(...);
return result;
Is there other way to do this? I mean I got the Class<V> valueType
to use.
If not, is following statement a little bit cumbersome
?
// no warning, huh? :)
final JAXBElement<?> result = (JAXBElement<?>) method.invoke(...);
return new JAXBElement<V>(result.getName(), valueType, result.getScope(),
valueType.cast(result.getValue()));
Thanks.