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