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

Compiler fails to resolve appropriate type for outer member

    Details

    • Subcomponent:
    • Introduced In Build:
      5.0
    • Introduced In Version:
      5.0
    • Resolved In Build:
      b73
    • CPU:
      generic
    • OS:
      solaris_8
    • Verification:
      Verified

      Description

      I wrote the following little program to understand how innerclass emulation
      would perform in presence of generics,
      especially how name lookup would resolve access to enclosing field which is
      generic.
      The offending code is located at #3 (see below).

      Inside the anonymous #2, the reference to 't' can only be bound through its
      enclosing type: anonymous#1, which inherits
      it from X<X>, but unfortunately it is private there, and thus isn't visible
      (the type of t in this context is X).

      Thus, it has to find it in a further enclosing scope, the one of X<T>,
      where t is of type T.
      Thus I would expect a compiler error when trying to assign t to x (of type
      X), as T cannot be assigned to X.

      Curiously, javac tolerates this code, but as expected when running it a
      ClassCastException occurs since
      t is actually holding a String (from new X<String>("OUTER")).
      I believe javac did not enforce type safety, and should have rejected this
      code as invalid.

      I wonder if your new spec is covering cases about innerclass emulation and
      generics, as they can complexify quite
      a bit the semantics...


      public class X <T> {
         private T t;
         X(T t) {
             this.t = t;
         }
         public static void main(String[] args) {
             new X<String>("OUTER").bar();
         }
         void bar() {
             new X<X>(this) { // #1
                 void run() {
                     new Object() { // #2
                         void run() {
                             X x = t; // #3 <--- which t is bound ?
                         System.out.println(x);
                         }
                     }.run();
                 }
             }.run();
         }
      }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                ahe Peter Ahe
                Reporter:
                gafter Neal Gafter
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: