I’ve run into a situation where I have a Method instance from an interface definition and a Method instance of the same method from an implementing class.
For example:
public interface Foo {
void bar();
}
public class FooImpl implements Foo {
public void bar() {
System.out.println("foobar");
}
}
Using reflection, I have obtained a Method instance of bar from the Foo interface and its derived implementation from FooImpl. Essentially, I need to consider these Methods equal, but obviously the equals method indicates they are not equivalent.
Specifically, I need a way to determine if a given Method obtained through reflection overrides a superclass/interface Method.
I’m currently using a workaround which avoids the situation completely by finding the “least-derived” method from a given class, but I’m wondering if there’s a more elegant solution.
I haven’t taken the modifiers into consideration in the equals method but this should get you reasonably close if you want to use this algorithm: