I have methods with a single parameter of type Event<Something>
and need to extract the Something
or its raw type. It may look like
void method1(Event<MyEnum1> event) {}
<E extends Enum<E>> method2(Event<E> event) {}
<K, V> method3(Event<Map<K, V>> event) {}
and I'd like a function mapping
method1 -> MyEnum.class
method2 -> Enum.class
method3 -> Map.class
For method1
, it's easy using (ParameterizedType) method.getGenericParameterTypes()
. For method2
, I can use Guava
TypeToken.of(method.getDeclaringClass())
.method(method)
.getTypeParameters()[0]
.getBounds()
to obtain a Type
and
TypeToken.of(method.getDeclaringClass())
.method(method)
.getParameters()
.get(0)
to obtain the Parameter
. I guess, I need to resolve the Parameter
using the Type
, but I'm stuck here.
I don't care much about method3
, though it'd be good to know a general way. I don't care about the type of actual parameters (I'm aware of the erasure).
Aucun commentaire:
Enregistrer un commentaire