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