vendredi 28 août 2015

Using Method.getReturnType() on overridden method

I have a class that needs to get the return type of a method, that has been overwritten by something else.

Say that I have a class like:

public class SuperDuperClass {
    public Object someMethod();
}

I then create a subclass:

public class DuperClass extends SuperDuperClass{
    public String someMethod();
}

I then get the someMethod() for DuperClass:

Method theMethod = DuperClass.class.getMethod("someMethod", new Class[]{});
Class theMethodReturnType = theMethod.getReturnType();

I would very much like the returnType to be String, as is defined in DuperClass. However, I receive Object, as defined in SuperDuperClass. It would seem reflection doesn't really care that the overriden method returns another type, so the supermethod type is returned.

Is there some what to get the return type of the subclass in this case?

I accept both voodoo, witchcraft and blood magic.





Aucun commentaire:

Enregistrer un commentaire