1 /* 2 * @test /nodynamiccopyright/ 3 * @bug 8325805 4 * @library /tools/javac/lib 5 * @summary Permit non-superclass instance field assignments before this/super in constructors 6 * @modules jdk.compiler/com.sun.tools.javac.tree 7 * jdk.compiler/com.sun.tools.javac.util 8 * @enablePreview 9 * @compile/fail/ref=EarlyAssignments.out -XDrawDiagnostics EarlyAssignments.java 10 */ 11 public class EarlyAssignments { 12 13 public static class Inner1 { 14 public int x; 15 16 public Inner1() { 17 x = 123; // OK - "x" belongs to this class 18 this.x = 123; // OK - "x" belongs to this class 19 Inner1.this.x = 123; // OK - "x" belongs to this class 20 super(); 21 } 22 23 public Inner1(int y) { 24 y = x; // OK - "x" belongs to this class 25 y = this.x; // OK - "x" belongs to this class 26 y = Inner1.this.x; // OK - "x" belongs to this class 27 super(); 28 } 29 30 public class Inner1a extends Inner1 { 31 public int z; 32 public Inner1a(byte value) { 33 Inner1.this.x = value; // OK - "x" belongs to outer class 34 z = super.x; // FAIL - "x" belongs to superclass 35 z = x; // FAIL - "x" belongs to superclass 36 this.z = x; // FAIL - "x" belongs to superclass 37 Inner1a.this.z = x; // FAIL - "x" belongs to superclass 38 Object o1 = Inner1.this; // OK - Inner1 is an outer class 39 Object o2 = Inner1a.this; // FAIL - Inner1a is this class 40 super(); 41 } 42 public Inner1a(short value) { 43 x = value; // FAIL - "x" belongs to superclass 44 super(); 45 } 46 public Inner1a(char value) { 47 this.x = value; // FAIL - "x" belongs to superclass 48 super(); 49 } 50 public Inner1a(int value) { 51 super.x = value; // FAIL - "x" belongs to superclass 52 super(); 53 } 54 } 55 56 public class Inner1b { 57 public Inner1b(int value) { 58 Inner1.this.x = value; // OK - "x" belongs to outer class 59 super(); 60 } 61 } 62 } 63 64 public static class Inner2 extends Inner1 { 65 int y; 66 public Inner2(int value) { 67 y = value; // OK - "y" belongs to this class 68 this.y = value; // OK - "y" belongs to this class 69 x = value; // FAIL - "x" belongs to superclass 70 this.x = value; // FAIL - "x" belongs to superclass 71 Object o1 = this; // FAIL - can't acces 'this' yet 72 Object o2 = Inner2.this; // FAIL - can't acces 'this' yet 73 super(); 74 } 75 } 76 77 public static class Inner3 { 78 79 public int e; 80 81 public class Inner3a { 82 83 public static int x; 84 85 public Inner3a(int val) { 86 x = val; // OK - "x" is a static field 87 val = x; // OK - "x" is a static field 88 e = val; // OK - "e" belongs to outer class 89 val = e; // OK - "e" belongs to outer class 90 Inner3.this.e = val; // OK - "e" belongs to outer class 91 super(); 92 } 93 } 94 } 95 96 public static class Inner4 { 97 public int x; 98 99 public Inner4() { 100 x = 0; // OK 101 x = x + 1; // OK 102 super(); 103 } 104 105 public Inner4(int a) { 106 this.x = 0; // OK 107 this.x = this.x + 1; // OK 108 super(); 109 } 110 111 public Inner4(char a) { 112 Inner4.this.x = 0; // OK 113 Inner4.this.x = Inner4.this.x + 1; // OK 114 super(); 115 } 116 } 117 118 public static class Inner5 extends Inner4 { 119 public int y; 120 121 public Inner5() { 122 y = x + 1; // FAIL - illegal early access 123 super(); 124 } 125 126 public Inner5(int a) { 127 this.y = x + 1; // FAIL - illegal early access 128 super(); 129 } 130 131 public Inner5(char a) { 132 Inner5.this.y = x + 1; // FAIL - illegal early access 133 super(); 134 } 135 136 public Inner5(short a) { 137 y = super.x + 1; // FAIL - illegal early access 138 super(); 139 } 140 141 public Inner5(float a) { 142 y = Inner5.this.x + 1; // FAIL - illegal early access 143 super(); 144 } 145 } 146 147 public static class Inner6 { 148 public int x = 1; 149 150 public Inner6() { 151 x = 2; // FAIL - illegal early access 152 super(); 153 } 154 } 155 156 public static class Inner7 { 157 public final int x = 1; 158 159 public Inner7() { 160 x = 2; // FAIL - illegal early access 161 super(); 162 } 163 } 164 165 public static class Inner8 { 166 class Inner8a { 167 int x; 168 } 169 170 public Inner8() { 171 this.new Inner8a().x = 1; // FAIL - illegal early access 172 super(); 173 } 174 } 175 176 public static class Inner9 { 177 int x = 1; 178 int y; 179 Inner9() { 180 y = x; // FAIL, x has an initializer; no warning mode diagnostic, as this would be ok for a strict field 181 super(); 182 } 183 } 184 185 public static class Inner10 { 186 int x = 1; 187 int y = x + 1; // no warning expected here 188 } 189 } --- EOF ---