Skip to content

Commit 8d9cb2e

Browse files
committed
8278078: Cannot reference super before supertype constructor has been called
Reviewed-by: mcimadamore
1 parent 65960f7 commit 8d9cb2e

File tree

4 files changed

+162
-1
lines changed

4 files changed

+162
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4348,7 +4348,8 @@ public void visitSelect(JCFieldAccess tree) {
43484348
if (env.info.isSelfCall &&
43494349
((sym.name == names._this &&
43504350
site.tsym == env.enclClass.sym) ||
4351-
sym.name == names._super && env.info.constructorArgs)) {
4351+
sym.name == names._super && env.info.constructorArgs &&
4352+
(sitesym.isInterface() || site.tsym == env.enclClass.sym))) {
43524353
chk.earlyRefError(tree.pos(), sym);
43534354
}
43544355
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
import java.util.function.Supplier;
3+
4+
/**
5+
* @test /nodynamiccopyright/
6+
* @bug 8278078
7+
* @summary error: cannot reference super before supertype constructor has been called
8+
* @compile/fail/ref=InvalidThisAndSuperInConstructorArgTest.out -XDrawDiagnostics InvalidThisAndSuperInConstructorArgTest.java
9+
*/
10+
public class InvalidThisAndSuperInConstructorArgTest {
11+
12+
interface InterfaceWithDefault {
13+
default String get() {
14+
return "";
15+
}
16+
}
17+
18+
InvalidThisAndSuperInConstructorArgTest(String s) {
19+
}
20+
21+
class InnerClass extends AssertionError implements InterfaceWithDefault {
22+
InnerClass() {
23+
super(InnerClass.super.toString());
24+
}
25+
InnerClass(int i) {
26+
this(InnerClass.super.toString());
27+
}
28+
InnerClass(boolean b) {
29+
super(InnerClass.this.toString());
30+
}
31+
InnerClass(double d) {
32+
this(InnerClass.this.toString());
33+
}
34+
InnerClass(float f) {
35+
super(AssertionError.super.toString());
36+
}
37+
InnerClass(char ch) {
38+
this(AssertionError.super.toString());
39+
}
40+
InnerClass(byte b) {
41+
super(AssertionError.this.toString());
42+
}
43+
InnerClass(Object o) {
44+
this(AssertionError.this.toString());
45+
}
46+
InnerClass(int[] ii) {
47+
this(InterfaceWithDefault.super.get());
48+
}
49+
InnerClass(boolean[] bb) {
50+
super(InterfaceWithDefault.this.get());
51+
}
52+
InnerClass(double[] dd) {
53+
this(InterfaceWithDefault.this.get());
54+
}
55+
InnerClass(float[] ff) {
56+
super(InterfaceWithDefault.super.get());
57+
}
58+
InnerClass(char[] chch) {
59+
this(InnerClass.this::toString);
60+
}
61+
InnerClass(String s) {
62+
super(s);
63+
}
64+
InnerClass(Supplier<String> sup) {
65+
super(sup);
66+
}
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
InvalidThisAndSuperInConstructorArgTest.java:23:29: compiler.err.cant.ref.before.ctor.called: super
2+
InvalidThisAndSuperInConstructorArgTest.java:26:28: compiler.err.cant.ref.before.ctor.called: super
3+
InvalidThisAndSuperInConstructorArgTest.java:29:29: compiler.err.cant.ref.before.ctor.called: this
4+
InvalidThisAndSuperInConstructorArgTest.java:32:28: compiler.err.cant.ref.before.ctor.called: this
5+
InvalidThisAndSuperInConstructorArgTest.java:35:33: compiler.err.not.encl.class: java.lang.AssertionError
6+
InvalidThisAndSuperInConstructorArgTest.java:38:32: compiler.err.not.encl.class: java.lang.AssertionError
7+
InvalidThisAndSuperInConstructorArgTest.java:41:33: compiler.err.not.encl.class: java.lang.AssertionError
8+
InvalidThisAndSuperInConstructorArgTest.java:44:32: compiler.err.not.encl.class: java.lang.AssertionError
9+
InvalidThisAndSuperInConstructorArgTest.java:47:38: compiler.err.cant.ref.before.ctor.called: super
10+
InvalidThisAndSuperInConstructorArgTest.java:50:39: compiler.err.not.encl.class: InvalidThisAndSuperInConstructorArgTest.InterfaceWithDefault
11+
InvalidThisAndSuperInConstructorArgTest.java:53:38: compiler.err.not.encl.class: InvalidThisAndSuperInConstructorArgTest.InterfaceWithDefault
12+
InvalidThisAndSuperInConstructorArgTest.java:56:39: compiler.err.cant.ref.before.ctor.called: super
13+
InvalidThisAndSuperInConstructorArgTest.java:59:28: compiler.err.cant.ref.before.ctor.called: this
14+
13 errors
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/**
25+
* @test
26+
* @bug 8278078
27+
* @summary error: cannot reference super before supertype constructor has been called
28+
* @compile ValidThisAndSuperInConstructorArgTest.java
29+
* @run main ValidThisAndSuperInConstructorArgTest
30+
*/
31+
public class ValidThisAndSuperInConstructorArgTest {
32+
33+
static final String SUPER = "unexpected super call";
34+
static final String THIS = "unexpected this call";
35+
36+
public String get() {
37+
return SUPER;
38+
}
39+
40+
static class StaticSubClass extends ValidThisAndSuperInConstructorArgTest {
41+
@Override
42+
public String get() {
43+
return THIS;
44+
}
45+
46+
class InnerClass extends AssertionError {
47+
InnerClass() {
48+
super(StaticSubClass.super.get());
49+
}
50+
InnerClass(int i) {
51+
this(StaticSubClass.super.get());
52+
}
53+
InnerClass(boolean b) {
54+
super(StaticSubClass.this.get());
55+
}
56+
InnerClass(double d) {
57+
this(StaticSubClass.this.get());
58+
}
59+
InnerClass(String s) {
60+
super(s);
61+
}
62+
void assertThis() {
63+
if (!THIS.equals(getMessage())) throw this;
64+
}
65+
void assertSuper() {
66+
if (!SUPER.equals(getMessage())) throw this;
67+
}
68+
}
69+
}
70+
71+
public static void main(String...args) {
72+
var test = new StaticSubClass();
73+
test.new InnerClass().assertSuper();
74+
test.new InnerClass(1).assertSuper();
75+
test.new InnerClass(true).assertThis();
76+
test.new InnerClass(1.0).assertThis();
77+
}
78+
}

0 commit comments

Comments
 (0)