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