When writing a simple JSON serializer using reflection in Java, I was taken aback by the behavior of the Class.getMethods () class. It seems that Java Class.getMethods () returns both overriding and overridden methods if the return type of the override method extends the override method.
So, for example, given the interfaces:
static interface A { A x(); A y(); } static interface B extends A { B x(); A y(); }
A.class.getMethods() returns an array of two methods, as expected however B.class.getMethods() returns an array of 3 methods (which was a bit contrasting intuitive for me). Out of 3, 1 corresponds to y() , as expected, but the other two correspond to the original x() with the return type A and the overriding version x() with the return type B respectively. It seemed a little strange to me, but the original x() in the array, since it is not accessible anywhere. Anyway, my question is this:
Is there an easy way to get a list of only the most specialized versions of class methods without resorting to manually checking overridden methods and filtering them?
source share