I need to ensure forward compatibility of my application with a dependency that introduced new hook methods to the superclass my application extends. The straightforward approach of introducing the newly added methods (to be ignored by the old version I build against and utilized by the new one) stopped working as soon as I started to define return types that are subtypes of declared ones.
When I call my overridden method directly as foo.bar("")
a superclass method gets called. However, when I invoke it through reflection from debugger foo.getClass().getMethod("bar", String.class).invoke(foo, "")
, it calls the overridden method as expected. The method gets called correctly when its return type is narrowed to the same type overridden methods return, it was a subtype before.