JDK-4892994 : (reflect) Class.getEnclosingClass - api to get outer enclosing class
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-07-18
  • Updated: 2012-09-28
  • Resolved: 2003-07-22
Related Reports
Relates :  
Description

Name: gm110360			Date: 07/18/2003


A DESCRIPTION OF THE REQUEST :
There is no clean way to determine the enclosing classes of an inner class.
When reflectively creating a class (that might be inner) you would have to determine if the class is inner and if it is, is it static, then if the class is inner and not static you must construct the class passing in the outclass as a parameter. You must know where to pass it in (in the front or in the back).

JUSTIFICATION :
Lets say you create an action wrapper that takes an action and regesters it so it can latter create a button, menu Item, ect... Multiple instances of the action may be necessary and creating it in different ways can be helpful on a per-situation basis. The action may come as part of an inner class or an outer class, there needs to be an easy way to determine if it is an inner class and if it is create it with the appropiate outer class pointer.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
new method in java.lang.Class:
public Class getEnclosingClass()

returns the Enclosing Class of this class or null if it is the outest most class.

new method in java.lang.reflect.Constructor:
public Object newInstance(Object enclosingClass,Object[] parameters)

returns a new instance of an inner class for the enclosing class.
ACTUAL -
These methods do not exits.

CUSTOMER SUBMITTED WORKAROUND :
A user can "guess" the enclosing class by:
String name = class.getName();
Class enclosingClass = Class.forName(name.substring(name.lastIndexOf("$")));
(Incident Review ID: 186775) 
======================================================================

Comments
EVALUATION An api to get the outer enclosing class as described already exists. Class.getDeclaringClass which returns the Class object representing the class in which it is declared. If the class is not a nested class, null is returned. The evaluation of rfe 4869001 for use of Class.getDeclaringClass() with anonymous classes. -- iag@sfbay 2003-07-22
22-07-2003