1 /*
2 * @test /nodynamiccopyright/
3 * @bug 8194743
4 * @summary Permit additional statements before this/super in constructors
5 * @compile/fail/ref=SuperInitFails.out -XDrawDiagnostics SuperInitFails.java
6 */
7 import java.util.concurrent.atomic.AtomicReference;
8 public class SuperInitFails extends AtomicReference<Object> implements Iterable<Object> {
9
10 private int x;
11
12 /// GOOD EXAMPLES
13
14 public SuperInitFails() { // this should be OK
15 }
16
17 public SuperInitFails(Object x) {
18 this.x = x.hashCode(); // this should be OK
19 }
20
21 public SuperInitFails(byte x) {
22 super(); // this should be OK
23 }
24
134 }
135
136 record Record2(int value) {
137 Record2(float x) { // this should FAIL
138 super();
139 }
140 }
141
142 @Override
143 public java.util.Iterator<Object> iterator() {
144 return null;
145 }
146
147 public SuperInitFails(float[][] x) {
148 Runnable r = () -> {
149 super(); // this should FAIL
150 };
151 }
152
153 public SuperInitFails(int[][] z) {
154 super((Runnable)() -> x); // this should FAIL
155 }
156
157 public SuperInitFails(long[][] z) {
158 super(new Inner1()); // this should FAIL
159 }
160
161 public static class Inner2 {
162 int x;
163 }
164 public static class Inner3 extends Inner2 {
165 int y;
166 Inner3(byte z) {
167 x = z; // this should FAIL
168 super();
169 }
170 Inner3(short z) {
171 this.x = z; // this should FAIL
172 super();
173 }
174 Inner3(char z) {
175 Inner3.this.x = z; // this should FAIL
176 super();
177 }
178 Inner3(int z) {
179 super.x = z; // this should FAIL
180 super();
181 }
182 }
183
184 public SuperInitFails(double[][] x) {
185 Runnable r = () -> this.x = 7; // this should FAIL
186 super();
187 }
188
189 public static class Inner4 {
190 Inner4() {
191 Runnable r = () -> {
192 class A {
193 A() {
194 return; // this should FAIL
195 super();
196 }
197 A(int x) {
198 {
199 this(); // this should FAIL
200 }
201 }
202 A(char x) {
203 super();
204 this(); // this should FAIL
205 }
206 }
207 };
208 super();
209 };
210 }
211 }
|
1 /*
2 * @test /nodynamiccopyright/
3 * @bug 8194743
4 * @library /tools/javac/lib
5 * @summary Permit additional statements before this/super in constructors
6 * @modules jdk.compiler/com.sun.tools.javac.tree
7 * jdk.compiler/com.sun.tools.javac.util
8 * @enablePreview
9 * @compile/fail/ref=SuperInitFails.out -XDrawDiagnostics SuperInitFails.java
10 */
11 import java.util.concurrent.atomic.AtomicReference;
12 public class SuperInitFails extends AtomicReference<Object> implements Iterable<Object> {
13
14 private int x;
15
16 /// GOOD EXAMPLES
17
18 public SuperInitFails() { // this should be OK
19 }
20
21 public SuperInitFails(Object x) {
22 this.x = x.hashCode(); // this should be OK
23 }
24
25 public SuperInitFails(byte x) {
26 super(); // this should be OK
27 }
28
138 }
139
140 record Record2(int value) {
141 Record2(float x) { // this should FAIL
142 super();
143 }
144 }
145
146 @Override
147 public java.util.Iterator<Object> iterator() {
148 return null;
149 }
150
151 public SuperInitFails(float[][] x) {
152 Runnable r = () -> {
153 super(); // this should FAIL
154 };
155 }
156
157 public SuperInitFails(int[][] z) {
158 super((Runnable)() -> System.err.println(x)); // this should FAIL
159 }
160
161 public SuperInitFails(long[][] z) {
162 super(new Inner1()); // this should FAIL
163 }
164
165 public static class Inner2 {
166 int x;
167 }
168 public static class Inner3 extends Inner2 {
169 int y;
170 Inner3(byte z) {
171 x = z; // this should FAIL
172 super();
173 }
174 Inner3(short z) {
175 this.x = z; // this should FAIL
176 super();
177 }
178 Inner3(char z) {
179 Inner3.this.x = z; // this should FAIL
180 super();
181 }
182 Inner3(int z) {
183 super.x = z; // this should FAIL
184 super();
185 }
186 }
187
188 public SuperInitFails(double[][] x) {
189 Runnable r = () -> this.x = 7; // this should FAIL
190 super();
191 }
192
193 public int xx;
194
195 SuperInitFails(short[][] ignore) {
196 int i = new SuperInitFails(){
197 void foo() {
198 System.err.println(xx); // this one is OK, reading field `xx` in the anonymous class
199 }
200 }.xx; // this one is OK too, field of a fully constructed class
201 super(null);
202 }
203
204 public static class Inner4 {
205 Inner4() {
206 Runnable r = () -> {
207 class A {
208 A() {
209 return; // this should FAIL
210 super();
211 }
212 A(int x) {
213 {
214 this(); // this should FAIL
215 }
216 }
217 A(char x) {
218 super();
219 this(); // this should FAIL
220 }
221 }
222 };
223 super();
224 };
225 }
226
227 static class Inner5 {
228 int x = 4;
229 static String m1(Runnable r) { return null; }
230 static String m2(Object r) { return null; }
231 Inner5() {
232 m1(() -> System.out.println(x)).toString();
233 m2(x).toString();
234 super();
235 }
236 }
237
238 static class Inner6 {
239 Inner6() {
240 class Bar {
241 Bar() {
242 Object o = Bar.this;
243 super();
244 }
245 }
246 super();
247 }
248 }
249
250 static class Inner7 {
251 private int x;
252
253 public Inner7(byte y) {
254 x = y;
255 this((int)y);
256 }
257 public Inner7(int x) {
258 this.x = x;
259 super();
260 }
261 }
262
263 static class Inner8 {
264 final int x;
265
266 Inner8() {
267 this(x = 3); // error
268 }
269 Inner8(int i) {
270 x = 4;
271 }
272 }
273
274 static class Inner9 {
275 interface Parent {
276 boolean check = true;
277 }
278
279 class Medium implements Parent {}
280
281 class Inner9Test extends Medium {
282 Inner9Test() {
283 boolean check1 = Inner9Test.super.check;
284 boolean check2 = super.check;
285 super();
286 }
287 }
288 }
289
290 static class Inner10 {
291 static boolean testMethod() { return true; }
292 Inner10() {}
293 Inner10(int a) {
294 Inner10.this.testMethod();
295 this();
296 }
297 }
298
299 static class Inner11 {
300 class Inner11_1 {
301 static <T> void m() {}
302 }
303
304 class Inner11_2 extends Inner11_1 {
305 Inner11_2() {
306 Inner11_1.super.<String>m();
307 super();
308 }
309 }
310 }
311
312 static class Inner12 {
313 static final boolean check = true;
314 Inner12() {}
315 Inner12(int a) {
316 boolean b = Inner12.this.check; //compilation error expected here
317 this();
318 }
319 }
320
321 static class Inner13 {
322 static final boolean check = true;
323 class Inner13_1 {
324 Inner13_1(int a) {
325 boolean b = Inner13.this.check;
326 this();
327 }
328 Inner13_1() {}
329 }
330 }
331 }
|