United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4635044 : Invalid error when implicitly referencing enclosing type

Details
Type:
Bug
Submit Date:
2002-02-08
Status:
Closed
Updated Date:
2003-04-12
Project Name:
JDK
Resolved Date:
2002-09-02
Component:
tools
OS:
solaris_8,generic
Sub-Component:
javac
CPU:
generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.4.0,1.4.2
Fixed Versions:
1.4.2 (mantis)

Related Reports
Duplicate:
Duplicate:
Duplicate:
Relates:

Sub Tasks

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


                                     
2004-06-14
WORK AROUND

Use the explicit qualification.
                                     
2004-06-11
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).
                                     
2004-06-10
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
                                     
2002-05-24



Hardware and Software, Engineered to Work Together