< 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  * @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) {

 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();

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 }
< prev index next >