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  * @enablePreview
  7  */
  8 import java.util.concurrent.atomic.AtomicReference;
  9 public class SuperInitFails extends AtomicReference<Object> implements Iterable<Object> {
 10 
 11     private int x;
 12 
 13 /// GOOD EXAMPLES
 14 
 15     public SuperInitFails() {           // this should be OK
 16     }
 17 
 18     public SuperInitFails(Object x) {
 19         this.x = x.hashCode();          // this should be OK
 20     }
 21 
 22     public SuperInitFails(byte x) {
 23         super();                        // this should be OK
 24     }
 25 
 26     public SuperInitFails(char x) {
 27         this((int)x);                   // this should be OK
 28     }
 29 
 30 /// FAIL EXAMPLES
 31 
 32     {
 33         this(1);                        // this should FAIL
 34     }
 35 
 36     {
 37         super();                        // this should FAIL
 38     }
 39 
 40     void normalMethod1() {
 41         super();                        // this should FAIL
 42     }
 43 
 44     void normalMethod2() {
 45         this();                         // this should FAIL
 46     }
 47 
 48     void normalMethod3() {
 49         Runnable r = () -> super();     // this should FAIL
 50     }
 51 
 52     void normalMethod4() {
 53         Runnable r = () -> this();      // this should FAIL
 54     }
 55 
 56     public SuperInitFails(short x) {
 57         hashCode();                     // this should FAIL
 58         super();
 59     }
 60 
 61     public SuperInitFails(float x) {
 62         this.hashCode();                // this should FAIL
 63         super();
 64     }
 65 
 66     public SuperInitFails(int x) {
 67         super.hashCode();               // this should FAIL
 68         super();
 69     }
 70 
 71     public SuperInitFails(long x) {
 72         SuperInitFails.this.hashCode();      // this should FAIL
 73         super();
 74     }
 75 
 76     public SuperInitFails(double x) {
 77         SuperInitFails.super.hashCode();     // this should FAIL
 78         super();
 79     }
 80 
 81     public SuperInitFails(byte[] x) {
 82         {
 83             super();                    // this should FAIL
 84         }
 85     }
 86 
 87     public SuperInitFails(char[] x) {
 88         if (x.length == 0)
 89             return;                     // this should FAIL
 90         super();
 91     }
 92 
 93     public SuperInitFails(short[] x) {
 94         this.x++;                       // this should FAIL
 95         super();
 96     }
 97 
 98     public SuperInitFails(float[] x) {
 99         System.identityHashCode(this);  // this should FAIL
100         super();
101     }
102 
103     public SuperInitFails(int[] x) {
104         this(this);                     // this should FAIL
105     }
106 
107     public SuperInitFails(long[] x) {
108         this(Object.this);              // this should FAIL
109     }
110 
111     public SuperInitFails(double[] x) {
112         Iterable.super.spliterator();   // this should FAIL
113         super();
114     }
115 
116     public SuperInitFails(byte[][] x) {
117         super(new Object() {
118             {
119                 super();                // this should FAIL
120             }
121         });
122     }
123 
124     public SuperInitFails(char[][] x) {
125         new Inner1();                   // this should FAIL
126         super();
127     }
128 
129     class Inner1 {
130     }
131 
132     record Record1(int value) {
133         Record1(float x) {              // this should FAIL
134         }
135     }
136 
137     record Record2(int value) {
138         Record2(float x) {              // this should FAIL
139             super();
140         }
141     }
142 
143     @Override
144     public java.util.Iterator<Object> iterator() {
145         return null;
146     }
147 
148     public SuperInitFails(float[][] x) {
149         Runnable r = () -> {
150             super();                    // this should FAIL
151         };
152     }
153 
154     public SuperInitFails(int[][] z) {
155         super((Runnable)() -> System.err.println(x));       // this should FAIL
156     }
157 
158     public SuperInitFails(long[][] z) {
159         super(new Inner1());            // this should FAIL
160     }
161 
162     public static class Inner2 {
163         int x;
164     }
165     public static class Inner3 extends Inner2 {
166         int y;
167         Inner3(byte z) {
168             x = z;                      // this should FAIL
169             super();
170         }
171         Inner3(short z) {
172             this.x = z;                 // this should FAIL
173             super();
174         }
175         Inner3(char z) {
176             Inner3.this.x = z;          // this should FAIL
177             super();
178         }
179         Inner3(int z) {
180             super.x = z;                // this should FAIL
181             super();
182         }
183     }
184 
185     public SuperInitFails(double[][] x) {
186         Runnable r = () -> this.x = 7;  // this should FAIL
187         super();
188     }
189 
190     public int xx;
191 
192     SuperInitFails(short[][] ignore) {
193         int i = new SuperInitFails(){
194             void foo() {
195                 System.err.println(xx);  // this should fail
196             }
197         }.xx;  // this one is OK though
198         super(null);
199     }
200 
201     public static class Inner4 {
202         Inner4() {
203             Runnable r = () -> {
204                 class A {
205                     A() {
206                         return;         // this should FAIL
207                         super();
208                     }
209                     A(int x) {
210                         {
211                             this();     // this should FAIL
212                         }
213                     }
214                     A(char x) {
215                         super();
216                         this();         // this should FAIL
217                     }
218                 }
219             };
220             super();
221         };
222     }
223 
224     static class Inner5 {
225         int x = 4;
226         static String m1(Runnable r) { return null; }
227         static String m2(Object r) { return null; }
228         Inner5() {
229             m1(() -> System.out.println(x)).toString();
230             m2(x).toString();
231             super();
232         }
233     }
234 
235     static class Inner6 {
236         Inner6() {
237             class Bar {
238                 Bar() {
239                     Object o = Bar.this;
240                     super();
241                 }
242             }
243             super();
244         }
245     }
246 }