-
Enhancement
-
Resolution: Duplicate
-
P3
-
None
-
1.4.1, 6
-
generic, sparc
-
generic, solaris_8
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)
======================================================================
- duplicates
-
JDK-4891872 (reflect) Extend reflection to support generics, metadata and JSR201 features
- Resolved
- relates to
-
JDK-4892994 (reflect) Class.getEnclosingClass - api to get outer enclosing class
- Closed