JDK-8062389 : Class.getMethod() is inconsistent with Class.getMethods() results
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 9
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2014-10-29
  • Updated: 2017-08-01
  • Resolved: 2017-01-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b151Fixed
Related Reports
Blocks :  
CSR :  
Relates :  
Relates :  
Relates :  
Description
The following program:

import java.util.stream.Collectors;
import java.util.stream.Stream;

public class GetMethodTest {

    static void test(Class<?> clazz) throws Exception {

        System.out.println(clazz.getName() + ".class.getMethods():  " +
                           Stream
                               .of(clazz.getMethods())
                               .filter(m -> m.getDeclaringClass() != Object.class)
                               .collect(Collectors.toList()));

        System.out.println(clazz.getName() + ".class.getMethod(\"m\"): " +
                           clazz.getMethod("m"));
    }

    public static void main(String[] args) throws Exception {
        test(B.class);
    }
}

interface I {
    void m();
}

interface J extends I {
    default void m() {}
}

abstract class A implements I {}

abstract class B extends A implements J {}


Prints:

B.class.getMethods():  [public default void J.m()]
B.class.getMethod("m"): public abstract void I.m()


I think that getMethods() gets it right. getMethod() stops searching (super)interfaces as soon as a method is found on superclass (here the superclass is A, which inherits I.m abstract method). It should consolidate this abstract method with possible default methods comming from (super)interfaces that might override this method.
Comments
Re-application of fix for this bug is tracked by JDK-8172190
03-01-2017

Reopened because the change was backed out to resolve test failures
26-12-2016

Just a note that a patch for JDK-8061950 is in preparation which basically rewrites the code of getMethod() and getMethods(). It might be a good idea to wait and fix this on top of JDK-8061950 changes.
01-04-2015