JDK-4869001 : (reflect) Class.getDeclaringClass() returns null for anonymous member classes
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.4.1,6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,solaris_8
  • CPU: generic,sparc
  • Submitted: 2003-05-23
  • Updated: 2012-09-28
  • Resolved: 2005-06-09
Related Reports
Duplicate :  
Relates :  
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) 
======================================================================

Comments
EVALUATION I disagree with this statement made in the description: 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". JLS 2nd edition, section 8.2, does not list anonymous classes as class members. The specification of Class.getDeclaringClass clearly states that the method will return null if the class or interface is not a member of any other class. The current behaviour is correct. Please provide a reference to the section in the JLS which supports your claim. -- iag@sfbay 2003-05-23 This rfe was meant to challenge the Spec. Perhaps it should be moved to a different subcategory. ###@###.### 2003-06-02 At best an anonymous class has a declaring method which itself has a declaring class so a new method getDeclaringMethod which returns the outer enclosing method for anonymous classes and null otherwise may be appropriate. -- iag@sfbay 2003-06-02 The requested functionality was added to JDK 5. ###@###.### 2005-06-09 18:52:23 GMT
02-06-2003