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 
 29     public SuperInitFails(char x) {
 30         this((int)x);                   // this should be OK
 31     }
 32 
 33 /// FAIL EXAMPLES
 34 
 35     {
 36         this(1);                        // this should FAIL
 37     }
 38 
 39     {
 40         super();                        // this should FAIL
 41     }
 42 
 43     void normalMethod1() {
 44         super();                        // this should FAIL
 45     }
 46 
 47     void normalMethod2() {
 48         this();                         // this should FAIL
 49     }
 50 
 51     void normalMethod3() {
 52         Runnable r = () -> super();     // this should FAIL
 53     }
 54 
 55     void normalMethod4() {
 56         Runnable r = () -> this();      // this should FAIL
 57     }
 58 
 59     public SuperInitFails(short x) {
 60         hashCode();                     // this should FAIL
 61         super();
 62     }
 63 
 64     public SuperInitFails(float x) {
 65         this.hashCode();                // this should FAIL
 66         super();
 67     }
 68 
 69     public SuperInitFails(int x) {
 70         super.hashCode();               // this should FAIL
 71         super();
 72     }
 73 
 74     public SuperInitFails(long x) {
 75         SuperInitFails.this.hashCode();      // this should FAIL
 76         super();
 77     }
 78 
 79     public SuperInitFails(double x) {
 80         SuperInitFails.super.hashCode();     // this should FAIL
 81         super();
 82     }
 83 
 84     public SuperInitFails(byte[] x) {
 85         {
 86             super();                    // this should FAIL
 87         }
 88     }
 89 
 90     public SuperInitFails(char[] x) {
 91         if (x.length == 0)
 92             return;                     // this should FAIL
 93         super();
 94     }
 95 
 96     public SuperInitFails(short[] x) {
 97         this.x++;                       // this should FAIL
 98         super();
 99     }
100 
101     public SuperInitFails(float[] x) {
102         System.identityHashCode(this);  // this should FAIL
103         super();
104     }
105 
106     public SuperInitFails(int[] x) {
107         this(this);                     // this should FAIL
108     }
109 
110     public SuperInitFails(long[] x) {
111         this(Object.this);              // this should FAIL
112     }
113 
114     public SuperInitFails(double[] x) {
115         Iterable.super.spliterator();   // this should FAIL
116         super();
117     }
118 
119     public SuperInitFails(byte[][] x) {
120         super(new Object() {
121             {
122                 super();                // this should FAIL
123             }
124         });
125     }
126 
127     public SuperInitFails(char[][] x) {
128         new Inner1();                   // this should FAIL
129         super();
130     }
131 
132     class Inner1 {
133     }
134 
135     record Record1(int value) {
136         Record1(float x) {              // this should FAIL
137         }
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 }