JDK-4986032 : (reflect) getEnclosingConstructor: spec contradiction, incorrect behavior
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2004-01-30
  • Updated: 2017-05-16
  • Resolved: 2004-02-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
5.0 b40Fixed
Related Reports
Relates :  
Relates :  
Description

Name: vrR10176			Date: 01/30/2004



The API spec (JDK1.5-b36) for method Class.getEnclosingConstructor() says:

"public Method getEnclosingConstructor()
        ~~~~~~

If this Class object represents a local or anonymous class within a constructor, 
returns a Constructor object representing the immediately enclosing constructor 
          ~~~~~~~~~~~
of the underlying class. Returns null otherwise. In particular, this method returns 
null if the underlying class is a local or anonymous class immediately enclosed by 
a type declaration, instance initializer or static initializer

Returns:
the immediately enclosing method of the underlying class, if that class is a local 
                          ~~~~~~
or anonymous class; otherwise null."

The description of the method is contradictory, it is not clear about method return 
type. Constructor is more consistent type for this case, but javac (JDK1.5-b36)
accepts Method.

Also if this Class object represents a local or anonymous class within a constructor
the method getEnclosingConstructor() returns null.

To reproduce the issue execute the following test.

------------ test.java -------------------------------
import java.lang.reflect.Method;
//import java.lang.reflect.Constructor;

public class test {

    public test() {

        class Local {}
        Class cl = (new Local()).getClass();
        Method m = cl.getEnclosingConstructor();
//        Constructor m = cl.getEnclosingConstructor(); //???
        System.out.println("Enclosing constructor for local class: " + m);

        m = (new Object () { Method getEncMthd(){return this.getClass().getEnclosingConstructor();}}).getEncMthd();
//        m = (new Object () { Constructor getEncMthd(){return this.getClass().getEnclosingConstructor();}}).getEncMthd(); //???
        System.out.println("Enclosing constructor for anonymous class: " + m);

    }

    public static void main(String argv[]) {
        test obj = new test();
        return;
    }
}

------------ Logs ---------------------------------------------
$javac -d . test.java
$
$java -version
java version "1.5.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b36)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b36, mixed mode)
$
$java test
Enclosing constructor for local class: null
Enclosing constructor for anonymous class: null
$
$
-------------------------------------------------------------------

New jck1.5-beta2 tests failed due to this bug:
api/java_lang/Class/index.html#GetEnclosingCnstr[getEnclosingConstructor003]
api/java_lang/Class/index.html#GetEnclosingCnstr[getEnclosingConstructor004]

======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-beta2 FIXED IN: tiger-beta2 INTEGRATED IN: tiger-b40 tiger-beta2 VERIFIED IN: tiger-beta2
14-06-2004

SUGGESTED FIX src/share/classes/java/lang>sccs sccsdiff -r1.167 -r1.168 Class.java ------- Class.java ------- 855c855 < * @return the immediately enclosing method of the underlying class, if --- > * @return the immediately enclosing constructor of the underlying class, if 859c859 < public Method getEnclosingConstructor() { --- > public Constructor getEnclosingConstructor() { ###@###.### 2004-02-06
06-02-2004

EVALUATION Yes, the return type of the getEnclosingConstructor method is incorrect. Like getEnclosingMethod, this method doesn't yet have a working implementation; see 4962341. Will file a ccc request to correct the signature. ###@###.### 2004-01-30
30-01-2004