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

(reflect) Class.getDeclaringClass() returns null for anonymous member classes

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Duplicate
    • P3
    • None
    • 1.4.1, 6
    • core-libs

    Description



      Name: nt126004 Date: 05/23/2003


      A DESCRIPTION OF THE REQUEST :
      With reference to bug report 4191731:
      Method Class.getDeclaringClass() returns null when called for anonymous member classes.

      JUSTIFICATION :
      It is quite clear that an anonymous member class must have a "declaring-class" and so it is perverse that Class.getDeclaringClass() does not correctly return the a reference to the Class object for the "declaring-class".

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Method Class.getDeclaringClass() should return Class object for the declaring-class. For example:

      public class MyClass
      {
         public OtherClass other = new OtherClass(){};
      }

      MyClass mc = new MyClass();

      we need

      mc.other.getClass().getDeclaringClass() == MyClass.class

      ACTUAL -
      Actual behavour is that

      other.getClass().getDeclaringClass() == null



      ---------- BEGIN SOURCE ----------
      package com.parctechnologies.rg.tests;

      /**
       * <p>Title: Demonstrate <code>Class.getDeclaringClass()</code> bug</p>
       * <p>Description: Demonstrate <code>Class.getDeclaringClass()</code> bug</p>
       * @author John Worrell
       * @version 1.0
       */

      public class DemoBug1
      {
          /**
           * An anonymous static member class
           */
          static Object thing1 = new Object()
          {
              public String toString()
              {
                  return "THING1";
              }
          };

          /**
           * A static member class
           */
          static class Thing2 extends Object
          {
              public String toString()
              {
                  return "THING2";
              }
          }

          /**
           * A static instance of the member class
           */
          static Object thing2 = new Thing2();

          /**
           * An anonymous member class
           */
          Object thing3 = new Object()
          {
              public String toString()
              {
                  return "THING3";
              }
          };

          /**
           * A member class
           */
          class Thing4 extends Object
          {
              public String toString()
              {
                  return "THING4";
              }
          }

          /**
           * An instance of the member class
           */
          Object thing4 = new Thing4();

          public DemoBug1()
          {
              System.out.println("Declaring Class for Thing1: " + thing1.getClass().getDeclaringClass());
              System.out.println("Declaring Class for Thing2: " + thing2.getClass().getDeclaringClass());
              System.out.println("Declaring Class for Thing3: " + thing3.getClass().getDeclaringClass());
              System.out.println("Declaring Class for Thing4: " + thing4.getClass().getDeclaringClass());
          }

          public static void main(String[] args)
          {
              DemoBug1 demoBug11 = new DemoBug1();
          }
      }
      /* END */

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Parsing and trimming the output from:

      mc.other.getClass().getName()

      can be used to find the declaring Class object.
      (Review ID: 186357)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              iris Iris Clark
              nthompsosunw Nathanael Thompson (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: