Draft Spec for Second Preview of Flexible Constructor Bodies (JEP 482)

Stephan Herrmann stephan.herrmann at berlin.de
Tue Jun 11 17:43:22 UTC 2024


Am 11.06.24 um 19:10 schrieb Maurizio Cimadamore:
> On 11/06/2024 17:17, Stephan Herrmann wrote:
> 
>> I'm sorry, I didn't look at the specific complications. This particular one is 
>> wrongly accepted also by ecj. IOW there is some confusion indeed regarding how 
>> far the "staticness" propagates into nested structures. 
> 
> Ok. I tried this example:
> 
> |$ cat Foo.java class Test { interface Bar { } static class Foo { Foo(Bar bar) { 
> } } int x; class Inner extends Foo { Inner() { super(new Bar() { { 
> Test.this.x++; } // allowed??? }); } } } |
> 
> |java -jar org.eclipse.jdt.core.compiler.batch_3.38.0.v20240524-2033.jar 
> Foo.java ---------- 1. ERROR in /w/lt/jdk/dev/Foo.java (at line 11) { 
> Test.this.x++; } // allowed??? ^^^^^^^^^ No enclosing instance of the type Test 
> is accessible in scope ---------- 1 problem (1 error) |
> 
> This seems to suggest that the “outside JLS” behavior is not supported by ecj?

With this invocation you implicitly compiled for compliance 1.4 (which will 
probably be dropped soon, at last).

At 1.5+ ecj accepts.

Frankly, that change was before my time, but perhaps this code comment serves 
for entertainment:

	// From 1.5 on, provide access to enclosing instance synthetic constructor 
argument when declared inside constructor call
	// only for direct anonymous type
	//public class X {
	//	void foo() {}
	//	class M {
	//		M(Object o) {}
	//		M() { this(new Object() { void baz() { foo(); }}); } // access to #foo() 
indirects through constructor synthetic arg: val$this$0
	//	}
	//}


Stephan


More information about the amber-spec-experts mailing list