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) {
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();
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 }
|
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 * @enablePreview
7 */
8 public class EarlyAssignments {
9
10 public static class Inner1 {
11 public int x;
12
13 public Inner1() {
14 x = 123; // OK - "x" belongs to this class
15 this.x = 123; // OK - "x" belongs to this class
16 Inner1.this.x = 123; // OK - "x" belongs to this class
17 super();
18 }
19
20 public Inner1(int y) {
21 y = x; // OK - "x" belongs to this class
22 y = this.x; // OK - "x" belongs to this class
23 y = Inner1.this.x; // OK - "x" belongs to this class
24 super();
25 }
26
27 public class Inner1a extends Inner1 {
28 public int z;
29 public Inner1a(byte value) {
30 Inner1.this.x = value; // OK - "x" belongs to outer class
31 z = super.x; // FAIL - "x" belongs to superclass
32 z = x; // FAIL - "x" belongs to superclass
33 this.z = x; // FAIL - "x" belongs to superclass
34 Inner1a.this.z = x; // FAIL - "x" belongs to superclass
35 Object o1 = Inner1.this; // OK - Inner1 is an outer class
36 Object o2 = Inner1a.this; // FAIL - Inner1a is this class
37 super();
38 }
39 public Inner1a(short value) {
40 x = value; // FAIL - "x" belongs to superclass
41 super();
42 }
43 public Inner1a(char value) {
78 public class Inner3a {
79
80 public static int x;
81
82 public Inner3a(int val) {
83 x = val; // OK - "x" is a static field
84 val = x; // OK - "x" is a static field
85 e = val; // OK - "e" belongs to outer class
86 val = e; // OK - "e" belongs to outer class
87 Inner3.this.e = val; // OK - "e" belongs to outer class
88 super();
89 }
90 }
91 }
92
93 public static class Inner4 {
94 public int x;
95
96 public Inner4() {
97 x = 0; // OK
98 x = x + 1; // OK
99 super();
100 }
101
102 public Inner4(int a) {
103 this.x = 0; // OK
104 this.x = this.x + 1; // OK
105 super();
106 }
107
108 public Inner4(char a) {
109 Inner4.this.x = 0; // OK
110 Inner4.this.x = Inner4.this.x + 1; // OK
111 super();
112 }
113 }
114
115 public static class Inner5 extends Inner4 {
116 public int y;
117
118 public Inner5() {
119 y = x + 1; // FAIL - illegal early access
120 super();
121 }
122
123 public Inner5(int a) {
124 this.y = x + 1; // FAIL - illegal early access
125 super();
126 }
127
128 public Inner5(char a) {
129 Inner5.this.y = x + 1; // FAIL - illegal early access
130 super();
152
153 public static class Inner7 {
154 public final int x = 1;
155
156 public Inner7() {
157 x = 2; // FAIL - illegal early access
158 super();
159 }
160 }
161
162 public static class Inner8 {
163 class Inner8a {
164 int x;
165 }
166
167 public Inner8() {
168 this.new Inner8a().x = 1; // FAIL - illegal early access
169 super();
170 }
171 }
172
173 public static class Inner9 {
174 int x = 1;
175 int y;
176 Inner9() {
177 y = x; // FAIL, x has an initializer
178 super();
179 }
180 }
181 }
|