Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
1 /*
2 * @test /nodynamiccopyright/
3 * @bug 4319507 4785453
4 * @summary Verify correct implementation of JLS2e 6.6.2.1
5 * @author maddox
6 *
7 * @compile/fail/ref=ProtectedMemberAccess4.out -XDdiags=-simpleNames -XDdiagsFormat=%b:%l:%_%m ProtectedMemberAccess4.java
8 */
10 // 33 errors expected.
12 import pkg.SuperClass;
14 class ProtectedMemberAccess4 extends pkg.SuperClass {
16 // Access to a protected instance (non-static) field, instance method,
17 // or member type by a qualified name is always legal in a subclass of
18 // the class in which the member is declared. Such access to a protected
19 // instance field or instance method is allowed only if the qualifying type
20 // or the type of the qualifying expression is (a subclass of) the class
21 // in which the reference occurs. In this test case, the qualifying type
22 // is a superclass, thus all of the qualified references to instance fields
23 // and instance methods are illegal.
25 pkg.SuperClass x = new pkg.SuperClass();
27 static pkg.SuperClass sx = new pkg.SuperClass();
29 int i = x.pi; // illegal
30 int j = x.spi; // ok
32 int n = sx.pi; // illegal
33 int m = sx.spi; // ok
35 static int sn = sx.pi; // illegal
36 static int sm = sx.spi; // ok
38 int w = x.pm(); // illegal
39 int y = x.spm(); // ok
41 int u = sx.pm(); // illegal
42 int v = sx.spm(); // ok
44 pkg.SuperClass.pC obj1; // ok
45 pkg.SuperClass.spC obj2; // ok
47 pkg.SuperClass.pI obj3; // ok
48 pkg.SuperClass.spI obj4; // ok
50 Object o1 = (pkg.SuperClass.pC) null; // ok
51 Object o2 = (pkg.SuperClass.spC) null; // ok
53 Object o3 = (pkg.SuperClass.pI) null; // ok
54 Object o4 = (pkg.SuperClass.spI) null; // ok
56 class C1 extends pkg.SuperClass.pC {} // ok
57 class C2 extends pkg.SuperClass.spC {} // ok
59 interface I1 extends pkg.SuperClass.pI {} // ok
60 interface I2 extends pkg.SuperClass.spI {} // ok
62 static {
64 pkg.SuperClass lx = new pkg.SuperClass();
66 sx.pi = 1; // illegal
67 sx.spi = 2; // ok
69 lx.pi = 1; // illegal
70 lx.spi = 2; // ok
72 int n = sx.pi; // illegal
73 int m = sx.spi; // ok
75 int k = lx.pi; // illegal
76 int l = lx.spi; // ok
78 int u = sx.pm(); // illegal
79 int v = sx.spm(); // ok
81 int w = lx.pm(); // illegal
82 int z = lx.spm(); // ok
84 pkg.SuperClass.pC obj1; // ok
85 pkg.SuperClass.spC obj2; // ok
87 pkg.SuperClass.pI obj3; // ok
88 pkg.SuperClass.spI obj4; // ok
90 Object o1 = (pkg.SuperClass.pC) null; // ok
91 Object o2 = (pkg.SuperClass.spC) null; // ok
93 Object o3 = (pkg.SuperClass.pI) null; // ok
94 Object o4 = (pkg.SuperClass.spI) null; // ok
96 //class C1 extends pkg.SuperClass.pC {}
97 class C2 extends pkg.SuperClass.spC {} // ok
99 //interface I1 extends pkg.SuperClass.pI {}
100 //interface I2 extends pkg.SuperClass.spI {}
102 }
104 void m() {
106 pkg.SuperClass lx = new pkg.SuperClass();
108 x.pi = 1; // illegal
109 x.spi = 2; // ok
111 sx.pi = 1; // illegal
112 sx.spi = 2; // ok
114 lx.pi = 1; // illegal
115 lx.spi = 2; // ok
117 int t = x.pm(); // illegal
118 int y = x.spm(); // ok
120 int u = sx.pm(); // illegal
121 int v = sx.spm(); // ok
123 int w = lx.pm(); // illegal
124 int z = lx.spm(); // ok
126 int i = x.pi; // illegal
127 int j = x.spi; // ok
129 int n = sx.pi; // illegal
130 int m = sx.spi; // ok
132 int k = lx.pi; // illegal
133 int l = lx.spi; // ok
135 pkg.SuperClass.pC obj1; // ok
136 pkg.SuperClass.spC obj2; // ok
138 pkg.SuperClass.pI obj3; // ok
139 pkg.SuperClass.spI obj4; // ok
141 Object o1 = (pkg.SuperClass.pC) null; // ok
142 Object o2 = (pkg.SuperClass.spC) null; // ok
144 Object o3 = (pkg.SuperClass.pI) null; // ok
145 Object o4 = (pkg.SuperClass.spI) null; // ok
147 class C1 extends pkg.SuperClass.pC {} // ok
148 class C2 extends pkg.SuperClass.spC {} // ok
150 //interface I1 extends pkg.SuperClass.pI {}
151 //interface I2 extends pkg.SuperClass.spI {}
153 }
155 class Inner {
157 int i = x.pi; // illegal
158 int j = x.spi; // ok
160 int n = sx.pi; // illegal
161 int m = sx.spi; // ok
163 //static int sn = sx.pi;
164 //static int sm = sx.spi;
166 int w = x.pm(); // illegal
167 int y = x.spm(); // ok
169 int u = sx.pm(); // illegal
170 int v = sx.spm(); // ok
172 pkg.SuperClass.pC obj1; // ok
173 pkg.SuperClass.spC obj2; // ok
175 pkg.SuperClass.pI obj3; // ok
176 pkg.SuperClass.spI obj4; // ok
178 Object o1 = (pkg.SuperClass.pC) null; // ok
179 Object o2 = (pkg.SuperClass.spC) null; // ok
181 Object o3 = (pkg.SuperClass.pI) null; // ok
182 Object o4 = (pkg.SuperClass.spI) null; // ok
184 class C1 extends pkg.SuperClass.pC {} // ok
185 class C2 extends pkg.SuperClass.spC {} // ok
187 //interface I1 extends pkg.SuperClass.pI {}
188 //interface I2 extends pkg.SuperClass.spI {}
190 // Not allowed in inner classes.
191 // static { ... }
193 void m() {
195 pkg.SuperClass lx = new pkg.SuperClass();
197 x.pi = 1; // illegal
198 x.spi = 2; // ok
200 sx.pi = 1; // illegal
201 sx.spi = 2; // ok
203 lx.pi = 1; // illegal
204 lx.spi = 2; // ok
206 int t = x.pm(); // illegal
207 int y = x.spm(); // ok
209 int u = sx.pm(); // illegal
210 int v = sx.spm(); // ok
212 int w = lx.pm(); // illegal
213 int z = lx.spm(); // ok
215 int i = x.pi; // illegal
216 int j = x.spi; // ok
218 int n = sx.pi; // illegal
219 int m = sx.spi; // ok
221 int k = lx.pi; // illegal
222 int l = lx.spi; // ok
224 pkg.SuperClass.pC obj1; // ok
225 pkg.SuperClass.spC obj2; // ok
227 pkg.SuperClass.pI obj3; // ok
228 pkg.SuperClass.spI obj4; // ok
230 Object o1 = (pkg.SuperClass.pC) null; // ok
231 Object o2 = (pkg.SuperClass.spC) null; // ok
233 Object o3 = (pkg.SuperClass.pI) null; // ok
234 Object o4 = (pkg.SuperClass.spI) null; // ok
236 class C1 extends pkg.SuperClass.pC {} // ok
237 class C2 extends pkg.SuperClass.spC {} // ok
239 //interface I1 extends pkg.SuperClass.pI {}
240 //interface I2 extends pkg.SuperClass.spI {}
242 }
244 }
246 }