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

(reflect) Class.getMethods() returns overridden and hidden method of interface

XMLWordPrintable



      Name: laC46010 Date: 01/28/99



      The Class.getMethods() returns non-member methods.
      If an interface declares method which overrides method with the same
      signature in the superinterfaces then Class.getMethods()
      returns these overridden methods.
      Also, Class.getMethods() returns hidden static methods.

      These methods are not inherited.

      In the JLS chapter 8.4.6, p.165 reads:
      "A class inherits from its direct superclass and direct superinterfaces
      all the methods (whether abstract or not) of the superclass and
      superinterfaces that are accessible to code in the class and are
      neither overridden (8.4.6.1) nor hidden (8.4.6.2) by a declaration
      in the class."

      In the Java Platform 1.2 API Specification reads:
      "public Method[] getMethods()throws SecurityException

      Returns an array containing Method objects reflecting all the public
      member methods of the class or interface represented by this Class
      object, including those declared by the class or interface and and
      those inherited from superclasses and superinterfaces."

      This bug is reproduced under all JDK versions from jdk1.1 to jdk1.2.2D

      The example below produces the following output.
      -----------------output------------------
      class Test
      public static void Super.test1()
      public static void Test.main(java.lang.String[])
      public static void Test.test1()
      public boolean java.lang.Object.equals(java.lang.Object)
      public final native java.lang.Class java.lang.Object.getClass()
      public native int java.lang.Object.hashCode()
      public final native void java.lang.Object.notify()
      public final native void java.lang.Object.notifyAll()
      public java.lang.String java.lang.Object.toString()
      public final void java.lang.Object.wait() throws java.lang.InterruptedException
      public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
      public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
      interface I
      public abstract void I1.test()
      public abstract void I.test()
      ---------------Test.java-----------------
      import java.lang.reflect.Method;

      class Super {
          public static void test1() {
          }
      }

      interface I1 {
          abstract public void test();
      }

      interface I extends I1 {
          abstract public void test();
      }

      class Test extends Super {
          public static void test1() {
          }

          static public void main(String arg[]) {
              Class c = Test.class;
              Method[] meths = c.getMethods();
              System.out.println(c);
              for (int i = 0; i < meths.length; System.out.println(meths[i++]));
              c = I.class;
              meths = c.getMethods();
              System.out.println(c);
              for (int i = 0; i < meths.length; System.out.println(meths[i++]));
          }
      }


      ======================================================================

            iris Iris Clark
            leosunw Leo Leo (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: