Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4986031

(reflect) Class.getEnclosingMethod() does not work

XMLWordPrintable



      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]

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

            darcy Joe Darcy
            rslasunw Rsla Rsla (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: