5

I have a long story.

java generics bounds type

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.

Community
  • 1
  • 1
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184

3 Answers3

2

No, method.invoke(...) is not a generic method even entire Method class is not generic. So it returns Object as result and you have to cast.

// no warning, huh? :)
final JAXBElement<?> result = ...

Yes no warnings but JAXBElement<?> puts some restrictions, for instance you cannot put values to this data structure. You can just get Objects from there, so you probably will need to canst it later in you code...

nkukhar
  • 1,975
  • 2
  • 18
  • 37
1

You need to declare the generic type V in your method like this:

public <V> JAXBElement<V> unmarshal(..., Class<V> valueType)

Other than that, I can't find any mistakes in your code.

tbodt
  • 16,609
  • 6
  • 58
  • 83
1

In general the short answer is no.

However if you think of it, what would you like to achieve? You've said you want to invoke a piece of code via reflection. Generics are used to prove your code is correct at the compilation time (therefore are erased at compilation time, and in general not available at runtime).

If you call method by reflection you could literally call it with any types and get any type in result. It would be very difficult task to prove the correctness at the compile time.

Maybe you could modify your code that it doesn't use the reflection, and thus avoid the casting.

lpiepiora
  • 13,659
  • 1
  • 35
  • 47