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

(reflect) Method.getDeclaringClass() doesn't return the right Class

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0_11"
      Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
      Java HotSpot(TM) Server VM (build 11.0-b16, mixed mode)
      ---
      java version "1.6.0_13"
      Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
      Java HotSpot(TM) Server VM (build 11.3-b02, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux laptop 2.6.27-11-generic #1 SMP Wed Apr 1 20:57:48 UTC 2009 i686 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      The method Method#getDeclaringClass() in a particular set of classes doesn't return the correct Class.

      The following example reproduce show the class configuration:

      example
      ----------------
      public abstract class DimensionSet {
      protected static class NaturalKey {
      public String getHash() {
      return "hash";
      }
      }
      }

      public class DimTag extends DimensionSet {
      public static class NaturalKey extends DimensionSet.NaturalKey {}
      }
      ----------------

      If I ask the declaring class for the method getHash() from DimTag$NaturalKey this example I should get DimensionSet$NaturalKey
      because is where the getHash() method is declared.

      But with JDK/JRE 1.6.0_7 - 11 - 13 I get DimTag$NaturalKey,
      Using JDK 1.5.0_17-b04 and JDT compile for java 1.6 I get correctly DimensionSet$NaturalKey

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Take the following classes example

      -------------------------------------------------
      package javabug;

      public abstract class DimensionSet {

      protected static class NaturalKey {

      public String getHash() {
      return "hash";
      }
      }

      }
      --------------------------------------------------------
      package javabug;

      public class DimTag extends DimensionSet {

      public static class NaturalKey extends DimensionSet.NaturalKey {}

      }
      ----------------------------------------------------------
      package javabug;

      import java.lang.reflect.Method;

      public class JavaBugTest
      {
      public static void main( String [] args) throws Exception
      {
      Class c = Class.forName("javabug.DimTag$NaturalKey" );
      Method method = c.getMethod( "getHash", new Class[0]);
      boolean correct = method.getDeclaringClass().getName().equals( "javabug.DimensionSet$NaturalKey" );
      System.out.println( "Method: " + method.getName() +", declared:"+method.getDeclaringClass()+" ---> "+(correct ? "correct" : "ERROR!!!") );
      }
      }
      ---------------------------------------------------------------------------

      compile and run javabug.JavaBugTest



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Method: getHash, declared:class javabug.DimensionSet$NaturalKey ---> correct

      ACTUAL -
      Method: getHash, declared:class javabug.DimTag$NaturalKey ---> ERROR!!!

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      see: Steps to Reproduce
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      change the super-inner-class from protected to public

      public abstract class DimensionSet {

      // protected static class NaturalKey { // <- error
      public static class NaturalKey { // <- OK

      public String getHash() {
      return "hash";
      }
      }

      }

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

            jfranck Joel Borggrén-Franck (Inactive)
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: