JDK-4986031 : (reflect) Class.getEnclosingMethod() does not work
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2004-01-30
  • Updated: 2012-09-28
  • Resolved: 2004-01-30
Related Reports
Duplicate :  
Relates :  
Description

Name: vrR10176			Date: 01/30/2004



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

"public Method getEnclosingMethod()

If this Class object represents a local or anonymous class within a method, 
returns a Method object representing the immediately enclosing method of 
the underlying class. Returns null otherwise. 
..."

But if this Class object represents a local or anonymous class within a method
the method getEnclosingMethod() returns null.

To reproduce the issue execute the following test.

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

public class test {

    public static void main(String argv[]) {

        class Local {}
        Class cl = (new Local()).getClass();
        Method m = cl.getEnclosingMethod();
        System.out.println("Enclosing method for local class: " + m);

        m = (new Object () { Method getEncMthd(){return this.getClass().getEnclosingMethod();}}).getEncMthd();
        System.out.println("Enclosing method for anonymous class: " + m);
    }
}

------------ 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 method for local class: null
Enclosing method for anonymous class: null
$
$
-------------------------------------------------------------------

New jck1.5-beta2 tests failed due to this bug:
api/java_lang/Class/index.html#GetEnclosingMethod[getEnclosingMethod003]
api/java_lang/Class/index.html#GetEnclosingMethod[getEnclosingMethod004]

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

Comments
EVALUATION The submitter is correct; dup of 4962341. ###@###.### 2004-01-30
30-01-2004