1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/protectedAccess/ProtectedMemberAccess1.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,648 @@ 1.4 +/* 1.5 + * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 4319507 1.30 + * @summary Verify correct implementation of JLS2e 6.6.2.1 1.31 + * @author maddox 1.32 + * 1.33 + * @run compile ProtectedMemberAccess1.java 1.34 + */ 1.35 + 1.36 +import pkg.SuperClass; 1.37 + 1.38 +class ProtectedMemberAccess1a extends SuperClass { 1.39 + 1.40 + // Access to a protected member via its simple name 1.41 + // is always legal in a subclass of the class in 1.42 + // which the member is declared. 1.43 + 1.44 + int i = pi; 1.45 + int j = spi; 1.46 + 1.47 + int x = pm(); 1.48 + int y = spm(); 1.49 + 1.50 + pC obj1; 1.51 + spC obj2; 1.52 + 1.53 + pI obj3; 1.54 + spI obj4; 1.55 + 1.56 + Object o1 = (pC) null; 1.57 + Object o2 = (spC) null; 1.58 + 1.59 + Object o3 = (pI) null; 1.60 + Object o4 = (spI) null; 1.61 + 1.62 + class C1 extends pC {} 1.63 + class C2 extends spC {} 1.64 + 1.65 + interface I1 extends pI {} 1.66 + interface I2 extends spI {} 1.67 + 1.68 + static { 1.69 + 1.70 + spi = 2; 1.71 + 1.72 + int y = spm(); 1.73 + 1.74 + pC obj1; 1.75 + spC obj2; 1.76 + 1.77 + pI obj3; 1.78 + spI obj4; 1.79 + 1.80 + Object o1 = (pC) null; 1.81 + Object o2 = (spC) null; 1.82 + 1.83 + Object o3 = (pI) null; 1.84 + Object o4 = (spI) null; 1.85 + 1.86 + //class C1 extends pC {} 1.87 + class C2 extends spC {} 1.88 + 1.89 + //interface I1 extends pI {} 1.90 + //interface I2 extends spI {} 1.91 + 1.92 + } 1.93 + 1.94 + void m() { 1.95 + 1.96 + pi = 1; 1.97 + spi = 2; 1.98 + 1.99 + int x = pm(); 1.100 + int y = spm(); 1.101 + 1.102 + pC obj1; 1.103 + spC obj2; 1.104 + 1.105 + pI obj3; 1.106 + spI obj4; 1.107 + 1.108 + Object o1 = (pC) null; 1.109 + Object o2 = (spC) null; 1.110 + 1.111 + Object o3 = (pI) null; 1.112 + Object o4 = (spI) null; 1.113 + 1.114 + class C1 extends pC {} 1.115 + class C2 extends spC {} 1.116 + 1.117 + //interface I1 extends pI {} 1.118 + //interface I2 extends spI {} 1.119 + 1.120 + } 1.121 + 1.122 + class Inner { 1.123 + 1.124 + int i = pi; 1.125 + int j = spi; 1.126 + 1.127 + int x = pm(); 1.128 + int y = spm(); 1.129 + 1.130 + pC obj1; 1.131 + spC obj2; 1.132 + 1.133 + pI obj3; 1.134 + spI obj4; 1.135 + 1.136 + Object o1 = (pC) null; 1.137 + Object o2 = (spC) null; 1.138 + 1.139 + Object o3 = (pI) null; 1.140 + Object o4 = (spI) null; 1.141 + 1.142 + class C1 extends pC {} 1.143 + class C2 extends spC {} 1.144 + 1.145 + //interface I1 extends pI {} 1.146 + //interface I2 extends spI {} 1.147 + 1.148 + // Not allowed in inner classes. 1.149 + // static { ... } 1.150 + 1.151 + void m() { 1.152 + 1.153 + pi = 1; 1.154 + spi = 2; 1.155 + 1.156 + int x = pm(); 1.157 + int y = spm(); 1.158 + 1.159 + pC obj1; 1.160 + spC obj2; 1.161 + 1.162 + pI obj3; 1.163 + spI obj4; 1.164 + 1.165 + Object o1 = (pC) null; 1.166 + Object o2 = (spC) null; 1.167 + 1.168 + Object o3 = (pI) null; 1.169 + Object o4 = (spI) null; 1.170 + 1.171 + class C1 extends pC {} 1.172 + class C2 extends spC {} 1.173 + 1.174 + //interface I1 extends pI {} 1.175 + //interface I2 extends spI {} 1.176 + } 1.177 + } 1.178 + 1.179 +} 1.180 + 1.181 +class ProtectedMemberAccess2a extends pkg.SuperClass { 1.182 + 1.183 + // Access to a protected instance (non-static) field, instance method, 1.184 + // or member type by a qualified name is always legal in a subclass of 1.185 + // the class in which the member is declared. Such access to a protected 1.186 + // instance field or instance method is allowed if the qualifying type 1.187 + // or the type of the qualifying expression is (a subclass of) the class 1.188 + // in which the reference occurs. 1.189 + 1.190 + ProtectedMemberAccess2a x = 1.191 + new ProtectedMemberAccess2a(); 1.192 + 1.193 + static ProtectedMemberAccess2a sx = 1.194 + new ProtectedMemberAccess2a(); 1.195 + 1.196 + int i = x.pi; 1.197 + int j = x.spi; 1.198 + 1.199 + int n = sx.pi; 1.200 + int m = sx.spi; 1.201 + 1.202 + static int sn = sx.pi; 1.203 + static int sm = sx.spi; 1.204 + 1.205 + int w = x.pm(); 1.206 + int y = x.spm(); 1.207 + 1.208 + int u = sx.pm(); 1.209 + int v = sx.spm(); 1.210 + 1.211 + ProtectedMemberAccess2a.pC obj1; 1.212 + ProtectedMemberAccess2a.spC obj2; 1.213 + 1.214 + ProtectedMemberAccess2a.pI obj3; 1.215 + ProtectedMemberAccess2a.spI obj4; 1.216 + 1.217 + Object o1 = (ProtectedMemberAccess2a.pC) null; 1.218 + Object o2 = (ProtectedMemberAccess2a.spC) null; 1.219 + 1.220 + Object o3 = (ProtectedMemberAccess2a.pI) null; 1.221 + Object o4 = (ProtectedMemberAccess2a.spI) null; 1.222 + 1.223 + class C1 extends ProtectedMemberAccess2a.pC {} 1.224 + class C2 extends ProtectedMemberAccess2a.spC {} 1.225 + 1.226 + interface I1 extends ProtectedMemberAccess2a.pI {} 1.227 + interface I2 extends ProtectedMemberAccess2a.spI {} 1.228 + 1.229 + static { 1.230 + 1.231 + ProtectedMemberAccess2a lx = 1.232 + new ProtectedMemberAccess2a(); 1.233 + 1.234 + sx.pi = 1; 1.235 + sx.spi = 2; 1.236 + 1.237 + lx.pi = 1; 1.238 + lx.spi = 2; 1.239 + 1.240 + int n = sx.pi; 1.241 + int m = sx.spi; 1.242 + 1.243 + int k = lx.pi; 1.244 + int l = lx.spi; 1.245 + 1.246 + int u = sx.pm(); 1.247 + int v = sx.spm(); 1.248 + 1.249 + int w = lx.pm(); 1.250 + int z = lx.spm(); 1.251 + 1.252 + ProtectedMemberAccess2a.pC obj1; 1.253 + ProtectedMemberAccess2a.spC obj2; 1.254 + 1.255 + ProtectedMemberAccess2a.pI obj3; 1.256 + ProtectedMemberAccess2a.spI obj4; 1.257 + 1.258 + Object o1 = (ProtectedMemberAccess2a.pC) null; 1.259 + Object o2 = (ProtectedMemberAccess2a.spC) null; 1.260 + 1.261 + Object o3 = (ProtectedMemberAccess2a.pI) null; 1.262 + Object o4 = (ProtectedMemberAccess2a.spI) null; 1.263 + 1.264 + //class C1 extends ProtectedMemberAccess2a.pC {} 1.265 + class C2 extends ProtectedMemberAccess2a.spC {} 1.266 + 1.267 + //interface I1 extends ProtectedMemberAccess2a.pI {} 1.268 + //interface I2 extends ProtectedMemberAccess2a.spI {} 1.269 + 1.270 + } 1.271 + 1.272 + void m() { 1.273 + 1.274 + ProtectedMemberAccess2a lx = 1.275 + new ProtectedMemberAccess2a(); 1.276 + 1.277 + x.pi = 1; 1.278 + x.spi = 2; 1.279 + 1.280 + sx.pi = 1; 1.281 + sx.spi = 2; 1.282 + 1.283 + lx.pi = 1; 1.284 + lx.spi = 2; 1.285 + 1.286 + int t = x.pm(); 1.287 + int y = x.spm(); 1.288 + 1.289 + int u = sx.pm(); 1.290 + int v = sx.spm(); 1.291 + 1.292 + int w = lx.pm(); 1.293 + int z = lx.spm(); 1.294 + 1.295 + int i = x.pi; 1.296 + int j = x.spi; 1.297 + 1.298 + int n = sx.pi; 1.299 + int m = sx.spi; 1.300 + 1.301 + int k = lx.pi; 1.302 + int l = lx.spi; 1.303 + 1.304 + ProtectedMemberAccess2a.pC obj1; 1.305 + ProtectedMemberAccess2a.spC obj2; 1.306 + 1.307 + ProtectedMemberAccess2a.pI obj3; 1.308 + ProtectedMemberAccess2a.spI obj4; 1.309 + 1.310 + Object o1 = (ProtectedMemberAccess2a.pC) null; 1.311 + Object o2 = (ProtectedMemberAccess2a.spC) null; 1.312 + 1.313 + Object o3 = (ProtectedMemberAccess2a.pI) null; 1.314 + Object o4 = (ProtectedMemberAccess2a.spI) null; 1.315 + 1.316 + class C1 extends ProtectedMemberAccess2a.pC {} 1.317 + class C2 extends ProtectedMemberAccess2a.spC {} 1.318 + 1.319 + //interface I1 extends ProtectedMemberAccess2a.pI {} 1.320 + //interface I2 extends ProtectedMemberAccess2a.spI {} 1.321 + 1.322 + } 1.323 + 1.324 + class Inner { 1.325 + 1.326 + int i = x.pi; 1.327 + int j = x.spi; 1.328 + 1.329 + int n = sx.pi; 1.330 + int m = sx.spi; 1.331 + 1.332 + //static int sn = sx.pi; 1.333 + //static int sm = sx.spi; 1.334 + 1.335 + int w = x.pm(); 1.336 + int y = x.spm(); 1.337 + 1.338 + int u = sx.pm(); 1.339 + int v = sx.spm(); 1.340 + 1.341 + ProtectedMemberAccess2a.pC obj1; 1.342 + ProtectedMemberAccess2a.spC obj2; 1.343 + 1.344 + ProtectedMemberAccess2a.pI obj3; 1.345 + ProtectedMemberAccess2a.spI obj4; 1.346 + 1.347 + Object o1 = (ProtectedMemberAccess2a.pC) null; 1.348 + Object o2 = (ProtectedMemberAccess2a.spC) null; 1.349 + 1.350 + Object o3 = (ProtectedMemberAccess2a.pI) null; 1.351 + Object o4 = (ProtectedMemberAccess2a.spI) null; 1.352 + 1.353 + class C1 extends ProtectedMemberAccess2a.pC {} 1.354 + class C2 extends ProtectedMemberAccess2a.spC {} 1.355 + 1.356 + //interface I1 extends ProtectedMemberAccess2a.pI {} 1.357 + //interface I2 extends ProtectedMemberAccess2a.spI {} 1.358 + 1.359 + // Not allowed in inner classes. 1.360 + // static { ... } 1.361 + 1.362 + void m() { 1.363 + 1.364 + ProtectedMemberAccess2a lx = 1.365 + new ProtectedMemberAccess2a(); 1.366 + 1.367 + x.pi = 1; 1.368 + x.spi = 2; 1.369 + 1.370 + sx.pi = 1; 1.371 + sx.spi = 2; 1.372 + 1.373 + lx.pi = 1; 1.374 + lx.spi = 2; 1.375 + 1.376 + int t = x.pm(); 1.377 + int y = x.spm(); 1.378 + 1.379 + int u = sx.pm(); 1.380 + int v = sx.spm(); 1.381 + 1.382 + int w = lx.pm(); 1.383 + int z = lx.spm(); 1.384 + 1.385 + int i = x.pi; 1.386 + int j = x.spi; 1.387 + 1.388 + int n = sx.pi; 1.389 + int m = sx.spi; 1.390 + 1.391 + int k = lx.pi; 1.392 + int l = lx.spi; 1.393 + 1.394 + ProtectedMemberAccess2a.pC obj1; 1.395 + ProtectedMemberAccess2a.spC obj2; 1.396 + 1.397 + ProtectedMemberAccess2a.pI obj3; 1.398 + ProtectedMemberAccess2a.spI obj4; 1.399 + 1.400 + Object o1 = (ProtectedMemberAccess2a.pC) null; 1.401 + Object o2 = (ProtectedMemberAccess2a.spC) null; 1.402 + 1.403 + Object o3 = (ProtectedMemberAccess2a.pI) null; 1.404 + Object o4 = (ProtectedMemberAccess2a.spI) null; 1.405 + 1.406 + class C1 extends ProtectedMemberAccess2a.pC {} 1.407 + class C2 extends ProtectedMemberAccess2a.spC {} 1.408 + 1.409 + //interface I1 extends ProtectedMemberAccess2a.pI {} 1.410 + //interface I2 extends ProtectedMemberAccess2a.spI {} 1.411 + 1.412 + } 1.413 + 1.414 + } 1.415 + 1.416 +} 1.417 + 1.418 + 1.419 +class SubClass extends ProtectedMemberAccess3a { } 1.420 + 1.421 +class ProtectedMemberAccess3a extends pkg.SuperClass { 1.422 + 1.423 + // Access to a protected instance (non-static) field, instance method, 1.424 + // or member type by a qualified name is always legal in a subclass of 1.425 + // the class in which the member is declared. Such access to a protected 1.426 + // instance field or instance method is allowed if the qualifying type 1.427 + // or the type of the qualifying expression is (a subclass of) the class 1.428 + // in which the reference occurs. 1.429 + 1.430 + SubClass x = new SubClass(); 1.431 + 1.432 + static SubClass sx = new SubClass(); 1.433 + 1.434 + int i = x.pi; 1.435 + int j = x.spi; 1.436 + 1.437 + int n = sx.pi; 1.438 + int m = sx.spi; 1.439 + 1.440 + static int sn = sx.pi; 1.441 + static int sm = sx.spi; 1.442 + 1.443 + int w = x.pm(); 1.444 + int y = x.spm(); 1.445 + 1.446 + int u = sx.pm(); 1.447 + int v = sx.spm(); 1.448 + 1.449 + SubClass.pC obj1; 1.450 + SubClass.spC obj2; 1.451 + 1.452 + SubClass.pI obj3; 1.453 + SubClass.spI obj4; 1.454 + 1.455 + Object o1 = (SubClass.pC) null; 1.456 + Object o2 = (SubClass.spC) null; 1.457 + 1.458 + Object o3 = (SubClass.pI) null; 1.459 + Object o4 = (SubClass.spI) null; 1.460 + 1.461 + class C1 extends SubClass.pC {} 1.462 + class C2 extends SubClass.spC {} 1.463 + 1.464 + interface I1 extends SubClass.pI {} 1.465 + interface I2 extends SubClass.spI {} 1.466 + 1.467 + static { 1.468 + 1.469 + SubClass lx = new SubClass(); 1.470 + 1.471 + sx.pi = 1; 1.472 + sx.spi = 2; 1.473 + 1.474 + lx.pi = 1; 1.475 + lx.spi = 2; 1.476 + 1.477 + int n = sx.pi; 1.478 + int m = sx.spi; 1.479 + 1.480 + int k = lx.pi; 1.481 + int l = lx.spi; 1.482 + 1.483 + int u = sx.pm(); 1.484 + int v = sx.spm(); 1.485 + 1.486 + int w = lx.pm(); 1.487 + int z = lx.spm(); 1.488 + 1.489 + SubClass.pC obj1; 1.490 + SubClass.spC obj2; 1.491 + 1.492 + SubClass.pI obj3; 1.493 + SubClass.spI obj4; 1.494 + 1.495 + Object o1 = (SubClass.pC) null; 1.496 + Object o2 = (SubClass.spC) null; 1.497 + 1.498 + Object o3 = (SubClass.pI) null; 1.499 + Object o4 = (SubClass.spI) null; 1.500 + 1.501 + //class C1 extends SubClass.pC {} 1.502 + class C2 extends SubClass.spC {} 1.503 + 1.504 + //interface I1 extends SubClass.pI {} 1.505 + //interface I2 extends SubClass.spI {} 1.506 + 1.507 + } 1.508 + 1.509 + void m() { 1.510 + 1.511 + SubClass lx = new SubClass(); 1.512 + 1.513 + x.pi = 1; 1.514 + x.spi = 2; 1.515 + 1.516 + sx.pi = 1; 1.517 + sx.spi = 2; 1.518 + 1.519 + lx.pi = 1; 1.520 + lx.spi = 2; 1.521 + 1.522 + int t = x.pm(); 1.523 + int y = x.spm(); 1.524 + 1.525 + int u = sx.pm(); 1.526 + int v = sx.spm(); 1.527 + 1.528 + int w = lx.pm(); 1.529 + int z = lx.spm(); 1.530 + 1.531 + int i = x.pi; 1.532 + int j = x.spi; 1.533 + 1.534 + int n = sx.pi; 1.535 + int m = sx.spi; 1.536 + 1.537 + int k = lx.pi; 1.538 + int l = lx.spi; 1.539 + 1.540 + SubClass.pC obj1; 1.541 + SubClass.spC obj2; 1.542 + 1.543 + SubClass.pI obj3; 1.544 + SubClass.spI obj4; 1.545 + 1.546 + Object o1 = (SubClass.pC) null; 1.547 + Object o2 = (SubClass.spC) null; 1.548 + 1.549 + Object o3 = (SubClass.pI) null; 1.550 + Object o4 = (SubClass.spI) null; 1.551 + 1.552 + class C1 extends SubClass.pC {} 1.553 + class C2 extends SubClass.spC {} 1.554 + 1.555 + //interface I1 extends SubClass.pI {} 1.556 + //interface I2 extends SubClass.spI {} 1.557 + 1.558 + } 1.559 + 1.560 + class Inner { 1.561 + 1.562 + int i = x.pi; 1.563 + int j = x.spi; 1.564 + 1.565 + int n = sx.pi; 1.566 + int m = sx.spi; 1.567 + 1.568 + //static int sn = sx.pi; 1.569 + //static int sm = sx.spi; 1.570 + 1.571 + int w = x.pm(); 1.572 + int y = x.spm(); 1.573 + 1.574 + int u = sx.pm(); 1.575 + int v = sx.spm(); 1.576 + 1.577 + SubClass.pC obj1; 1.578 + SubClass.spC obj2; 1.579 + 1.580 + SubClass.pI obj3; 1.581 + SubClass.spI obj4; 1.582 + 1.583 + Object o1 = (SubClass.pC) null; 1.584 + Object o2 = (SubClass.spC) null; 1.585 + 1.586 + Object o3 = (SubClass.pI) null; 1.587 + Object o4 = (SubClass.spI) null; 1.588 + 1.589 + class C1 extends SubClass.pC {} 1.590 + class C2 extends SubClass.spC {} 1.591 + 1.592 + //interface I1 extends SubClass.pI {} 1.593 + //interface I2 extends SubClass.spI {} 1.594 + 1.595 + // Not allowed in inner classes. 1.596 + // static { ... } 1.597 + 1.598 + void m() { 1.599 + 1.600 + SubClass lx = new SubClass(); 1.601 + 1.602 + x.pi = 1; 1.603 + x.spi = 2; 1.604 + 1.605 + sx.pi = 1; 1.606 + sx.spi = 2; 1.607 + 1.608 + lx.pi = 1; 1.609 + lx.spi = 2; 1.610 + 1.611 + int t = x.pm(); 1.612 + int y = x.spm(); 1.613 + 1.614 + int u = sx.pm(); 1.615 + int v = sx.spm(); 1.616 + 1.617 + int w = lx.pm(); 1.618 + int z = lx.spm(); 1.619 + 1.620 + int i = x.pi; 1.621 + int j = x.spi; 1.622 + 1.623 + int n = sx.pi; 1.624 + int m = sx.spi; 1.625 + 1.626 + int k = lx.pi; 1.627 + int l = lx.spi; 1.628 + 1.629 + SubClass.pC obj1; 1.630 + SubClass.spC obj2; 1.631 + 1.632 + SubClass.pI obj3; 1.633 + SubClass.spI obj4; 1.634 + 1.635 + Object o1 = (SubClass.pC) null; 1.636 + Object o2 = (SubClass.spC) null; 1.637 + 1.638 + Object o3 = (SubClass.pI) null; 1.639 + Object o4 = (SubClass.spI) null; 1.640 + 1.641 + class C1 extends SubClass.pC {} 1.642 + class C2 extends SubClass.spC {} 1.643 + 1.644 + //interface I1 extends SubClass.pI {} 1.645 + //interface I2 extends SubClass.spI {} 1.646 + 1.647 + } 1.648 + 1.649 + } 1.650 + 1.651 +}