JDK-4635044 : Invalid error when implicitly referencing enclosing type
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 1.4.0,1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,solaris_8
  • CPU: generic
  • Submitted: 2002-02-08
  • Updated: 2003-04-12
  • Resolved: 2002-09-02
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
1.4.2 mantisFixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Description
This is a correction to 4531312:

As pointed out by the Java Spec Report,
http://www.ergnosis.com/java-spec-report/java-language/jls-8.8.5.1-d.html,
it is legal to reference enclosing types in an explicit constructor invocation.
 For example, this compiles successfully with javac:

$ cat C.java
class C {
  C(Object o) {}
  private class Middle extends C {
    Middle(int i) {
      super(null);
    }
    Middle() {
      super(C.this.new Middle(1).new Inner(){});
    }
    class Inner {}
  }
}
$ javac C.java
$

However, remove the reference to C.this, and the compiler throws up its hands.

$ cat C.java
class C {
  C(Object o) {}
  private class Middle extends C {
    Middle(int i) {
      super(null);
    }
    Middle() {
      super(/*C.this.*/new Middle(1).new Inner(){}); // only this line changed
    }
    class Inner {}
  }
}
$ javac C.java
C.java:8: cannot reference this before supertype constructor has been called

      super(/*C.this.*/new Middle(1).new Inner(){}); // only this line changed
                       ^
1 error
$

This goes against JLS 15.9.2: Middle is an inner member class, the class
instance creation expression is unqualified, so bullet 3.1.2 applies: the
innermost class O which contains Middle as a member is C, so the enclosing
instance i should be C.this, and the example should compile identically to the
previous example.

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

WORK AROUND Use the explicit qualification.
11-06-2004

PUBLIC COMMENTS The compiler now correctly implements the rules for determining the correct default enclosing instance for class creation expressions (JLS2 15.9.2) and superclass constructor invocations (8.8.5.1).
10-06-2004

EVALUATION When the compiler accepts this without diagnostic, it generates code that doesn't verify. ###@###.### 2002-02-13 The test case given in the bug report is now accepted by the compiler but the generated code fails to verify. There are, I think, two bugs: first, the compiler synthesizes the incorrect outer instance when the user does't provide one (the commented code; the implicit outer instance should be C.this not Middle.this because Middle is not an inherited member of Middle by virtue of being private). Second, the anonymous class is given an outer instance but shouldn't. ###@###.### 2002-02-13 Pending resolution of JLS2 issue 4689050. If the JLS is changed as a result of that bug report, this will be closed as not a bug. However, the related verification problem has been split into the separate bug report 4689058. ###@###.### 2002-05-21 The JLS question is now resolved. See 4689050 for two regression tests that should be added to Sun's arsenal once this problem is fixed. Also, this bug should be reported against jikes, EDG, aspectj, etc. ###@###.### 2002-05-24
24-05-2002