mercredi 15 février 2017

Getting typed return of method using java reflection

If I have a class like so:

class Test {
    Thing<String> getThing(String thing) {}
}

how can I determine String.class using reflection on an instance of Test? The path I want to take is new Test() -> get class -> find method -> get return type ->-> somehow get String.class, but I'm not finding how the last part is done. What I have so far gets me Thing<java.lang.String> but not the inner class.

import org.reflections.ReflectionUtils;

Iterables.getOnlyElement(ReflectionUtils.getAllMethods((new Test()).getClass())).getGenericType();

whereas .getReturnType() just gets me a Thing.class...





Aucun commentaire:

Enregistrer un commentaire