< prev index next >

test/langtools/tools/javac/SuperInit/SuperInitFails.java

Print this page

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

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 }
< prev index next >