Skip to content

Commit b2ff166

Browse files
committed
[24] Local instantiation in static context allowed by ECJ
Similar checks and reporting should be done in ExplicitConstructorCall as well.
1 parent ec9a8cc commit b2ff166

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/ExplicitConstructorCall.java

+8
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,14 @@ public void resolve(BlockScope scope) {
378378
&& receiverType.erasure().id == TypeIds.T_JavaLangEnum) {
379379
scope.problemReporter().cannotInvokeSuperConstructorInEnum(this, methodScope.referenceMethod().binding);
380380
}
381+
if (!receiverType.isEnum() &&
382+
this.accessMode <= ExplicitConstructorCall.Super &&
383+
receiverType instanceof LocalTypeBinding local) {
384+
MethodScope allocationStaticEnclosing = scope.parent.nearestEnclosingStaticScope(); // Constructor scope already has static, start from parent scope
385+
MethodScope typesEnclosingStaticScope = local.scope.nearestEnclosingStaticScope();
386+
if (allocationStaticEnclosing != null && typesEnclosingStaticScope != null && allocationStaticEnclosing != typesEnclosingStaticScope)
387+
scope.problemReporter().allocationInStaticContext(this, local);
388+
}
381389
// qualification should be from the type of the enclosingType
382390
if (this.qualification != null) {
383391
if (this.accessMode != ExplicitConstructorCall.Super) {

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SuperAfterStatementsTest.java

+103
Original file line numberDiff line numberDiff line change
@@ -3220,4 +3220,107 @@ public class X {
32203220
----------
32213221
""");
32223222
}
3223+
public void testGH3687c() {
3224+
runNegativeTest(new String[] {
3225+
"X.java",
3226+
"""
3227+
import java.util.function.IntSupplier;
3228+
@SuppressWarnings("unused")
3229+
public class X {
3230+
public static void main(String argv[]) {
3231+
class Parent {
3232+
int value;
3233+
Parent(int i) {
3234+
this.value = i;
3235+
}
3236+
}
3237+
class Outer {
3238+
static IntSupplier supplier = () -> {
3239+
class InnerLocal extends Parent {
3240+
InnerLocal() {
3241+
super(10);
3242+
}
3243+
}
3244+
return new InnerLocal().value;
3245+
};
3246+
}
3247+
System.out.println(Outer.supplier.getAsInt());
3248+
}
3249+
}"""
3250+
},
3251+
"----------\n" +
3252+
"1. ERROR in X.java (at line 15)\n" +
3253+
" super(10);\n" +
3254+
" ^^^^^^^^^^\n" +
3255+
"Cannot instantiate local class \'Parent\' in a static context\n" +
3256+
"----------\n");
3257+
}
3258+
public void testGH3687d() {
3259+
runNegativeTest(new String[] {
3260+
"X.java",
3261+
"""
3262+
import java.util.function.IntSupplier;
3263+
import java.util.function.Predicate;
3264+
@SuppressWarnings("unused")
3265+
public class X {
3266+
public static void main(String argv[]) {
3267+
Predicate<Integer> condition = (Integer param) -> {
3268+
class Parent {
3269+
int value;
3270+
Parent(int i) {
3271+
value = i;
3272+
}
3273+
}
3274+
class Outer {
3275+
static {
3276+
class Inner extends Parent {
3277+
Inner(int i) {
3278+
super(i);
3279+
}
3280+
}
3281+
}
3282+
}
3283+
return param <= 10;
3284+
};
3285+
System.out.println(condition.test(10));
3286+
}
3287+
}"""
3288+
},
3289+
"----------\n" +
3290+
"1. ERROR in X.java (at line 17)\n" +
3291+
" super(i);\n" +
3292+
" ^^^^^^^^^\n" +
3293+
"Cannot instantiate local class \'Parent\' in a static context\n" +
3294+
"----------\n");
3295+
}
3296+
public void testGH3687e() {
3297+
runNegativeTest(new String[] {
3298+
"X.java",
3299+
"""
3300+
import java.io.PrintStream;
3301+
import java.util.function.Predicate;
3302+
@SuppressWarnings("unused")
3303+
public class X {
3304+
static {
3305+
class SuperClass {}
3306+
class Outer {
3307+
static int a;
3308+
static Predicate<Object> test = (o) -> new SuperClass() {
3309+
public boolean test() {
3310+
return true;
3311+
}
3312+
}.test();
3313+
}
3314+
}
3315+
public static void main(String argv[]) {
3316+
}
3317+
}"""
3318+
},
3319+
"----------\n" +
3320+
"1. ERROR in X.java (at line 9)\n" +
3321+
" static Predicate<Object> test = (o) -> new SuperClass() {\n" +
3322+
" ^^^^^^^^^^^^\n" +
3323+
"Cannot instantiate local class \'SuperClass\' in a static context\n" +
3324+
"----------\n");
3325+
}
32233326
}

0 commit comments

Comments
 (0)