mercredi 15 août 2018

How to get "genericInfo" from method in java reflection

I have class, DummyController,

@APSController
@RequestMapping("grand/old/dummy")
public class DummyContollerV2 {

    private final EntityResponseWrapper<Dummy> wrapper = EntityResponseWrapper.buildForEntity(Dummy.class);

    @RequestMapping(value = "", method = RequestMethod.POST)
    public HttpEntity<DendriteEnvelope<Dummy>> createNewDummy(
            @RequestBody final Dummy newDummy) {

        newDummy.setId("3");
        newDummy.setApproved(false);
        newDummy.setValue(ValuesEnum.VALUE_2);
        return wrapper.wrapEntityResult(() -> newDummy);
    }
}

What I want is tree of the return type, and its generics, of the "createNewDummy" method.

I have found, through debugging, that I can call the method:

    Class controllerClass = DummyContollerV2.class;
    List<Method> methods = Arrays.asList(controllerClass.getMethods());
    ClassTypeSignature returnSigniture = methods.get(0).getGenericInfo().getTree().getReturnType();

Where the "returnSignature" let's me traverse each class and the "ClassTypeSignature" of its generic types. However, the "getGernicInfo" method is private, and can only be accessed through debugging (or hacking with more reflection).

It shows that the generic information of the return class is reserved at run time. Does anyone know how I can access this information?

I know this question is probably a duplicate, but I wasn't able to find a question that addressed this exactly.





Aucun commentaire:

Enregistrer un commentaire