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 = x.length; // 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(short[][] x) { 149 class Foo { 150 Foo() { 151 SuperInitFails.this.hashCode(); 152 } 153 }; 154 new Foo(); // this should FAIL 155 super(); 156 } 157 158 public SuperInitFails(float[][] x) { 159 Runnable r = () -> { 160 super(); // this should FAIL 161 }; 162 } 163 164 public SuperInitFails(int[][] z) { 165 super((Runnable)() -> x); // this should FAIL 166 } 167 168 public SuperInitFails(long[][] z) { 169 super(new Inner1()); // this should FAIL 170 } 171 }