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

(reflect) getEnclosingConstructor: spec contradiction, incorrect behavior

    XMLWordPrintable

Details

    • b40
    • generic
    • generic
    • Verified

    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]

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

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: