Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8062389

Class.getMethod() is inconsistent with Class.getMethods() results

    XMLWordPrintable

Details

    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.

      Attachments

        Issue Links

          Activity

            People

              plevart Peter Levart
              plevart Peter Levart
              Votes:
              0 Vote for this issue
              Watchers:
              10 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: