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) {
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 * @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) {
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 }
|