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

(reflect) Class.getDeclaredMethods() is returning inherited methods

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0_12"
      Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
      Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      SunOS folgers.amer.interwoven.com 5.10 Generic_127127-11 sun4u sparc SUNW,Sun-Blade-1500
      Linux iwov-livesite-3 2.6.18-53.el5 #1 SMP Wed Oct 10 16:34:02 EDT 2007 i686 i686 i386 GNU/Linux
      Microsoft Windows [Version 5.2.3790]

      A DESCRIPTION OF THE PROBLEM :
      Assume class A is abstract, class B is concrete and extends A.
      Calling B.class.getDeclaredMethods() returns class A's method signatures in addition to class B's.

      While http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getDeclaredMethods() clearly states that "This includes public, protected, default (package) access, and private methods, but excludes inherited methods."

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create abstract class A with method foo.
      2. Create class B extends A without any method.
      3. B.class.getDeclaredMethods() returns method foo().

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      method foo() which is inherited from abstract parent class should not be returned from getDeclaredMethods() call.
      ACTUAL -
      method foo() which is inherited from abstract parent class is returned from getDeclaredMethods() call.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      //---------------- A.java -----------------
      abstract class A {
          public void foo() {}
      }

      //---------------- B.java -----------------
      import java.lang.reflect.*;

      public class B extends A {
          public static void main(String[] args) throws Exception {
              Method[] methods = B.class.getDeclaredMethods();
              for( int i = 0 ; i < methods.length ; i++ ){
                  System.out.println(methods[i]);
              }
          }
      }

      //-------------- JDK 1.5 run -------------------
      > javac *.java
      > java -classpath . B
      public static void B.main(java.lang.String[]) throws java.lang.Exception

      //-------------- JDK 1.6 run -------------------
      > javac *.java
      > java -classpath . B
      public static void B.main(java.lang.String[]) throws java.lang.Exception
      public void B.foo()

      //--------------- conclusion -------------------
      public void B.foo() is returned in JDK 1.6 which contradict with what the JavaDoc states.
      ---------- END SOURCE ----------

      Release Regression From : 5.0u17
      The above release value was the last known release where this
      bug was not reproducible. Since then there has been a regression.

            darcy Joe Darcy
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: