< prev index next >

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

Print this page

  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  * @enablePreview
  6  * @compile/fail/ref=EarlyAssignments.out -XDrawDiagnostics EarlyAssignments.java
  7  * @build InitializationWarningTester
  8  * @run main InitializationWarningTester EarlyAssignments EarlyAssignmentsWarnings.out
  9  */
 10 public class EarlyAssignments {
 11 
 12     public static class Inner1 {
 13         public int x;
 14 
 15         public Inner1() {
 16             x = 123;                        // OK - "x" belongs to this class
 17             this.x = 123;                   // OK - "x" belongs to this class
 18             Inner1.this.x = 123;            // OK - "x" belongs to this class
 19             super();
 20         }
 21 
 22         public Inner1(int y) {
 23             y = x;                          // OK - "x" belongs to this class
 24             y = this.x;                     // OK - "x" belongs to this class
 25             y = Inner1.this.x;              // OK - "x" belongs to this class
 26             super();
 27         }
 28 
 29         public class Inner1a extends Inner1 {
 30             public int z;
 31             public Inner1a(byte value) {
 32                 Inner1.this.x = value;      // OK - "x" belongs to outer class
 33                 z = super.x;                // FAIL - "x" belongs to superclass
 34                 z = x;                      // FAIL - "x" belongs to superclass
 35                 this.z = x;                 // FAIL - "x" belongs to superclass
 36                 Inner1a.this.z = x;         // FAIL - "x" belongs to superclass
 37                 Object o1 = Inner1.this;    // OK - Inner1 is an outer class
 38                 Object o2 = Inner1a.this;   // FAIL - Inner1a is this class
 39                 super();
 40             }
 41             public Inner1a(short value) {
 42                 x = value;                  // FAIL - "x" belongs to superclass
 43                 super();
 44             }
 45             public Inner1a(char value) {

 80         public class Inner3a {
 81 
 82             public static int x;
 83 
 84             public Inner3a(int val) {
 85                 x = val;                    // OK - "x" is a static field
 86                 val = x;                    // OK - "x" is a static field
 87                 e = val;                    // OK - "e" belongs to outer class
 88                 val = e;                    // OK - "e" belongs to outer class
 89                 Inner3.this.e = val;        // OK - "e" belongs to outer class
 90                 super();
 91             }
 92         }
 93     }
 94 
 95     public static class Inner4 {
 96         public int x;
 97 
 98         public Inner4() {
 99             x = 0;                              // OK
100             x = x + 1;                          // OK
101             super();
102         }
103 
104         public Inner4(int a) {
105             this.x = 0;                         // OK
106             this.x = this.x + 1;                // OK
107             super();
108         }
109 
110         public Inner4(char a) {
111             Inner4.this.x = 0;                  // OK
112             Inner4.this.x = Inner4.this.x + 1;  // OK
113             super();
114         }
115     }
116 
117     public static class Inner5 extends Inner4 {
118         public int y;
119 
120         public Inner5() {
121             y = x + 1;                          // FAIL - illegal early access
122             super();
123         }
124 
125         public Inner5(int a) {
126             this.y = x + 1;                     // FAIL - illegal early access
127             super();
128         }
129 
130         public Inner5(char a) {
131             Inner5.this.y = x + 1;              // FAIL - illegal early access
132             super();

154 
155     public static class Inner7 {
156         public final int x = 1;
157 
158         public Inner7() {
159             x = 2;                              // FAIL - illegal early access
160             super();
161         }
162     }
163 
164     public static class Inner8 {
165         class Inner8a {
166             int x;
167         }
168 
169         public Inner8() {
170             this.new Inner8a().x = 1;           // FAIL - illegal early access
171             super();
172         }
173     }
174 
175     public static class Inner9 {
176         int x = 1;
177         int y;
178         Inner9() {
179             y = x; // FAIL, x has an initializer
180             super();
181         }
182     }
183 }
< prev index next >