12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8046171 27 * @summary Test the various rules for nest members and nest-hosts by 28 * triggering nestmate access checks on all possible paths 29 * @compile TestNestmateMembership.java 30 * PackagedNestHost.java 31 * PackagedNestHost2.java 32 * 33 * @compile TargetNoHost.jcod 34 * CallerNoHost.jcod 35 * TargetMissingHost.jcod 36 * CallerMissingHost.jcod 37 * CallerNotInstanceHost.jcod 38 * TargetNotInstanceHost.jcod 39 * CallerNotOurHost.jcod 40 * TargetNotOurHost.jcod 41 * PackagedNestHost.jcod 42 * PackagedNestHost2Member.jcod 43 * PackagedNestHostMember.jcod 44 * 45 * @run main/othervm TestNestmateMembership method 46 * @run main/othervm TestNestmateMembership constructor 47 * @run main/othervm TestNestmateMembership getField 48 * @run main/othervm TestNestmateMembership putField 49 * @run main/othervm -Xcomp TestNestmateMembership getField 50 */ 51 52 // We test all the "illegal" relationships between a nest member and its nest-host 53 // except for the case where the name of the nest-member matches the name listed 54 // in the nest-host, but resolves to a different class. There doesn't seem to 55 // be a way to construct that scenario. 56 // For each nested class below there is a corresponding .jcod file which breaks one 57 // of the rules regarding nest membership. For the package related tests we have 58 // additional PackageNestHost*.java sources. 59 // Note that all the .java files must be compiled in the same step, while all 60 // .jcod files must be compiled in a later step. 61 62 // We test all the different nestmate access check paths: method invocation, constructor 63 // invocations, field get and field put. The test is invoked four times with each using 64 // a different test mode. Plus an extra Xcomp run for field access to hit ciField path. 65 // 66 // As access checking requires resolution and validation of the nest-host of 67 // both the caller class and the target class, we must check that all 68 // combinations of good/bad caller/target are checked for each of the 69 // possible errors: 70 // - no nest-host attribute 71 // - nest-host class can not be found 72 // - nest-host class is not an instance class 73 // - class is not a member of nest-host's nest 74 // - class and nest-host are in different packages 75 // 76 // To provide coverage for reflection and MethodHandle paths through 77 // JVM_AreNestmates, we add reflection/MH accesses to a subset of the tests. 78 // We only need to test one case (for Caller.xxx) as all cases use the same path; further 79 // we don't need to test all failure cases, as all exceptions are equivalent in that regard, 80 // but for good measure we test the four basic error situations (eliding the different 81 // package test for simplicity). 82 // 83 84 import java.lang.invoke.*; 85 import static java.lang.invoke.MethodHandles.*; 86 import static java.lang.invoke.MethodType.*; 87 88 public class TestNestmateMembership { 89 90 static final MethodType VOID_T = MethodType.methodType(void.class); 91 92 static class Caller { 93 94 private Caller() {} 95 96 private static void m() { 97 System.out.println("Caller.m()"); 98 } 99 100 // direct static method invocations 101 102 public static void invokeTarget() { 103 Target.m(); 104 } 105 public static void invokeTargetNoHost() { 106 TargetNoHost.m(); 107 } 108 public static void invokeTargetMissingHost() { 109 TargetMissingHost.m(); 110 } 111 public static void invokeTargetNotInstanceHost() { 112 TargetNotInstanceHost.m(); 113 } 114 public static void invokeTargetNotOurHost() { 115 TargetNotOurHost.m(); 116 } 117 118 // reflective static method invocations 119 120 public static void invokeTargetNoHostReflectively() throws Throwable { 121 TargetNoHost.class.getDeclaredMethod("m", new Class<?>[0]).invoke(null, new Object[0]); 122 } 123 public static void invokeTargetMissingHostReflectively() throws Throwable { 124 TargetMissingHost.class.getDeclaredMethod("m", new Class<?>[0]).invoke(null, new Object[0]); 125 } 126 public static void invokeTargetNotInstanceHostReflectively() throws Throwable { 127 TargetNotInstanceHost.class.getDeclaredMethod("m", new Class<?>[0]).invoke(null, new Object[0]); 128 } 129 public static void invokeTargetNotOurHostReflectively() throws Throwable { 130 TargetNotOurHost.class.getDeclaredMethod("m", new Class<?>[0]).invoke(null, new Object[0]); 131 } 132 133 // MethodHandle static method lookup (no invoke as the lookup should fail) 134 135 public static void invokeTargetNoHostMH() throws Throwable { 136 MethodHandle mh = lookup().findStatic(TargetNoHost.class, "m", VOID_T); 137 } 138 public static void invokeTargetMissingHostMH() throws Throwable { 139 MethodHandle mh = lookup().findStatic(TargetMissingHost.class, "m", VOID_T); 140 } 141 public static void invokeTargetNotInstanceHostMH() throws Throwable { 142 MethodHandle mh = lookup().findStatic(TargetNotInstanceHost.class, "m", VOID_T); 143 } 144 public static void invokeTargetNotOurHostMH() throws Throwable { 145 MethodHandle mh = lookup().findStatic(TargetNotOurHost.class, "m", VOID_T); 146 } 147 148 149 // direct constructor invocations 150 151 public static void newTarget() { 152 Object o = new Target(); 153 } 154 public static void newTargetNoHost() { 155 Object o = new TargetNoHost(); 156 } 157 public static void newTargetMissingHost() { 158 Object o = new TargetMissingHost(); 159 } 160 public static void newTargetNotInstanceHost() { 161 Object o = new TargetNotInstanceHost(); 162 } 163 public static void newTargetNotOurHost() { 164 Object o = new TargetNotOurHost(); 165 } 166 167 // reflective constructor invocations 168 169 public static void newTargetNoHostReflectively() throws Throwable { 170 Object o = TargetNoHost.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]); 171 } 172 public static void newTargetMissingHostReflectively() throws Throwable { 173 Object o = TargetMissingHost.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]); 174 } 175 public static void newTargetNotInstanceHostReflectively() throws Throwable { 176 Object o = TargetNotInstanceHost.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]); 177 } 178 public static void newTargetNotOurHostReflectively() throws Throwable { 179 Object o = TargetNotOurHost.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]); 180 } 181 182 // MethodHandle constructor lookup (no invoke as the lookup should fail) 183 184 public static void newTargetNoHostMH() throws Throwable { 185 MethodHandle mh = lookup().findConstructor(TargetNoHost.class, VOID_T); 186 } 187 public static void newTargetMissingHostMH() throws Throwable { 188 MethodHandle mh = lookup().findConstructor(TargetMissingHost.class, VOID_T); 189 } 190 public static void newTargetNotInstanceHostMH() throws Throwable { 191 MethodHandle mh = lookup().findConstructor(TargetNotInstanceHost.class, VOID_T); 192 } 193 public static void newTargetNotOurHostMH() throws Throwable { 194 MethodHandle mh = lookup().findConstructor(TargetNotOurHost.class, VOID_T); 195 } 196 197 private static int f; 198 199 // direct field accesses 200 201 public static void getFieldTarget() { 202 int x = Target.f; 203 } 204 public static void getFieldTargetNoHost() { 205 int x = TargetNoHost.f; 206 } 207 public static void getFieldTargetMissingHost() { 208 int x = TargetMissingHost.f; 209 } 210 public static void getFieldTargetNotInstanceHost() { 211 int x = TargetNotInstanceHost.f; 212 } 213 public static void getFieldTargetNotOurHost() { 214 int x = TargetNotOurHost.f; 215 } 216 217 public static void putFieldTarget() { 218 Target.f = 42; 219 } 220 public static void putFieldTargetNoHost() { 221 TargetNoHost.f = 42; 222 } 223 public static void putFieldTargetMissingHost() { 224 TargetMissingHost.f = 42; 225 } 226 public static void putFieldTargetNotInstanceHost() { 227 TargetNotInstanceHost.f = 42; 228 } 229 public static void putFieldTargetNotOurHost() { 230 TargetNotOurHost.f = 42; 231 } 232 233 // reflective field accesses 234 235 public static void getFieldTargetNoHostReflectively() throws Throwable { 236 int x = TargetNoHost.class.getDeclaredField("f").getInt(null); 237 } 238 public static void getFieldTargetMissingHostReflectively() throws Throwable { 239 int x = TargetMissingHost.class.getDeclaredField("f").getInt(null); 240 } 241 public static void getFieldTargetNotInstanceHostReflectively() throws Throwable { 242 int x = TargetNotInstanceHost.class.getDeclaredField("f").getInt(null); 243 } 244 public static void getFieldTargetNotOurHostReflectively() throws Throwable { 245 int x = TargetNotOurHost.class.getDeclaredField("f").getInt(null); 246 } 247 248 public static void putFieldTargetNoHostReflectively() throws Throwable { 249 TargetNoHost.class.getDeclaredField("f").setInt(null, 42); 250 } 251 public static void putFieldTargetMissingHostReflectively() throws Throwable { 252 TargetMissingHost.class.getDeclaredField("f").setInt(null, 42); 253 } 254 public static void putFieldTargetNotInstanceHostReflectively() throws Throwable { 255 TargetNotInstanceHost.class.getDeclaredField("f").setInt(null, 42); 256 } 257 public static void putFieldTargetNotOurHostReflectively() throws Throwable { 258 TargetNotOurHost.class.getDeclaredField("f").setInt(null, 42); 259 } 260 261 // MethodHandle field lookup (no access as the lookup will fail) 262 263 public static void getFieldTargetNoHostMH() throws Throwable { 264 MethodHandle mh = lookup().findStaticGetter(TargetNoHost.class, "f", int.class); 265 } 266 public static void getFieldTargetMissingHostMH() throws Throwable { 267 MethodHandle mh = lookup().findStaticGetter(TargetMissingHost.class, "f", int.class); 268 } 269 public static void getFieldTargetNotInstanceHostMH() throws Throwable { 270 MethodHandle mh = lookup().findStaticGetter(TargetNotInstanceHost.class, "f", int.class); 271 } 272 public static void getFieldTargetNotOurHostMH() throws Throwable { 273 MethodHandle mh = lookup().findStaticGetter(TargetNotOurHost.class, "f", int.class); 274 } 275 276 public static void putFieldTargetNoHostMH() throws Throwable { 277 MethodHandle mh = lookup().findStaticSetter(TargetNoHost.class, "f", int.class); 278 } 279 public static void putFieldTargetMissingHostMH() throws Throwable { 280 MethodHandle mh = lookup().findStaticSetter(TargetMissingHost.class, "f", int.class); 281 } 282 public static void putFieldTargetNotInstanceHostMH() throws Throwable { 283 MethodHandle mh = lookup().findStaticSetter(TargetNotInstanceHost.class, "f", int.class); 284 } 285 public static void putFieldTargetNotOurHostMH() throws Throwable { 286 MethodHandle mh = lookup().findStaticSetter(TargetNotOurHost.class, "f", int.class); 287 } 288 289 } 290 291 static class CallerNoHost { 292 293 // method invocations 294 295 private static void m() { 296 System.out.println("CallerNoHost.m() - java version"); 297 } 298 public static void invokeTarget() { 316 // field accesses 317 318 private static int f; 319 320 public static void getFieldTarget() { 321 int x = Target.f; 322 } 323 public static void getFieldTargetNoHost() { 324 int x = TargetNoHost.f; 325 } 326 327 public static void putFieldTarget() { 328 Target.f = 42; 329 } 330 public static void putFieldTargetNoHost() { 331 TargetNoHost.f = 42; 332 } 333 334 } 335 336 static class CallerMissingHost { 337 String msg = "NoCallerMissingHost"; // for cp entry 338 339 // method invocations 340 341 private static void m() { 342 System.out.println("CallerMissingHost.m() - java version"); 343 } 344 public static void invokeTarget() { 345 Target.m(); 346 } 347 public static void invokeTargetMissingHost() { 348 TargetMissingHost.m(); 349 } 350 351 // constructor invocations 352 353 private CallerMissingHost() {} 354 355 public static void newTarget() { 465 } 466 467 } 468 469 static class Target { 470 private Target() {} 471 private static int f; 472 private static void m() { 473 System.out.println("Target.m()"); 474 } 475 } 476 477 static class TargetNoHost { 478 private TargetNoHost() {} 479 private static int f; 480 private static void m() { 481 System.out.println("TargetNoHost.m() - java version"); 482 } 483 } 484 485 static class TargetMissingHost { 486 String msg = "NoTargetMissingHost"; // for cp entry 487 private TargetMissingHost() {} 488 private static int f; 489 private static void m() { 490 System.out.println("TargetMissingHost.m() - java version"); 491 } 492 } 493 494 static class TargetNotInstanceHost { 495 Object[] oa; // create CP entry to use in jcod change 496 private TargetNotInstanceHost() {} 497 private static int f; 498 private static void m() { 499 System.out.println("TargetNotInstanceHost.m() - java version"); 500 } 501 } 502 503 static class TargetNotOurHost { 504 private TargetNotOurHost() {} 505 private static int f; 506 private static void m() { 507 System.out.println("TargetNotOurHost.m() - java version"); 508 } 509 } 510 511 public static void main(String[] args) throws Throwable { 512 if (args.length < 1) { 513 throw new Error("Test mode argument must be one of: method, constructor, getField or putField"); 514 } 515 switch(args[0]) { 516 case "method": 517 System.out.println("TESTING METHOD INVOCATIONS:"); 518 test_GoodInvoke(); 519 test_NoHostInvoke(); 520 test_MissingHostInvoke(); 521 test_NotInstanceHostInvoke(); 522 test_NotOurHostInvoke(); 523 test_WrongPackageHostInvoke(); 524 break; 525 case "constructor": 526 System.out.println("TESTING CONSTRUCTOR INVOCATIONS:"); 527 test_GoodConstruct(); 528 test_NoHostConstruct(); 529 test_MissingHostConstruct(); 530 test_NotInstanceHostConstruct(); 531 test_NotOurHostConstruct(); 532 test_WrongPackageHostConstruct(); 533 break; 534 case "getField": 535 System.out.println("TESTING GETFIELD INVOCATIONS:"); 536 test_GoodGetField(); 537 test_NoHostGetField(); 538 test_MissingHostGetField(); 539 test_NotInstanceHostGetField(); 540 test_NotOurHostGetField(); 541 test_WrongPackageHostGetField(); 542 break; 543 case "putField": 544 System.out.println("TESTING PUTFIELD INVOCATIONS:"); 545 test_GoodPutField(); 546 test_NoHostPutField(); 547 test_MissingHostPutField(); 548 test_NotInstanceHostPutField(); 549 test_NotOurHostPutField(); 550 test_WrongPackageHostPutField(); 551 break; 552 default: 553 throw new Error("Uknown mode: " + args[0] + 554 ". Must be one of: method, constructor, getField or putField"); 555 } 556 } 557 558 static void test_GoodInvoke(){ 559 try { 560 Caller.invokeTarget(); 561 } 562 catch (Exception e) { 563 throw new Error("Unexpected exception on good invocation " + e); 564 } 565 } 566 597 msg = "tried to access method TestNestmateMembership$Target.m()V" + 598 " from class TestNestmateMembership$CallerNoHost"; 599 try { 600 CallerNoHost.invokeTarget(); 601 throw new Error("Missing IllegalAccessError: " + msg); 602 } 603 catch (IllegalAccessError expected) { 604 check_expected(expected, msg); 605 } 606 msg = "tried to access method TestNestmateMembership$TargetNoHost.m()V" + 607 " from class TestNestmateMembership$CallerNoHost"; 608 try { 609 CallerNoHost.invokeTargetNoHost(); 610 throw new Error("Missing IllegalAccessError: " + msg); 611 } 612 catch (IllegalAccessError expected) { 613 check_expected(expected, msg); 614 } 615 } 616 617 static void test_MissingHostInvoke() throws Throwable { 618 System.out.println("Testing for nest-host class that does not exist"); 619 String msg = "Unable to load nest-host class (NoTargetMissingHost) of " + 620 "TestNestmateMembership$TargetMissingHost"; 621 String cause_msg = "NoTargetMissingHost"; 622 try { 623 Caller.invokeTargetMissingHost(); 624 throw new Error("Missing NoClassDefFoundError: " + msg); 625 } 626 catch (NoClassDefFoundError expected) { 627 check_expected(expected, msg, cause_msg); 628 } 629 try { 630 Caller.invokeTargetMissingHostReflectively(); 631 throw new Error("Missing NoClassDefFoundError: " + msg); 632 } 633 catch (NoClassDefFoundError expected) { 634 check_expected(expected, msg, cause_msg); 635 } 636 msg = "no such method: TestNestmateMembership$TargetMissingHost.m()void/invokeStatic"; 658 throw new Error("Missing NoClassDefFoundError: " + msg); 659 } 660 catch (NoClassDefFoundError expected) { 661 check_expected(expected, msg, cause_msg); 662 } 663 msg = "Unable to load nest-host class (NoCallerMissingHost) of "+ 664 "TestNestmateMembership$CallerMissingHost"; 665 cause_msg = "NoCallerMissingHost"; 666 try { 667 CallerMissingHost.invokeTargetMissingHost(); 668 throw new Error("Missing NoClassDefFoundError: " + msg); 669 } 670 catch (NoClassDefFoundError expected) { 671 check_expected(expected, msg, cause_msg); 672 } 673 } 674 675 static void test_NotInstanceHostInvoke() throws Throwable { 676 System.out.println("Testing for nest-host class that is not an instance class"); 677 String msg = "Type TestNestmateMembership$TargetNotInstanceHost is not a "+ 678 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 679 try { 680 Caller.invokeTargetNotInstanceHost(); 681 throw new Error("Missing IllegalAccessError: " + msg); 682 } 683 catch (IllegalAccessError expected) { 684 check_expected(expected, msg); 685 } 686 try { 687 Caller.invokeTargetNotInstanceHostReflectively(); 688 throw new Error("Missing IllegalAccessError: " + msg); 689 } 690 catch (IllegalAccessError expected) { 691 check_expected(expected, msg); 692 } 693 msg = "no such method: TestNestmateMembership$TargetNotInstanceHost.m()void/invokeStatic"; 694 try { 695 Caller.invokeTargetNotInstanceHostMH(); 696 throw new Error("Missing IllegalAccessException: " + msg); 697 } 698 catch (IllegalAccessException expected) { 699 check_expected(expected, msg); 700 } 701 702 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 703 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 704 try { 705 CallerNotInstanceHost.invokeTarget(); 706 throw new Error("Missing IllegalAccessError: " + msg); 707 } 708 catch (IllegalAccessError expected) { 709 check_expected(expected, msg); 710 } 711 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 712 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 713 try { 714 CallerNotInstanceHost.invokeTargetNotInstanceHost(); 715 throw new Error("Missing IllegalAccessError: " + msg); 716 } 717 catch (IllegalAccessError expected) { 718 check_expected(expected, msg); 719 } 720 } 721 722 static void test_NotOurHostInvoke() throws Throwable { 723 System.out.println("Testing for nest-host class that does not list us in its nest"); 724 String msg = "Type TestNestmateMembership$TargetNotOurHost is not a nest member" + 725 " of java.lang.Object: current type is not listed as a nest member"; 726 try { 727 Caller.invokeTargetNotOurHost(); 728 throw new Error("Missing IllegalAccessError: " + msg); 729 } 730 catch (IllegalAccessError expected) { 731 check_expected(expected, msg); 732 } 733 try { 734 Caller.invokeTargetNotOurHostReflectively(); 735 throw new Error("Missing IllegalAccessError: " + msg); 736 } 737 catch (IllegalAccessError expected) { 738 check_expected(expected, msg); 739 } 740 msg = "no such method: TestNestmateMembership$TargetNotOurHost.m()void/invokeStatic"; 741 try { 742 Caller.invokeTargetNotOurHostMH(); 743 throw new Error("Missing IllegalAccessException: " + msg); 744 } 745 catch (IllegalAccessException expected) { 746 check_expected(expected, msg); 747 } 748 749 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 750 " of java.lang.Object: current type is not listed as a nest member"; 751 try { 752 CallerNotOurHost.invokeTarget(); 753 throw new Error("Missing IllegalAccessError: " + msg); 754 } 755 catch (IllegalAccessError expected) { 756 check_expected(expected, msg); 757 } 758 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 759 " of java.lang.Object: current type is not listed as a nest member"; 760 try { 761 CallerNotOurHost.invokeTargetNotOurHost(); 762 throw new Error("Missing IllegalAccessError: " + msg); 763 } 764 catch (IllegalAccessError expected) { 765 check_expected(expected, msg); 766 } 767 } 768 769 static void test_WrongPackageHostInvoke() { 770 System.out.println("Testing for nest-host and nest-member in different packages"); 771 String msg = "Type P2.PackagedNestHost2$Member is not a nest member of " + 772 "P1.PackagedNestHost: types are in different packages"; 773 try { 774 P1.PackagedNestHost.doInvoke(); 775 throw new Error("Missing IllegalAccessError: " + msg); 776 } 777 catch (IllegalAccessError expected) { 778 check_expected(expected, msg); 779 } 780 try { 781 P2.PackagedNestHost2.Member.doInvoke(); 782 throw new Error("Missing IllegalAccessError: " + msg); 783 } 784 catch (IllegalAccessError expected) { 785 check_expected(expected, msg); 786 } 787 } 788 789 // constructor tests 790 791 static void test_GoodConstruct(){ 792 try { 793 Caller.newTarget(); 794 } 795 catch (Exception e) { 796 throw new Error("Unexpected exception on good construction: " + e); 797 } 798 } 799 800 static void test_NoHostConstruct() throws Throwable { 801 System.out.println("Testing for missing nest-host attribute"); 802 String msg = "tried to access method TestNestmateMembership$TargetNoHost.<init>()V" + 803 " from class TestNestmateMembership$Caller"; 804 try { 805 Caller.newTargetNoHost(); 806 throw new Error("Missing IllegalAccessError: " + msg); 807 } 808 catch (IllegalAccessError expected) { 809 check_expected(expected, msg); 810 } 811 msg = "class TestNestmateMembership$Caller cannot access a member of class " + 812 "TestNestmateMembership$TargetNoHost with modifiers \"private\""; 813 try { 814 Caller.newTargetNoHostReflectively(); 815 throw new Error("Missing IllegalAccessException: " + msg); 816 } 817 catch (IllegalAccessException expected) { 818 check_expected(expected, msg); 819 } 820 msg = "no such constructor: TestNestmateMembership$TargetNoHost.<init>()void/newInvokeSpecial"; 821 try { 822 Caller.newTargetNoHostMH(); 823 throw new Error("Missing IllegalAccessException: " + msg); 824 } 825 catch (IllegalAccessException expected) { 826 check_expected(expected, msg); 827 } 828 829 msg = "tried to access method TestNestmateMembership$Target.<init>()V" + 830 " from class TestNestmateMembership$CallerNoHost"; 831 try { 832 CallerNoHost.newTarget(); 833 throw new Error("Missing IllegalAccessError: " + msg); 834 } 835 catch (IllegalAccessError expected) { 836 check_expected(expected, msg); 837 } 838 msg = "tried to access method TestNestmateMembership$TargetNoHost.<init>()V" + 839 " from class TestNestmateMembership$CallerNoHost"; 840 try { 841 CallerNoHost.newTargetNoHost(); 842 throw new Error("Missing IllegalAccessError: " + msg); 843 } 844 catch (IllegalAccessError expected) { 845 check_expected(expected, msg); 846 } 847 } 848 849 static void test_MissingHostConstruct() throws Throwable { 850 System.out.println("Testing for nest-host class that does not exist"); 851 String msg = "Unable to load nest-host class (NoTargetMissingHost) of " + 852 "TestNestmateMembership$TargetMissingHost"; 853 String cause_msg = "NoTargetMissingHost"; 854 try { 855 Caller.newTargetMissingHost(); 856 throw new Error("Missing NoClassDefFoundError: " + msg); 857 } 858 catch (NoClassDefFoundError expected) { 859 check_expected(expected, msg, cause_msg); 860 } 861 try { 862 Caller.newTargetMissingHostReflectively(); 863 throw new Error("Missing NoClassDefFoundError: " + msg); 864 } 882 throw new Error("Missing NoClassDefFoundError: " + msg); 883 } 884 catch (NoClassDefFoundError expected) { 885 check_expected(expected, msg, cause_msg); 886 } 887 msg = "Unable to load nest-host class (NoCallerMissingHost) of "+ 888 "TestNestmateMembership$CallerMissingHost"; 889 cause_msg = "NoCallerMissingHost"; 890 try { 891 CallerMissingHost.newTargetMissingHost(); 892 throw new Error("Missing NoClassDefFoundError: " + msg); 893 } 894 catch (NoClassDefFoundError expected) { 895 check_expected(expected, msg, cause_msg); 896 } 897 } 898 899 static void test_NotInstanceHostConstruct() throws Throwable { 900 System.out.println("Testing for nest-host class that is not an instance class"); 901 String msg = "Type TestNestmateMembership$TargetNotInstanceHost is not a "+ 902 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 903 try { 904 Caller.newTargetNotInstanceHost(); 905 throw new Error("Missing IllegalAccessError: " + msg); 906 } 907 catch (IllegalAccessError expected) { 908 check_expected(expected, msg); 909 } 910 try { 911 Caller.newTargetNotInstanceHostReflectively(); 912 throw new Error("Missing IllegalAccessError: " + msg); 913 } 914 catch (IllegalAccessError expected) { 915 check_expected(expected, msg); 916 } 917 msg = "no such constructor: TestNestmateMembership$TargetNotInstanceHost.<init>()void/newInvokeSpecial"; 918 try { 919 Caller.newTargetNotInstanceHostMH(); 920 throw new Error("Missing IllegalAccessException: " + msg); 921 } 922 catch (IllegalAccessException expected) { 923 check_expected(expected, msg); 924 } 925 926 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 927 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 928 try { 929 CallerNotInstanceHost.newTarget(); 930 throw new Error("Missing IllegalAccessError: " + msg); 931 } 932 catch (IllegalAccessError expected) { 933 check_expected(expected, msg); 934 } 935 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 936 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 937 try { 938 CallerNotInstanceHost.newTargetNotInstanceHost(); 939 throw new Error("Missing IllegalAccessError: " + msg); 940 } 941 catch (IllegalAccessError expected) { 942 check_expected(expected, msg); 943 } 944 } 945 946 static void test_NotOurHostConstruct() throws Throwable { 947 System.out.println("Testing for nest-host class that does not list us in its nest"); 948 String msg = "Type TestNestmateMembership$TargetNotOurHost is not a nest member" + 949 " of java.lang.Object: current type is not listed as a nest member"; 950 try { 951 Caller.newTargetNotOurHost(); 952 throw new Error("Missing IllegalAccessError: " + msg); 953 } 954 catch (IllegalAccessError expected) { 955 check_expected(expected, msg); 956 } 957 try { 958 Caller.newTargetNotOurHostReflectively(); 959 throw new Error("Missing IllegalAccessError: " + msg); 960 } 961 catch (IllegalAccessError expected) { 962 check_expected(expected, msg); 963 } 964 msg = "no such constructor: TestNestmateMembership$TargetNotOurHost.<init>()void/newInvokeSpecial"; 965 try { 966 Caller.newTargetNotOurHostMH(); 967 throw new Error("Missing IllegalAccessException: " + msg); 968 } 969 catch (IllegalAccessException expected) { 970 check_expected(expected, msg); 971 } 972 973 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 974 " of java.lang.Object: current type is not listed as a nest member"; 975 try { 976 CallerNotOurHost.newTarget(); 977 throw new Error("Missing IllegalAccessError: " + msg); 978 } 979 catch (IllegalAccessError expected) { 980 check_expected(expected, msg); 981 } 982 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 983 " of java.lang.Object: current type is not listed as a nest member"; 984 try { 985 CallerNotOurHost.newTargetNotOurHost(); 986 throw new Error("Missing IllegalAccessError: " + msg); 987 } 988 catch (IllegalAccessError expected) { 989 check_expected(expected, msg); 990 } 991 } 992 993 static void test_WrongPackageHostConstruct() { 994 System.out.println("Testing for nest-host and nest-member in different packages"); 995 String msg = "Type P2.PackagedNestHost2$Member is not a nest member of " + 996 "P1.PackagedNestHost: types are in different packages"; 997 try { 998 P1.PackagedNestHost.doConstruct(); 999 throw new Error("Missing IllegalAccessError: " + msg); 1000 } 1001 catch (IllegalAccessError expected) { 1002 check_expected(expected, msg); 1003 } 1004 try { 1005 P2.PackagedNestHost2.Member.doConstruct(); 1006 throw new Error("Missing IllegalAccessError: " + msg); 1007 } 1008 catch (IllegalAccessError expected) { 1009 check_expected(expected, msg); 1010 } 1011 } 1012 1013 // field tests 1014 1015 static void test_GoodGetField(){ 1016 try { 1017 Caller.getFieldTarget(); 1018 } 1019 catch (Exception e) { 1020 throw new Error("Unexpected exception on good field access: " + e); 1021 } 1022 } 1023 1024 static void test_NoHostGetField() throws Throwable { 1025 System.out.println("Testing for missing nest-host attribute"); 1026 String msg = "tried to access field TestNestmateMembership$TargetNoHost.f" + 1027 " from class TestNestmateMembership$Caller"; 1028 try { 1029 Caller.getFieldTargetNoHost(); 1030 throw new Error("Missing IllegalAccessError: " + msg); 1031 } 1032 catch (IllegalAccessError expected) { 1033 check_expected(expected, msg); 1034 } 1035 msg = "class TestNestmateMembership$Caller cannot access a member of class " + 1036 "TestNestmateMembership$TargetNoHost with modifiers \"private static\""; 1037 try { 1038 Caller.getFieldTargetNoHostReflectively(); 1039 throw new Error("Missing IllegalAccessException: " + msg); 1040 } 1041 catch (IllegalAccessException expected) { 1042 check_expected(expected, msg); 1043 } 1044 msg = "member is private: TestNestmateMembership$TargetNoHost.f/int/getStatic"; 1045 try { 1046 Caller.getFieldTargetNoHostMH(); 1047 throw new Error("Missing IllegalAccessException: " + msg); 1048 } 1049 catch (IllegalAccessException expected) { 1050 check_expected(expected, msg); 1051 } 1052 1053 msg = "tried to access field TestNestmateMembership$Target.f" + 1054 " from class TestNestmateMembership$CallerNoHost"; 1055 try { 1056 CallerNoHost.getFieldTarget(); 1057 throw new Error("Missing IllegalAccessError: " + msg); 1058 } 1059 catch (IllegalAccessError expected) { 1060 check_expected(expected, msg); 1061 } 1062 msg = "tried to access field TestNestmateMembership$TargetNoHost.f" + 1063 " from class TestNestmateMembership$CallerNoHost"; 1064 try { 1065 CallerNoHost.getFieldTargetNoHost(); 1066 throw new Error("Missing IllegalAccessError: " + msg); 1067 } 1068 catch (IllegalAccessError expected) { 1069 check_expected(expected, msg); 1070 } 1071 } 1072 1073 static void test_MissingHostGetField() throws Throwable { 1074 System.out.println("Testing for nest-host class that does not exist"); 1075 String msg = "Unable to load nest-host class (NoTargetMissingHost) of " + 1076 "TestNestmateMembership$TargetMissingHost"; 1077 String cause_msg = "NoTargetMissingHost"; 1078 try { 1079 Caller.getFieldTargetMissingHost(); 1080 throw new Error("Missing NoClassDefFoundError: " + msg); 1081 } 1082 catch (NoClassDefFoundError expected) { 1083 check_expected(expected, msg, cause_msg); 1084 } 1085 try { 1086 Caller.getFieldTargetMissingHostReflectively(); 1087 throw new Error("Missing NoClassDefFoundError: " + msg); 1088 } 1105 throw new Error("Missing NoClassDefFoundError: " + msg); 1106 } 1107 catch (NoClassDefFoundError expected) { 1108 check_expected(expected, msg, cause_msg); 1109 } 1110 msg = "Unable to load nest-host class (NoCallerMissingHost) of "+ 1111 "TestNestmateMembership$CallerMissingHost"; 1112 cause_msg = "NoCallerMissingHost"; 1113 try { 1114 CallerMissingHost.getFieldTargetMissingHost(); 1115 throw new Error("Missing NoClassDefFoundError: " + msg); 1116 } 1117 catch (NoClassDefFoundError expected) { 1118 check_expected(expected, msg, cause_msg); 1119 } 1120 } 1121 1122 static void test_NotInstanceHostGetField() throws Throwable { 1123 System.out.println("Testing for nest-host class that is not an instance class"); 1124 String msg = "Type TestNestmateMembership$TargetNotInstanceHost is not a "+ 1125 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 1126 try { 1127 Caller.getFieldTargetNotInstanceHost(); 1128 throw new Error("Missing IllegalAccessError: " + msg); 1129 } 1130 catch (IllegalAccessError expected) { 1131 check_expected(expected, msg); 1132 } 1133 try { 1134 Caller.getFieldTargetNotInstanceHostReflectively(); 1135 throw new Error("Missing IllegalAccessError: " + msg); 1136 } 1137 catch (IllegalAccessError expected) { 1138 check_expected(expected, msg); 1139 } 1140 try { 1141 Caller.getFieldTargetNotInstanceHostMH(); 1142 throw new Error("Missing IllegalAccessError: " + msg); 1143 } 1144 catch (IllegalAccessError expected) { 1145 check_expected(expected, msg); 1146 } 1147 1148 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 1149 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 1150 try { 1151 CallerNotInstanceHost.getFieldTarget(); 1152 throw new Error("Missing IllegalAccessError: " + msg); 1153 } 1154 catch (IllegalAccessError expected) { 1155 check_expected(expected, msg); 1156 } 1157 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 1158 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 1159 try { 1160 CallerNotInstanceHost.getFieldTargetNotInstanceHost(); 1161 throw new Error("Missing IllegalAccessError: " + msg); 1162 } 1163 catch (IllegalAccessError expected) { 1164 check_expected(expected, msg); 1165 } 1166 } 1167 1168 static void test_NotOurHostGetField() throws Throwable { 1169 System.out.println("Testing for nest-host class that does not list us in its nest"); 1170 String msg = "Type TestNestmateMembership$TargetNotOurHost is not a nest member" + 1171 " of java.lang.Object: current type is not listed as a nest member"; 1172 try { 1173 Caller.getFieldTargetNotOurHost(); 1174 throw new Error("Missing IllegalAccessError: " + msg); 1175 } 1176 catch (IllegalAccessError expected) { 1177 check_expected(expected, msg); 1178 } 1179 try { 1180 Caller.getFieldTargetNotOurHostReflectively(); 1181 throw new Error("Missing IllegalAccessError: " + msg); 1182 } 1183 catch (IllegalAccessError expected) { 1184 check_expected(expected, msg); 1185 } 1186 try { 1187 Caller.getFieldTargetNotOurHostMH(); 1188 throw new Error("Missing IllegalAccessError: " + msg); 1189 } 1190 catch (IllegalAccessError expected) { 1191 check_expected(expected, msg); 1192 } 1193 1194 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 1195 " of java.lang.Object: current type is not listed as a nest member"; 1196 try { 1197 CallerNotOurHost.getFieldTarget(); 1198 throw new Error("Missing IllegalAccessError: " + msg); 1199 } 1200 catch (IllegalAccessError expected) { 1201 check_expected(expected, msg); 1202 } 1203 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 1204 " of java.lang.Object: current type is not listed as a nest member"; 1205 try { 1206 CallerNotOurHost.getFieldTargetNotOurHost(); 1207 throw new Error("Missing IllegalAccessError: " + msg); 1208 } 1209 catch (IllegalAccessError expected) { 1210 check_expected(expected, msg); 1211 } 1212 } 1213 1214 static void test_WrongPackageHostGetField() { 1215 System.out.println("Testing for nest-host and nest-member in different packages"); 1216 String msg = "Type P2.PackagedNestHost2$Member is not a nest member of " + 1217 "P1.PackagedNestHost: types are in different packages"; 1218 try { 1219 P1.PackagedNestHost.doGetField(); 1220 throw new Error("Missing IllegalAccessError: " + msg); 1221 } 1222 catch (IllegalAccessError expected) { 1223 check_expected(expected, msg); 1224 } 1225 try { 1226 P2.PackagedNestHost2.Member.doGetField(); 1227 throw new Error("Missing IllegalAccessError: " + msg); 1228 } 1229 catch (IllegalAccessError expected) { 1230 check_expected(expected, msg); 1231 } 1232 } 1233 1234 static void test_GoodPutField(){ 1235 try { 1236 Caller.putFieldTarget(); 1237 } 1238 catch (Exception e) { 1239 throw new Error("Unexpected exception on good field access: " + e); 1240 } 1241 } 1242 1243 static void test_NoHostPutField() throws Throwable { 1244 System.out.println("Testing for missing nest-host attribute"); 1245 String msg = "tried to access field TestNestmateMembership$TargetNoHost.f" + 1246 " from class TestNestmateMembership$Caller"; 1247 try { 1248 Caller.putFieldTargetNoHost(); 1249 throw new Error("Missing IllegalAccessError: " + msg); 1250 } 1251 catch (IllegalAccessError expected) { 1252 check_expected(expected, msg); 1253 } 1254 msg = "class TestNestmateMembership$Caller cannot access a member of class " + 1255 "TestNestmateMembership$TargetNoHost with modifiers \"private static\""; 1256 try { 1257 Caller.putFieldTargetNoHostReflectively(); 1258 throw new Error("Missing IllegalAccessException: " + msg); 1259 } 1260 catch (IllegalAccessException expected) { 1261 check_expected(expected, msg); 1262 } 1263 msg = "member is private: TestNestmateMembership$TargetNoHost.f/int/putStatic"; 1264 try { 1265 Caller.putFieldTargetNoHostMH(); 1266 throw new Error("Missing IllegalAccessException: " + msg); 1267 } 1268 catch (IllegalAccessException expected) { 1269 check_expected(expected, msg); 1270 } 1271 1272 msg = "tried to access field TestNestmateMembership$Target.f" + 1273 " from class TestNestmateMembership$CallerNoHost"; 1274 try { 1275 CallerNoHost.putFieldTarget(); 1276 throw new Error("Missing IllegalAccessError: " + msg); 1277 } 1278 catch (IllegalAccessError expected) { 1279 check_expected(expected, msg); 1280 } 1281 msg = "tried to access field TestNestmateMembership$TargetNoHost.f" + 1282 " from class TestNestmateMembership$CallerNoHost"; 1283 try { 1284 CallerNoHost.putFieldTargetNoHost(); 1285 throw new Error("Missing IllegalAccessError: " + msg); 1286 } 1287 catch (IllegalAccessError expected) { 1288 check_expected(expected, msg); 1289 } 1290 } 1291 1292 static void test_MissingHostPutField() throws Throwable { 1293 System.out.println("Testing for nest-host class that does not exist"); 1294 String msg = "Unable to load nest-host class (NoTargetMissingHost) of " + 1295 "TestNestmateMembership$TargetMissingHost"; 1296 String cause_msg = "NoTargetMissingHost"; 1297 try { 1298 Caller.putFieldTargetMissingHost(); 1299 throw new Error("Missing NoClassDefFoundError: " + msg); 1300 } 1301 catch (NoClassDefFoundError expected) { 1302 check_expected(expected, msg, cause_msg); 1303 } 1304 try { 1305 Caller.putFieldTargetMissingHostReflectively(); 1306 throw new Error("Missing NoClassDefFoundError: " + msg); 1307 } 1324 throw new Error("Missing NoClassDefFoundError: " + msg); 1325 } 1326 catch (NoClassDefFoundError expected) { 1327 check_expected(expected, msg, cause_msg); 1328 } 1329 msg = "Unable to load nest-host class (NoCallerMissingHost) of "+ 1330 "TestNestmateMembership$CallerMissingHost"; 1331 cause_msg = "NoCallerMissingHost"; 1332 try { 1333 CallerMissingHost.putFieldTargetMissingHost(); 1334 throw new Error("Missing NoClassDefFoundError: " + msg); 1335 } 1336 catch (NoClassDefFoundError expected) { 1337 check_expected(expected, msg, cause_msg); 1338 } 1339 } 1340 1341 static void test_NotInstanceHostPutField() throws Throwable { 1342 System.out.println("Testing for nest-host class that is not an instance class"); 1343 String msg = "Type TestNestmateMembership$TargetNotInstanceHost is not a "+ 1344 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 1345 try { 1346 Caller.putFieldTargetNotInstanceHost(); 1347 throw new Error("Missing IllegalAccessError: " + msg); 1348 } 1349 catch (IllegalAccessError expected) { 1350 check_expected(expected, msg); 1351 } 1352 try { 1353 Caller.putFieldTargetNotInstanceHostReflectively(); 1354 throw new Error("Missing IllegalAccessError: " + msg); 1355 } 1356 catch (IllegalAccessError expected) { 1357 check_expected(expected, msg); 1358 } 1359 try { 1360 Caller.putFieldTargetNotInstanceHostMH(); 1361 throw new Error("Missing IllegalAccessError: " + msg); 1362 } 1363 catch (IllegalAccessError expected) { 1364 check_expected(expected, msg); 1365 } 1366 1367 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 1368 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 1369 try { 1370 CallerNotInstanceHost.putFieldTarget(); 1371 throw new Error("Missing IllegalAccessError: " + msg); 1372 } 1373 catch (IllegalAccessError expected) { 1374 check_expected(expected, msg); 1375 } 1376 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 1377 "nest member of [Ljava.lang.Object;: nest-host is not an instance class"; 1378 try { 1379 CallerNotInstanceHost.putFieldTargetNotInstanceHost(); 1380 throw new Error("Missing IllegalAccessError: " + msg); 1381 } 1382 catch (IllegalAccessError expected) { 1383 check_expected(expected, msg); 1384 } 1385 } 1386 1387 static void test_NotOurHostPutField() throws Throwable { 1388 System.out.println("Testing for nest-host class that does not list us in its nest"); 1389 String msg = "Type TestNestmateMembership$TargetNotOurHost is not a nest member" + 1390 " of java.lang.Object: current type is not listed as a nest member"; 1391 try { 1392 Caller.putFieldTargetNotOurHost(); 1393 throw new Error("Missing IllegalAccessError: " + msg); 1394 } 1395 catch (IllegalAccessError expected) { 1396 check_expected(expected, msg); 1397 } 1398 try { 1399 Caller.putFieldTargetNotOurHostReflectively(); 1400 throw new Error("Missing IllegalAccessError: " + msg); 1401 } 1402 catch (IllegalAccessError expected) { 1403 check_expected(expected, msg); 1404 } 1405 try { 1406 Caller.putFieldTargetNotOurHostMH(); 1407 throw new Error("Missing IllegalAccessError: " + msg); 1408 } 1409 catch (IllegalAccessError expected) { 1410 check_expected(expected, msg); 1411 } 1412 1413 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 1414 " of java.lang.Object: current type is not listed as a nest member"; 1415 try { 1416 CallerNotOurHost.putFieldTarget(); 1417 throw new Error("Missing IllegalAccessError: " + msg); 1418 } 1419 catch (IllegalAccessError expected) { 1420 check_expected(expected, msg); 1421 } 1422 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 1423 " of java.lang.Object: current type is not listed as a nest member"; 1424 try { 1425 CallerNotOurHost.putFieldTargetNotOurHost(); 1426 throw new Error("Missing IllegalAccessError: " + msg); 1427 } 1428 catch (IllegalAccessError expected) { 1429 check_expected(expected, msg); 1430 } 1431 } 1432 1433 static void test_WrongPackageHostPutField() { 1434 System.out.println("Testing for nest-host and nest-member in different packages"); 1435 String msg = "Type P2.PackagedNestHost2$Member is not a nest member of " + 1436 "P1.PackagedNestHost: types are in different packages"; 1437 try { 1438 P1.PackagedNestHost.doPutField(); 1439 throw new Error("Missing IllegalAccessError: " + msg); 1440 } 1441 catch (IllegalAccessError expected) { 1442 check_expected(expected, msg); 1443 } 1444 try { 1445 P2.PackagedNestHost2.Member.doPutField(); 1446 throw new Error("Missing IllegalAccessError: " + msg); 1447 } 1448 catch (IllegalAccessError expected) { 1449 check_expected(expected, msg); 1450 } 1451 } 1452 1453 // utilities 1454 1455 static void check_expected(Throwable expected, String msg) { 1456 if (!expected.getMessage().contains(msg)) { 1457 throw new Error("Wrong " + expected.getClass().getSimpleName() +": \"" + 1458 expected.getMessage() + "\" does not contain \"" + 1459 msg + "\""); 1460 } 1461 System.out.println("OK - got expected exception: " + expected); 1462 } 1463 1464 static void check_expected(Throwable expected, String msg, String cause_msg) { 1465 if (!expected.getMessage().contains(msg)) { 1466 throw new Error("Wrong " + expected.getClass().getSimpleName() +": \"" + 1467 expected.getMessage() + "\" does not contain \"" + 1468 msg + "\""); | 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8046171 27 * @summary Test the various rules for nest members and nest-hosts by 28 * triggering nestmate access checks on all possible paths 29 * @compile TestNestmateMembership.java 30 * PackagedNestHost.java 31 * PackagedNestHost2.java 32 * InvalidNestHost.java 33 * 34 * @compile TargetNoHost.jcod 35 * CallerNoHost.jcod 36 * TargetSelfHost.jcod 37 * CallerSelfHost.jcod 38 * TargetMissingHost.jcod 39 * CallerMissingHost.jcod 40 * TargetNotInstanceHost.jcod 41 * CallerNotInstanceHost.jcod 42 * TargetNotOurHost.jcod 43 * CallerNotOurHost.jcod 44 * PackagedNestHost.jcod 45 * PackagedNestHost2Member.jcod 46 * PackagedNestHostMember.jcod 47 * 48 * @run main/othervm TestNestmateMembership method 49 * @run main/othervm TestNestmateMembership constructor 50 * @run main/othervm TestNestmateMembership getField 51 * @run main/othervm TestNestmateMembership putField 52 * @run main/othervm -Xcomp TestNestmateMembership getField 53 */ 54 55 // We test all the "illegal" relationships between a nest member and its nest-host 56 // except for the case where the name of the nest-member matches the name listed 57 // in the nest-host, but resolves to a different class. There doesn't seem to 58 // be a way to construct that scenario. 59 // For each nested class below there is a corresponding .jcod file which breaks one 60 // of the rules regarding nest membership. For the package related tests we have 61 // additional PackageNestHost*.java sources.[1] 62 // 63 // Note that all the .java files must be compiled in the same step, while all 64 // .jcod files must be compiled in a later step. 65 66 // We test all the different nestmate access check paths: method invocation, constructor 67 // invocations, field get and field put. The test is invoked four times with each using 68 // a different test mode. Plus an extra Xcomp run for field access to hit ciField path. 69 // 70 // As access checking requires resolution and validation of the nest-host of 71 // both the caller class and the target class, we must check that all 72 // combinations of good/bad caller/target are checked for each of the 73 // possible errors: 74 // - no nest-host attribute 75 // - nest-host refers to self 76 // - nest-host class can not be found 77 // - nest-host class is not an instance class (but is in same package) 78 // - class is not a member of nest-host's nest (but is in same package) 79 // - class and nest-host are in different packages 80 // 81 // To provide coverage for reflection and MethodHandle paths through 82 // JVM_AreNestmates, we add reflection/MH accesses to a subset of the tests. 83 // We only need to test one case (for Caller.xxx) as all cases use the same path; further 84 // we don't need to test all failure cases, as all exceptions are equivalent in that regard, 85 // but for good measure we test the four basic error situations (eliding the different 86 // package test for simplicity). 87 // 88 // [1] In earlier versions the package-test was the final check done in nest membership 89 // validation, so we needed actual test classes in different packages that claimed 90 // membership. The final spec requires the package test to be done first, so it can 91 // be trivially tested by using Object as the nest-host. But we leave the explicit 92 // package tests as they are, and adjust the other tests so that a "bad host" is 93 // always in the same package. 94 95 import java.lang.invoke.*; 96 import static java.lang.invoke.MethodHandles.*; 97 import static java.lang.invoke.MethodType.*; 98 99 public class TestNestmateMembership { 100 101 static final MethodType VOID_T = MethodType.methodType(void.class); 102 103 static class Caller { 104 105 private Caller() {} 106 107 private static void m() { 108 System.out.println("Caller.m()"); 109 } 110 111 // direct static method invocations 112 113 public static void invokeTarget() { 114 Target.m(); 115 } 116 public static void invokeTargetNoHost() { 117 TargetNoHost.m(); 118 } 119 public static void invokeTargetSelfHost() { 120 TargetSelfHost.m(); 121 } 122 public static void invokeTargetMissingHost() { 123 TargetMissingHost.m(); 124 } 125 public static void invokeTargetNotInstanceHost() { 126 TargetNotInstanceHost.m(); 127 } 128 public static void invokeTargetNotOurHost() { 129 TargetNotOurHost.m(); 130 } 131 132 // reflective static method invocations 133 134 public static void invokeTargetNoHostReflectively() throws Throwable { 135 TargetNoHost.class.getDeclaredMethod("m", new Class<?>[0]).invoke(null, new Object[0]); 136 } 137 public static void invokeTargetSelfHostReflectively() throws Throwable { 138 TargetSelfHost.class.getDeclaredMethod("m", new Class<?>[0]).invoke(null, new Object[0]); 139 } 140 public static void invokeTargetMissingHostReflectively() throws Throwable { 141 TargetMissingHost.class.getDeclaredMethod("m", new Class<?>[0]).invoke(null, new Object[0]); 142 } 143 public static void invokeTargetNotInstanceHostReflectively() throws Throwable { 144 TargetNotInstanceHost.class.getDeclaredMethod("m", new Class<?>[0]).invoke(null, new Object[0]); 145 } 146 public static void invokeTargetNotOurHostReflectively() throws Throwable { 147 TargetNotOurHost.class.getDeclaredMethod("m", new Class<?>[0]).invoke(null, new Object[0]); 148 } 149 150 // MethodHandle static method lookup (no invoke as the lookup should fail) 151 152 public static void invokeTargetNoHostMH() throws Throwable { 153 MethodHandle mh = lookup().findStatic(TargetNoHost.class, "m", VOID_T); 154 } 155 public static void invokeTargetSelfHostMH() throws Throwable { 156 MethodHandle mh = lookup().findStatic(TargetSelfHost.class, "m", VOID_T); 157 } 158 public static void invokeTargetMissingHostMH() throws Throwable { 159 MethodHandle mh = lookup().findStatic(TargetMissingHost.class, "m", VOID_T); 160 } 161 public static void invokeTargetNotInstanceHostMH() throws Throwable { 162 MethodHandle mh = lookup().findStatic(TargetNotInstanceHost.class, "m", VOID_T); 163 } 164 public static void invokeTargetNotOurHostMH() throws Throwable { 165 MethodHandle mh = lookup().findStatic(TargetNotOurHost.class, "m", VOID_T); 166 } 167 168 169 // direct constructor invocations 170 171 public static void newTarget() { 172 Object o = new Target(); 173 } 174 public static void newTargetNoHost() { 175 Object o = new TargetNoHost(); 176 } 177 public static void newTargetSelfHost() { 178 Object o = new TargetSelfHost(); 179 } 180 public static void newTargetMissingHost() { 181 Object o = new TargetMissingHost(); 182 } 183 public static void newTargetNotInstanceHost() { 184 Object o = new TargetNotInstanceHost(); 185 } 186 public static void newTargetNotOurHost() { 187 Object o = new TargetNotOurHost(); 188 } 189 190 // reflective constructor invocations 191 192 public static void newTargetNoHostReflectively() throws Throwable { 193 Object o = TargetNoHost.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]); 194 } 195 public static void newTargetSelfHostReflectively() throws Throwable { 196 Object o = TargetSelfHost.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]); 197 } 198 public static void newTargetMissingHostReflectively() throws Throwable { 199 Object o = TargetMissingHost.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]); 200 } 201 public static void newTargetNotInstanceHostReflectively() throws Throwable { 202 Object o = TargetNotInstanceHost.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]); 203 } 204 public static void newTargetNotOurHostReflectively() throws Throwable { 205 Object o = TargetNotOurHost.class.getDeclaredConstructor(new Class<?>[0]).newInstance(new Object[0]); 206 } 207 208 // MethodHandle constructor lookup (no invoke as the lookup should fail) 209 210 public static void newTargetNoHostMH() throws Throwable { 211 MethodHandle mh = lookup().findConstructor(TargetNoHost.class, VOID_T); 212 } 213 public static void newTargetSelfHostMH() throws Throwable { 214 MethodHandle mh = lookup().findConstructor(TargetSelfHost.class, VOID_T); 215 } 216 public static void newTargetMissingHostMH() throws Throwable { 217 MethodHandle mh = lookup().findConstructor(TargetMissingHost.class, VOID_T); 218 } 219 public static void newTargetNotInstanceHostMH() throws Throwable { 220 MethodHandle mh = lookup().findConstructor(TargetNotInstanceHost.class, VOID_T); 221 } 222 public static void newTargetNotOurHostMH() throws Throwable { 223 MethodHandle mh = lookup().findConstructor(TargetNotOurHost.class, VOID_T); 224 } 225 226 private static int f; 227 228 // direct field accesses 229 230 public static void getFieldTarget() { 231 int x = Target.f; 232 } 233 public static void getFieldTargetNoHost() { 234 int x = TargetNoHost.f; 235 } 236 public static void getFieldTargetSelfHost() { 237 int x = TargetSelfHost.f; 238 } 239 public static void getFieldTargetMissingHost() { 240 int x = TargetMissingHost.f; 241 } 242 public static void getFieldTargetNotInstanceHost() { 243 int x = TargetNotInstanceHost.f; 244 } 245 public static void getFieldTargetNotOurHost() { 246 int x = TargetNotOurHost.f; 247 } 248 249 public static void putFieldTarget() { 250 Target.f = 42; 251 } 252 public static void putFieldTargetNoHost() { 253 TargetNoHost.f = 42; 254 } 255 public static void putFieldTargetSelfHost() { 256 TargetSelfHost.f = 42; 257 } 258 public static void putFieldTargetMissingHost() { 259 TargetMissingHost.f = 42; 260 } 261 public static void putFieldTargetNotInstanceHost() { 262 TargetNotInstanceHost.f = 42; 263 } 264 public static void putFieldTargetNotOurHost() { 265 TargetNotOurHost.f = 42; 266 } 267 268 // reflective field accesses 269 270 public static void getFieldTargetNoHostReflectively() throws Throwable { 271 int x = TargetNoHost.class.getDeclaredField("f").getInt(null); 272 } 273 public static void getFieldTargetSelfHostReflectively() throws Throwable { 274 int x = TargetSelfHost.class.getDeclaredField("f").getInt(null); 275 } 276 public static void getFieldTargetMissingHostReflectively() throws Throwable { 277 int x = TargetMissingHost.class.getDeclaredField("f").getInt(null); 278 } 279 public static void getFieldTargetNotInstanceHostReflectively() throws Throwable { 280 int x = TargetNotInstanceHost.class.getDeclaredField("f").getInt(null); 281 } 282 public static void getFieldTargetNotOurHostReflectively() throws Throwable { 283 int x = TargetNotOurHost.class.getDeclaredField("f").getInt(null); 284 } 285 286 public static void putFieldTargetNoHostReflectively() throws Throwable { 287 TargetNoHost.class.getDeclaredField("f").setInt(null, 42); 288 } 289 public static void putFieldTargetSelfHostReflectively() throws Throwable { 290 TargetSelfHost.class.getDeclaredField("f").setInt(null, 42); 291 } 292 public static void putFieldTargetMissingHostReflectively() throws Throwable { 293 TargetMissingHost.class.getDeclaredField("f").setInt(null, 42); 294 } 295 public static void putFieldTargetNotInstanceHostReflectively() throws Throwable { 296 TargetNotInstanceHost.class.getDeclaredField("f").setInt(null, 42); 297 } 298 public static void putFieldTargetNotOurHostReflectively() throws Throwable { 299 TargetNotOurHost.class.getDeclaredField("f").setInt(null, 42); 300 } 301 302 // MethodHandle field lookup (no access as the lookup will fail) 303 304 public static void getFieldTargetNoHostMH() throws Throwable { 305 MethodHandle mh = lookup().findStaticGetter(TargetNoHost.class, "f", int.class); 306 } 307 public static void getFieldTargetSelfHostMH() throws Throwable { 308 MethodHandle mh = lookup().findStaticGetter(TargetSelfHost.class, "f", int.class); 309 } 310 public static void getFieldTargetMissingHostMH() throws Throwable { 311 MethodHandle mh = lookup().findStaticGetter(TargetMissingHost.class, "f", int.class); 312 } 313 public static void getFieldTargetNotInstanceHostMH() throws Throwable { 314 MethodHandle mh = lookup().findStaticGetter(TargetNotInstanceHost.class, "f", int.class); 315 } 316 public static void getFieldTargetNotOurHostMH() throws Throwable { 317 MethodHandle mh = lookup().findStaticGetter(TargetNotOurHost.class, "f", int.class); 318 } 319 320 public static void putFieldTargetNoHostMH() throws Throwable { 321 MethodHandle mh = lookup().findStaticSetter(TargetNoHost.class, "f", int.class); 322 } 323 public static void putFieldTargetSelfHostMH() throws Throwable { 324 MethodHandle mh = lookup().findStaticSetter(TargetSelfHost.class, "f", int.class); 325 } 326 public static void putFieldTargetMissingHostMH() throws Throwable { 327 MethodHandle mh = lookup().findStaticSetter(TargetMissingHost.class, "f", int.class); 328 } 329 public static void putFieldTargetNotInstanceHostMH() throws Throwable { 330 MethodHandle mh = lookup().findStaticSetter(TargetNotInstanceHost.class, "f", int.class); 331 } 332 public static void putFieldTargetNotOurHostMH() throws Throwable { 333 MethodHandle mh = lookup().findStaticSetter(TargetNotOurHost.class, "f", int.class); 334 } 335 336 } 337 338 static class CallerNoHost { 339 340 // method invocations 341 342 private static void m() { 343 System.out.println("CallerNoHost.m() - java version"); 344 } 345 public static void invokeTarget() { 363 // field accesses 364 365 private static int f; 366 367 public static void getFieldTarget() { 368 int x = Target.f; 369 } 370 public static void getFieldTargetNoHost() { 371 int x = TargetNoHost.f; 372 } 373 374 public static void putFieldTarget() { 375 Target.f = 42; 376 } 377 public static void putFieldTargetNoHost() { 378 TargetNoHost.f = 42; 379 } 380 381 } 382 383 static class CallerSelfHost { 384 385 // method invocations 386 387 private static void m() { 388 System.out.println("CallerSelfHost.m() - java version"); 389 } 390 public static void invokeTarget() { 391 Target.m(); 392 } 393 public static void invokeTargetSelfHost() { 394 TargetSelfHost.m(); 395 } 396 397 // constructor invocations 398 399 private CallerSelfHost() {} 400 401 public static void newTarget() { 402 Object o = new Target(); 403 } 404 public static void newTargetSelfHost() { 405 Object o = new TargetSelfHost(); 406 } 407 408 // field accesses 409 410 private static int f; 411 412 public static void getFieldTarget() { 413 int x = Target.f; 414 } 415 public static void getFieldTargetSelfHost() { 416 int x = TargetSelfHost.f; 417 } 418 419 public static void putFieldTarget() { 420 Target.f = 42; 421 } 422 public static void putFieldTargetSelfHost() { 423 TargetSelfHost.f = 42; 424 } 425 426 } 427 428 static class CallerMissingHost { 429 String msg = "NoCallerMissingHost"; // for cp entry 430 431 // method invocations 432 433 private static void m() { 434 System.out.println("CallerMissingHost.m() - java version"); 435 } 436 public static void invokeTarget() { 437 Target.m(); 438 } 439 public static void invokeTargetMissingHost() { 440 TargetMissingHost.m(); 441 } 442 443 // constructor invocations 444 445 private CallerMissingHost() {} 446 447 public static void newTarget() { 557 } 558 559 } 560 561 static class Target { 562 private Target() {} 563 private static int f; 564 private static void m() { 565 System.out.println("Target.m()"); 566 } 567 } 568 569 static class TargetNoHost { 570 private TargetNoHost() {} 571 private static int f; 572 private static void m() { 573 System.out.println("TargetNoHost.m() - java version"); 574 } 575 } 576 577 static class TargetSelfHost { 578 private TargetSelfHost() {} 579 private static int f; 580 private static void m() { 581 System.out.println("TargetSelfHost.m() - java version"); 582 } 583 } 584 585 static class TargetMissingHost { 586 String msg = "NoTargetMissingHost"; // for cp entry 587 private TargetMissingHost() {} 588 private static int f; 589 private static void m() { 590 System.out.println("TargetMissingHost.m() - java version"); 591 } 592 } 593 594 static class TargetNotInstanceHost { 595 Object[] oa; // create CP entry to use in jcod change 596 private TargetNotInstanceHost() {} 597 private static int f; 598 private static void m() { 599 System.out.println("TargetNotInstanceHost.m() - java version"); 600 } 601 } 602 603 static class TargetNotOurHost { 604 private TargetNotOurHost() {} 605 private static int f; 606 private static void m() { 607 System.out.println("TargetNotOurHost.m() - java version"); 608 } 609 } 610 611 public static void main(String[] args) throws Throwable { 612 if (args.length < 1) { 613 throw new Error("Test mode argument must be one of: method, constructor, getField or putField"); 614 } 615 switch(args[0]) { 616 case "method": 617 System.out.println("TESTING METHOD INVOCATIONS:"); 618 test_GoodInvoke(); 619 test_NoHostInvoke(); 620 test_SelfHostInvoke(); 621 test_MissingHostInvoke(); 622 test_NotInstanceHostInvoke(); 623 test_NotOurHostInvoke(); 624 test_WrongPackageHostInvoke(); 625 break; 626 case "constructor": 627 System.out.println("TESTING CONSTRUCTOR INVOCATIONS:"); 628 test_GoodConstruct(); 629 test_NoHostConstruct(); 630 test_SelfHostConstruct(); 631 test_MissingHostConstruct(); 632 test_NotInstanceHostConstruct(); 633 test_NotOurHostConstruct(); 634 test_WrongPackageHostConstruct(); 635 break; 636 case "getField": 637 System.out.println("TESTING GETFIELD INVOCATIONS:"); 638 test_GoodGetField(); 639 test_NoHostGetField(); 640 test_SelfHostGetField(); 641 test_MissingHostGetField(); 642 test_NotInstanceHostGetField(); 643 test_NotOurHostGetField(); 644 test_WrongPackageHostGetField(); 645 break; 646 case "putField": 647 System.out.println("TESTING PUTFIELD INVOCATIONS:"); 648 test_GoodPutField(); 649 test_NoHostPutField(); 650 test_SelfHostPutField(); 651 test_MissingHostPutField(); 652 test_NotInstanceHostPutField(); 653 test_NotOurHostPutField(); 654 test_WrongPackageHostPutField(); 655 break; 656 default: 657 throw new Error("Uknown mode: " + args[0] + 658 ". Must be one of: method, constructor, getField or putField"); 659 } 660 } 661 662 static void test_GoodInvoke(){ 663 try { 664 Caller.invokeTarget(); 665 } 666 catch (Exception e) { 667 throw new Error("Unexpected exception on good invocation " + e); 668 } 669 } 670 701 msg = "tried to access method TestNestmateMembership$Target.m()V" + 702 " from class TestNestmateMembership$CallerNoHost"; 703 try { 704 CallerNoHost.invokeTarget(); 705 throw new Error("Missing IllegalAccessError: " + msg); 706 } 707 catch (IllegalAccessError expected) { 708 check_expected(expected, msg); 709 } 710 msg = "tried to access method TestNestmateMembership$TargetNoHost.m()V" + 711 " from class TestNestmateMembership$CallerNoHost"; 712 try { 713 CallerNoHost.invokeTargetNoHost(); 714 throw new Error("Missing IllegalAccessError: " + msg); 715 } 716 catch (IllegalAccessError expected) { 717 check_expected(expected, msg); 718 } 719 } 720 721 static void test_SelfHostInvoke() throws Throwable { 722 System.out.println("Testing for class that lists itself as nest-host"); 723 String msg = "Type TestNestmateMembership$TargetSelfHost is not a nest member" + 724 " of TestNestmateMembership$TargetSelfHost: current type is not listed as a nest member"; 725 try { 726 Caller.invokeTargetSelfHost(); 727 throw new Error("Missing IncompatibleClassChangeError: " + msg); 728 } 729 catch (IncompatibleClassChangeError expected) { 730 check_expected(expected, msg); 731 } 732 try { 733 Caller.invokeTargetSelfHostReflectively(); 734 throw new Error("Missing IncompatibleClassChangeError: " + msg); 735 } 736 catch (IncompatibleClassChangeError expected) { 737 check_expected(expected, msg); 738 } 739 msg = "no such method: TestNestmateMembership$TargetSelfHost.m()void/invokeStatic"; 740 try { 741 Caller.invokeTargetSelfHostMH(); 742 throw new Error("Missing IllegalAccessException: " + msg); 743 } 744 catch (IllegalAccessException expected) { 745 check_expected(expected, msg); 746 } 747 748 msg = "Type TestNestmateMembership$CallerSelfHost is not a nest member" + 749 " of TestNestmateMembership$CallerSelfHost: current type is not listed as a nest member"; 750 try { 751 CallerSelfHost.invokeTarget(); 752 throw new Error("Missing IncompatibleClassChangeError: " + msg); 753 } 754 catch (IncompatibleClassChangeError expected) { 755 check_expected(expected, msg); 756 } 757 msg = "Type TestNestmateMembership$CallerSelfHost is not a nest member" + 758 " of TestNestmateMembership$CallerSelfHost: current type is not listed as a nest member"; 759 try { 760 CallerSelfHost.invokeTargetSelfHost(); 761 throw new Error("Missing IncompatibleClassChangeError: " + msg); 762 } 763 catch (IncompatibleClassChangeError expected) { 764 check_expected(expected, msg); 765 } 766 } 767 768 static void test_MissingHostInvoke() throws Throwable { 769 System.out.println("Testing for nest-host class that does not exist"); 770 String msg = "Unable to load nest-host class (NoTargetMissingHost) of " + 771 "TestNestmateMembership$TargetMissingHost"; 772 String cause_msg = "NoTargetMissingHost"; 773 try { 774 Caller.invokeTargetMissingHost(); 775 throw new Error("Missing NoClassDefFoundError: " + msg); 776 } 777 catch (NoClassDefFoundError expected) { 778 check_expected(expected, msg, cause_msg); 779 } 780 try { 781 Caller.invokeTargetMissingHostReflectively(); 782 throw new Error("Missing NoClassDefFoundError: " + msg); 783 } 784 catch (NoClassDefFoundError expected) { 785 check_expected(expected, msg, cause_msg); 786 } 787 msg = "no such method: TestNestmateMembership$TargetMissingHost.m()void/invokeStatic"; 809 throw new Error("Missing NoClassDefFoundError: " + msg); 810 } 811 catch (NoClassDefFoundError expected) { 812 check_expected(expected, msg, cause_msg); 813 } 814 msg = "Unable to load nest-host class (NoCallerMissingHost) of "+ 815 "TestNestmateMembership$CallerMissingHost"; 816 cause_msg = "NoCallerMissingHost"; 817 try { 818 CallerMissingHost.invokeTargetMissingHost(); 819 throw new Error("Missing NoClassDefFoundError: " + msg); 820 } 821 catch (NoClassDefFoundError expected) { 822 check_expected(expected, msg, cause_msg); 823 } 824 } 825 826 static void test_NotInstanceHostInvoke() throws Throwable { 827 System.out.println("Testing for nest-host class that is not an instance class"); 828 String msg = "Type TestNestmateMembership$TargetNotInstanceHost is not a "+ 829 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 830 try { 831 Caller.invokeTargetNotInstanceHost(); 832 throw new Error("Missing IncompatibleClassChangeError: " + msg); 833 } 834 catch (IncompatibleClassChangeError expected) { 835 check_expected(expected, msg); 836 } 837 try { 838 Caller.invokeTargetNotInstanceHostReflectively(); 839 throw new Error("Missing IncompatibleClassChangeError: " + msg); 840 } 841 catch (IncompatibleClassChangeError expected) { 842 check_expected(expected, msg); 843 } 844 msg = "no such method: TestNestmateMembership$TargetNotInstanceHost.m()void/invokeStatic"; 845 try { 846 Caller.invokeTargetNotInstanceHostMH(); 847 throw new Error("Missing IllegalAccessException: " + msg); 848 } 849 catch (IllegalAccessException expected) { 850 check_expected(expected, msg); 851 } 852 853 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 854 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 855 try { 856 CallerNotInstanceHost.invokeTarget(); 857 throw new Error("Missing IncompatibleClassChangeError: " + msg); 858 } 859 catch (IncompatibleClassChangeError expected) { 860 check_expected(expected, msg); 861 } 862 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 863 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 864 try { 865 CallerNotInstanceHost.invokeTargetNotInstanceHost(); 866 throw new Error("Missing IncompatibleClassChangeError: " + msg); 867 } 868 catch (IncompatibleClassChangeError expected) { 869 check_expected(expected, msg); 870 } 871 } 872 873 static void test_NotOurHostInvoke() throws Throwable { 874 System.out.println("Testing for nest-host class that does not list us in its nest"); 875 String msg = "Type TestNestmateMembership$TargetNotOurHost is not a nest member" + 876 " of InvalidNestHost: current type is not listed as a nest member"; 877 try { 878 Caller.invokeTargetNotOurHost(); 879 throw new Error("Missing IncompatibleClassChangeError: " + msg); 880 } 881 catch (IncompatibleClassChangeError expected) { 882 check_expected(expected, msg); 883 } 884 try { 885 Caller.invokeTargetNotOurHostReflectively(); 886 throw new Error("Missing IncompatibleClassChangeError: " + msg); 887 } 888 catch (IncompatibleClassChangeError expected) { 889 check_expected(expected, msg); 890 } 891 msg = "no such method: TestNestmateMembership$TargetNotOurHost.m()void/invokeStatic"; 892 try { 893 Caller.invokeTargetNotOurHostMH(); 894 throw new Error("Missing IllegalAccessException: " + msg); 895 } 896 catch (IllegalAccessException expected) { 897 check_expected(expected, msg); 898 } 899 900 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 901 " of InvalidNestHost: current type is not listed as a nest member"; 902 try { 903 CallerNotOurHost.invokeTarget(); 904 throw new Error("Missing IncompatibleClassChangeError: " + msg); 905 } 906 catch (IncompatibleClassChangeError expected) { 907 check_expected(expected, msg); 908 } 909 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 910 " of InvalidNestHost: current type is not listed as a nest member"; 911 try { 912 CallerNotOurHost.invokeTargetNotOurHost(); 913 throw new Error("Missing IncompatibleClassChangeError: " + msg); 914 } 915 catch (IncompatibleClassChangeError expected) { 916 check_expected(expected, msg); 917 } 918 } 919 920 static void test_WrongPackageHostInvoke() { 921 System.out.println("Testing for nest-host and nest-member in different packages"); 922 String msg = "Type P2.PackagedNestHost2$Member is not a nest member of " + 923 "P1.PackagedNestHost: types are in different packages"; 924 try { 925 P1.PackagedNestHost.doInvoke(); 926 throw new Error("Missing IncompatibleClassChangeError: " + msg); 927 } 928 catch (IncompatibleClassChangeError expected) { 929 check_expected(expected, msg); 930 } 931 try { 932 P2.PackagedNestHost2.Member.doInvoke(); 933 throw new Error("Missing IncompatibleClassChangeError: " + msg); 934 } 935 catch (IncompatibleClassChangeError expected) { 936 check_expected(expected, msg); 937 } 938 } 939 940 // constructor tests 941 942 static void test_GoodConstruct(){ 943 try { 944 Caller.newTarget(); 945 } 946 catch (Exception e) { 947 throw new Error("Unexpected exception on good construction: " + e); 948 } 949 } 950 951 static void test_NoHostConstruct() throws Throwable { 952 System.out.println("Testing for missing nest-host attribute"); 953 String msg = "tried to access method TestNestmateMembership$TargetNoHost.<init>()V" + 954 " from class TestNestmateMembership$Caller"; 955 try { 956 Caller.newTargetNoHost(); 957 throw new Error("Missing IncompatibleClassChangeError: " + msg); 958 } 959 catch (IncompatibleClassChangeError expected) { 960 check_expected(expected, msg); 961 } 962 msg = "class TestNestmateMembership$Caller cannot access a member of class " + 963 "TestNestmateMembership$TargetNoHost with modifiers \"private\""; 964 try { 965 Caller.newTargetNoHostReflectively(); 966 throw new Error("Missing IllegalAccessException: " + msg); 967 } 968 catch (IllegalAccessException expected) { 969 check_expected(expected, msg); 970 } 971 msg = "no such constructor: TestNestmateMembership$TargetNoHost.<init>()void/newInvokeSpecial"; 972 try { 973 Caller.newTargetNoHostMH(); 974 throw new Error("Missing IllegalAccessException: " + msg); 975 } 976 catch (IllegalAccessException expected) { 977 check_expected(expected, msg); 978 } 979 980 msg = "tried to access method TestNestmateMembership$Target.<init>()V" + 981 " from class TestNestmateMembership$CallerNoHost"; 982 try { 983 CallerNoHost.newTarget(); 984 throw new Error("Missing IncompatibleClassChangeError: " + msg); 985 } 986 catch (IncompatibleClassChangeError expected) { 987 check_expected(expected, msg); 988 } 989 msg = "tried to access method TestNestmateMembership$TargetNoHost.<init>()V" + 990 " from class TestNestmateMembership$CallerNoHost"; 991 try { 992 CallerNoHost.newTargetNoHost(); 993 throw new Error("Missing IncompatibleClassChangeError: " + msg); 994 } 995 catch (IncompatibleClassChangeError expected) { 996 check_expected(expected, msg); 997 } 998 } 999 1000 static void test_SelfHostConstruct() throws Throwable { 1001 System.out.println("Testing for class that lists itself as nest-host"); 1002 String msg = "Type TestNestmateMembership$TargetSelfHost is not a nest member" + 1003 " of TestNestmateMembership$TargetSelfHost: current type is not listed as a nest member"; 1004 try { 1005 Caller.newTargetSelfHost(); 1006 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1007 } 1008 catch (IncompatibleClassChangeError expected) { 1009 check_expected(expected, msg); 1010 } 1011 try { 1012 Caller.newTargetSelfHostReflectively(); 1013 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1014 } 1015 catch (IncompatibleClassChangeError expected) { 1016 check_expected(expected, msg); 1017 } 1018 msg = "no such constructor: TestNestmateMembership$TargetSelfHost.<init>()void/newInvokeSpecial"; 1019 try { 1020 Caller.newTargetSelfHostMH(); 1021 throw new Error("Missing IllegalAccessException: " + msg); 1022 } 1023 catch (IllegalAccessException expected) { 1024 check_expected(expected, msg); 1025 } 1026 1027 msg = "Type TestNestmateMembership$CallerSelfHost is not a nest member" + 1028 " of TestNestmateMembership$CallerSelfHost: current type is not listed as a nest member"; 1029 try { 1030 CallerSelfHost.newTarget(); 1031 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1032 } 1033 catch (IncompatibleClassChangeError expected) { 1034 check_expected(expected, msg); 1035 } 1036 msg = "Type TestNestmateMembership$CallerSelfHost is not a nest member" + 1037 " of TestNestmateMembership$CallerSelfHost: current type is not listed as a nest member"; 1038 try { 1039 CallerSelfHost.newTargetSelfHost(); 1040 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1041 } 1042 catch (IncompatibleClassChangeError expected) { 1043 check_expected(expected, msg); 1044 } 1045 } 1046 1047 static void test_MissingHostConstruct() throws Throwable { 1048 System.out.println("Testing for nest-host class that does not exist"); 1049 String msg = "Unable to load nest-host class (NoTargetMissingHost) of " + 1050 "TestNestmateMembership$TargetMissingHost"; 1051 String cause_msg = "NoTargetMissingHost"; 1052 try { 1053 Caller.newTargetMissingHost(); 1054 throw new Error("Missing NoClassDefFoundError: " + msg); 1055 } 1056 catch (NoClassDefFoundError expected) { 1057 check_expected(expected, msg, cause_msg); 1058 } 1059 try { 1060 Caller.newTargetMissingHostReflectively(); 1061 throw new Error("Missing NoClassDefFoundError: " + msg); 1062 } 1080 throw new Error("Missing NoClassDefFoundError: " + msg); 1081 } 1082 catch (NoClassDefFoundError expected) { 1083 check_expected(expected, msg, cause_msg); 1084 } 1085 msg = "Unable to load nest-host class (NoCallerMissingHost) of "+ 1086 "TestNestmateMembership$CallerMissingHost"; 1087 cause_msg = "NoCallerMissingHost"; 1088 try { 1089 CallerMissingHost.newTargetMissingHost(); 1090 throw new Error("Missing NoClassDefFoundError: " + msg); 1091 } 1092 catch (NoClassDefFoundError expected) { 1093 check_expected(expected, msg, cause_msg); 1094 } 1095 } 1096 1097 static void test_NotInstanceHostConstruct() throws Throwable { 1098 System.out.println("Testing for nest-host class that is not an instance class"); 1099 String msg = "Type TestNestmateMembership$TargetNotInstanceHost is not a "+ 1100 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 1101 try { 1102 Caller.newTargetNotInstanceHost(); 1103 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1104 } 1105 catch (IncompatibleClassChangeError expected) { 1106 check_expected(expected, msg); 1107 } 1108 try { 1109 Caller.newTargetNotInstanceHostReflectively(); 1110 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1111 } 1112 catch (IncompatibleClassChangeError expected) { 1113 check_expected(expected, msg); 1114 } 1115 msg = "no such constructor: TestNestmateMembership$TargetNotInstanceHost.<init>()void/newInvokeSpecial"; 1116 try { 1117 Caller.newTargetNotInstanceHostMH(); 1118 throw new Error("Missing IllegalAccessException: " + msg); 1119 } 1120 catch (IllegalAccessException expected) { 1121 check_expected(expected, msg); 1122 } 1123 1124 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 1125 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 1126 try { 1127 CallerNotInstanceHost.newTarget(); 1128 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1129 } 1130 catch (IncompatibleClassChangeError expected) { 1131 check_expected(expected, msg); 1132 } 1133 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 1134 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 1135 try { 1136 CallerNotInstanceHost.newTargetNotInstanceHost(); 1137 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1138 } 1139 catch (IncompatibleClassChangeError expected) { 1140 check_expected(expected, msg); 1141 } 1142 } 1143 1144 static void test_NotOurHostConstruct() throws Throwable { 1145 System.out.println("Testing for nest-host class that does not list us in its nest"); 1146 String msg = "Type TestNestmateMembership$TargetNotOurHost is not a nest member" + 1147 " of InvalidNestHost: current type is not listed as a nest member"; 1148 try { 1149 Caller.newTargetNotOurHost(); 1150 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1151 } 1152 catch (IncompatibleClassChangeError expected) { 1153 check_expected(expected, msg); 1154 } 1155 try { 1156 Caller.newTargetNotOurHostReflectively(); 1157 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1158 } 1159 catch (IncompatibleClassChangeError expected) { 1160 check_expected(expected, msg); 1161 } 1162 msg = "no such constructor: TestNestmateMembership$TargetNotOurHost.<init>()void/newInvokeSpecial"; 1163 try { 1164 Caller.newTargetNotOurHostMH(); 1165 throw new Error("Missing IllegalAccessException: " + msg); 1166 } 1167 catch (IllegalAccessException expected) { 1168 check_expected(expected, msg); 1169 } 1170 1171 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 1172 " of InvalidNestHost: current type is not listed as a nest member"; 1173 try { 1174 CallerNotOurHost.newTarget(); 1175 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1176 } 1177 catch (IncompatibleClassChangeError expected) { 1178 check_expected(expected, msg); 1179 } 1180 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 1181 " of InvalidNestHost: current type is not listed as a nest member"; 1182 try { 1183 CallerNotOurHost.newTargetNotOurHost(); 1184 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1185 } 1186 catch (IncompatibleClassChangeError expected) { 1187 check_expected(expected, msg); 1188 } 1189 } 1190 1191 static void test_WrongPackageHostConstruct() { 1192 System.out.println("Testing for nest-host and nest-member in different packages"); 1193 String msg = "Type P2.PackagedNestHost2$Member is not a nest member of " + 1194 "P1.PackagedNestHost: types are in different packages"; 1195 try { 1196 P1.PackagedNestHost.doConstruct(); 1197 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1198 } 1199 catch (IncompatibleClassChangeError expected) { 1200 check_expected(expected, msg); 1201 } 1202 try { 1203 P2.PackagedNestHost2.Member.doConstruct(); 1204 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1205 } 1206 catch (IncompatibleClassChangeError expected) { 1207 check_expected(expected, msg); 1208 } 1209 } 1210 1211 // field tests 1212 1213 static void test_GoodGetField(){ 1214 try { 1215 Caller.getFieldTarget(); 1216 } 1217 catch (Exception e) { 1218 throw new Error("Unexpected exception on good field access: " + e); 1219 } 1220 } 1221 1222 static void test_NoHostGetField() throws Throwable { 1223 System.out.println("Testing for missing nest-host attribute"); 1224 String msg = "tried to access field TestNestmateMembership$TargetNoHost.f" + 1225 " from class TestNestmateMembership$Caller"; 1226 try { 1227 Caller.getFieldTargetNoHost(); 1228 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1229 } 1230 catch (IncompatibleClassChangeError expected) { 1231 check_expected(expected, msg); 1232 } 1233 msg = "class TestNestmateMembership$Caller cannot access a member of class " + 1234 "TestNestmateMembership$TargetNoHost with modifiers \"private static\""; 1235 try { 1236 Caller.getFieldTargetNoHostReflectively(); 1237 throw new Error("Missing IllegalAccessException: " + msg); 1238 } 1239 catch (IllegalAccessException expected) { 1240 check_expected(expected, msg); 1241 } 1242 msg = "member is private: TestNestmateMembership$TargetNoHost.f/int/getStatic"; 1243 try { 1244 Caller.getFieldTargetNoHostMH(); 1245 throw new Error("Missing IllegalAccessException: " + msg); 1246 } 1247 catch (IllegalAccessException expected) { 1248 check_expected(expected, msg); 1249 } 1250 1251 msg = "tried to access field TestNestmateMembership$Target.f" + 1252 " from class TestNestmateMembership$CallerNoHost"; 1253 try { 1254 CallerNoHost.getFieldTarget(); 1255 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1256 } 1257 catch (IncompatibleClassChangeError expected) { 1258 check_expected(expected, msg); 1259 } 1260 msg = "tried to access field TestNestmateMembership$TargetNoHost.f" + 1261 " from class TestNestmateMembership$CallerNoHost"; 1262 try { 1263 CallerNoHost.getFieldTargetNoHost(); 1264 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1265 } 1266 catch (IncompatibleClassChangeError expected) { 1267 check_expected(expected, msg); 1268 } 1269 } 1270 1271 static void test_SelfHostGetField() throws Throwable { 1272 System.out.println("Testing for class that lists itself as nest-host"); 1273 String msg = "Type TestNestmateMembership$TargetSelfHost is not a nest member" + 1274 " of TestNestmateMembership$TargetSelfHost: current type is not listed as a nest member"; 1275 try { 1276 Caller.getFieldTargetSelfHost(); 1277 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1278 } 1279 catch (IncompatibleClassChangeError expected) { 1280 check_expected(expected, msg); 1281 } 1282 try { 1283 Caller.getFieldTargetSelfHostReflectively(); 1284 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1285 } 1286 catch (IncompatibleClassChangeError expected) { 1287 check_expected(expected, msg); 1288 } 1289 try { 1290 Caller.getFieldTargetSelfHostMH(); 1291 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1292 } 1293 catch (IncompatibleClassChangeError expected) { 1294 check_expected(expected, msg); 1295 } 1296 1297 msg = "Type TestNestmateMembership$CallerSelfHost is not a nest member" + 1298 " of TestNestmateMembership$CallerSelfHost: current type is not listed as a nest member"; 1299 try { 1300 CallerSelfHost.getFieldTarget(); 1301 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1302 } 1303 catch (IncompatibleClassChangeError expected) { 1304 check_expected(expected, msg); 1305 } 1306 msg = "Type TestNestmateMembership$CallerSelfHost is not a nest member" + 1307 " of TestNestmateMembership$CallerSelfHost: current type is not listed as a nest member"; 1308 try { 1309 CallerSelfHost.getFieldTargetSelfHost(); 1310 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1311 } 1312 catch (IncompatibleClassChangeError expected) { 1313 check_expected(expected, msg); 1314 } 1315 } 1316 1317 static void test_MissingHostGetField() throws Throwable { 1318 System.out.println("Testing for nest-host class that does not exist"); 1319 String msg = "Unable to load nest-host class (NoTargetMissingHost) of " + 1320 "TestNestmateMembership$TargetMissingHost"; 1321 String cause_msg = "NoTargetMissingHost"; 1322 try { 1323 Caller.getFieldTargetMissingHost(); 1324 throw new Error("Missing NoClassDefFoundError: " + msg); 1325 } 1326 catch (NoClassDefFoundError expected) { 1327 check_expected(expected, msg, cause_msg); 1328 } 1329 try { 1330 Caller.getFieldTargetMissingHostReflectively(); 1331 throw new Error("Missing NoClassDefFoundError: " + msg); 1332 } 1349 throw new Error("Missing NoClassDefFoundError: " + msg); 1350 } 1351 catch (NoClassDefFoundError expected) { 1352 check_expected(expected, msg, cause_msg); 1353 } 1354 msg = "Unable to load nest-host class (NoCallerMissingHost) of "+ 1355 "TestNestmateMembership$CallerMissingHost"; 1356 cause_msg = "NoCallerMissingHost"; 1357 try { 1358 CallerMissingHost.getFieldTargetMissingHost(); 1359 throw new Error("Missing NoClassDefFoundError: " + msg); 1360 } 1361 catch (NoClassDefFoundError expected) { 1362 check_expected(expected, msg, cause_msg); 1363 } 1364 } 1365 1366 static void test_NotInstanceHostGetField() throws Throwable { 1367 System.out.println("Testing for nest-host class that is not an instance class"); 1368 String msg = "Type TestNestmateMembership$TargetNotInstanceHost is not a "+ 1369 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 1370 try { 1371 Caller.getFieldTargetNotInstanceHost(); 1372 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1373 } 1374 catch (IncompatibleClassChangeError expected) { 1375 check_expected(expected, msg); 1376 } 1377 try { 1378 Caller.getFieldTargetNotInstanceHostReflectively(); 1379 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1380 } 1381 catch (IncompatibleClassChangeError expected) { 1382 check_expected(expected, msg); 1383 } 1384 try { 1385 Caller.getFieldTargetNotInstanceHostMH(); 1386 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1387 } 1388 catch (IncompatibleClassChangeError expected) { 1389 check_expected(expected, msg); 1390 } 1391 1392 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 1393 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 1394 try { 1395 CallerNotInstanceHost.getFieldTarget(); 1396 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1397 } 1398 catch (IncompatibleClassChangeError expected) { 1399 check_expected(expected, msg); 1400 } 1401 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 1402 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 1403 try { 1404 CallerNotInstanceHost.getFieldTargetNotInstanceHost(); 1405 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1406 } 1407 catch (IncompatibleClassChangeError expected) { 1408 check_expected(expected, msg); 1409 } 1410 } 1411 1412 static void test_NotOurHostGetField() throws Throwable { 1413 System.out.println("Testing for nest-host class that does not list us in its nest"); 1414 String msg = "Type TestNestmateMembership$TargetNotOurHost is not a nest member" + 1415 " of InvalidNestHost: current type is not listed as a nest member"; 1416 try { 1417 Caller.getFieldTargetNotOurHost(); 1418 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1419 } 1420 catch (IncompatibleClassChangeError expected) { 1421 check_expected(expected, msg); 1422 } 1423 try { 1424 Caller.getFieldTargetNotOurHostReflectively(); 1425 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1426 } 1427 catch (IncompatibleClassChangeError expected) { 1428 check_expected(expected, msg); 1429 } 1430 try { 1431 Caller.getFieldTargetNotOurHostMH(); 1432 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1433 } 1434 catch (IncompatibleClassChangeError expected) { 1435 check_expected(expected, msg); 1436 } 1437 1438 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 1439 " of InvalidNestHost: current type is not listed as a nest member"; 1440 try { 1441 CallerNotOurHost.getFieldTarget(); 1442 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1443 } 1444 catch (IncompatibleClassChangeError expected) { 1445 check_expected(expected, msg); 1446 } 1447 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 1448 " of InvalidNestHost: current type is not listed as a nest member"; 1449 try { 1450 CallerNotOurHost.getFieldTargetNotOurHost(); 1451 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1452 } 1453 catch (IncompatibleClassChangeError expected) { 1454 check_expected(expected, msg); 1455 } 1456 } 1457 1458 static void test_WrongPackageHostGetField() { 1459 System.out.println("Testing for nest-host and nest-member in different packages"); 1460 String msg = "Type P2.PackagedNestHost2$Member is not a nest member of " + 1461 "P1.PackagedNestHost: types are in different packages"; 1462 try { 1463 P1.PackagedNestHost.doGetField(); 1464 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1465 } 1466 catch (IncompatibleClassChangeError expected) { 1467 check_expected(expected, msg); 1468 } 1469 try { 1470 P2.PackagedNestHost2.Member.doGetField(); 1471 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1472 } 1473 catch (IncompatibleClassChangeError expected) { 1474 check_expected(expected, msg); 1475 } 1476 } 1477 1478 static void test_GoodPutField(){ 1479 try { 1480 Caller.putFieldTarget(); 1481 } 1482 catch (Exception e) { 1483 throw new Error("Unexpected exception on good field access: " + e); 1484 } 1485 } 1486 1487 static void test_NoHostPutField() throws Throwable { 1488 System.out.println("Testing for missing nest-host attribute"); 1489 String msg = "tried to access field TestNestmateMembership$TargetNoHost.f" + 1490 " from class TestNestmateMembership$Caller"; 1491 try { 1492 Caller.putFieldTargetNoHost(); 1493 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1494 } 1495 catch (IncompatibleClassChangeError expected) { 1496 check_expected(expected, msg); 1497 } 1498 msg = "class TestNestmateMembership$Caller cannot access a member of class " + 1499 "TestNestmateMembership$TargetNoHost with modifiers \"private static\""; 1500 try { 1501 Caller.putFieldTargetNoHostReflectively(); 1502 throw new Error("Missing IllegalAccessException: " + msg); 1503 } 1504 catch (IllegalAccessException expected) { 1505 check_expected(expected, msg); 1506 } 1507 msg = "member is private: TestNestmateMembership$TargetNoHost.f/int/putStatic"; 1508 try { 1509 Caller.putFieldTargetNoHostMH(); 1510 throw new Error("Missing IllegalAccessException: " + msg); 1511 } 1512 catch (IllegalAccessException expected) { 1513 check_expected(expected, msg); 1514 } 1515 1516 msg = "tried to access field TestNestmateMembership$Target.f" + 1517 " from class TestNestmateMembership$CallerNoHost"; 1518 try { 1519 CallerNoHost.putFieldTarget(); 1520 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1521 } 1522 catch (IncompatibleClassChangeError expected) { 1523 check_expected(expected, msg); 1524 } 1525 msg = "tried to access field TestNestmateMembership$TargetNoHost.f" + 1526 " from class TestNestmateMembership$CallerNoHost"; 1527 try { 1528 CallerNoHost.putFieldTargetNoHost(); 1529 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1530 } 1531 catch (IncompatibleClassChangeError expected) { 1532 check_expected(expected, msg); 1533 } 1534 } 1535 1536 static void test_SelfHostPutField() throws Throwable { 1537 System.out.println("Testing for class that lists itself as nest-host"); 1538 String msg = "Type TestNestmateMembership$TargetSelfHost is not a nest member" + 1539 " of TestNestmateMembership$TargetSelfHost: current type is not listed as a nest member"; 1540 try { 1541 Caller.putFieldTargetSelfHost(); 1542 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1543 } 1544 catch (IncompatibleClassChangeError expected) { 1545 check_expected(expected, msg); 1546 } 1547 try { 1548 Caller.putFieldTargetSelfHostReflectively(); 1549 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1550 } 1551 catch (IncompatibleClassChangeError expected) { 1552 check_expected(expected, msg); 1553 } 1554 try { 1555 Caller.putFieldTargetSelfHostMH(); 1556 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1557 } 1558 catch (IncompatibleClassChangeError expected) { 1559 check_expected(expected, msg); 1560 } 1561 1562 msg = "Type TestNestmateMembership$CallerSelfHost is not a nest member" + 1563 " of TestNestmateMembership$CallerSelfHost: current type is not listed as a nest member"; 1564 try { 1565 CallerSelfHost.putFieldTarget(); 1566 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1567 } 1568 catch (IncompatibleClassChangeError expected) { 1569 check_expected(expected, msg); 1570 } 1571 msg = "Type TestNestmateMembership$CallerSelfHost is not a nest member" + 1572 " of TestNestmateMembership$CallerSelfHost: current type is not listed as a nest member"; 1573 try { 1574 CallerSelfHost.putFieldTargetSelfHost(); 1575 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1576 } 1577 catch (IncompatibleClassChangeError expected) { 1578 check_expected(expected, msg); 1579 } 1580 } 1581 1582 static void test_MissingHostPutField() throws Throwable { 1583 System.out.println("Testing for nest-host class that does not exist"); 1584 String msg = "Unable to load nest-host class (NoTargetMissingHost) of " + 1585 "TestNestmateMembership$TargetMissingHost"; 1586 String cause_msg = "NoTargetMissingHost"; 1587 try { 1588 Caller.putFieldTargetMissingHost(); 1589 throw new Error("Missing NoClassDefFoundError: " + msg); 1590 } 1591 catch (NoClassDefFoundError expected) { 1592 check_expected(expected, msg, cause_msg); 1593 } 1594 try { 1595 Caller.putFieldTargetMissingHostReflectively(); 1596 throw new Error("Missing NoClassDefFoundError: " + msg); 1597 } 1614 throw new Error("Missing NoClassDefFoundError: " + msg); 1615 } 1616 catch (NoClassDefFoundError expected) { 1617 check_expected(expected, msg, cause_msg); 1618 } 1619 msg = "Unable to load nest-host class (NoCallerMissingHost) of "+ 1620 "TestNestmateMembership$CallerMissingHost"; 1621 cause_msg = "NoCallerMissingHost"; 1622 try { 1623 CallerMissingHost.putFieldTargetMissingHost(); 1624 throw new Error("Missing NoClassDefFoundError: " + msg); 1625 } 1626 catch (NoClassDefFoundError expected) { 1627 check_expected(expected, msg, cause_msg); 1628 } 1629 } 1630 1631 static void test_NotInstanceHostPutField() throws Throwable { 1632 System.out.println("Testing for nest-host class that is not an instance class"); 1633 String msg = "Type TestNestmateMembership$TargetNotInstanceHost is not a "+ 1634 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 1635 try { 1636 Caller.putFieldTargetNotInstanceHost(); 1637 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1638 } 1639 catch (IncompatibleClassChangeError expected) { 1640 check_expected(expected, msg); 1641 } 1642 try { 1643 Caller.putFieldTargetNotInstanceHostReflectively(); 1644 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1645 } 1646 catch (IncompatibleClassChangeError expected) { 1647 check_expected(expected, msg); 1648 } 1649 try { 1650 Caller.putFieldTargetNotInstanceHostMH(); 1651 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1652 } 1653 catch (IncompatibleClassChangeError expected) { 1654 check_expected(expected, msg); 1655 } 1656 1657 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 1658 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 1659 try { 1660 CallerNotInstanceHost.putFieldTarget(); 1661 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1662 } 1663 catch (IncompatibleClassChangeError expected) { 1664 check_expected(expected, msg); 1665 } 1666 msg = "Type TestNestmateMembership$CallerNotInstanceHost is not a "+ 1667 "nest member of [LInvalidNestHost;: current type is not listed as a nest member"; 1668 try { 1669 CallerNotInstanceHost.putFieldTargetNotInstanceHost(); 1670 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1671 } 1672 catch (IncompatibleClassChangeError expected) { 1673 check_expected(expected, msg); 1674 } 1675 } 1676 1677 static void test_NotOurHostPutField() throws Throwable { 1678 System.out.println("Testing for nest-host class that does not list us in its nest"); 1679 String msg = "Type TestNestmateMembership$TargetNotOurHost is not a nest member" + 1680 " of InvalidNestHost: current type is not listed as a nest member"; 1681 try { 1682 Caller.putFieldTargetNotOurHost(); 1683 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1684 } 1685 catch (IncompatibleClassChangeError expected) { 1686 check_expected(expected, msg); 1687 } 1688 try { 1689 Caller.putFieldTargetNotOurHostReflectively(); 1690 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1691 } 1692 catch (IncompatibleClassChangeError expected) { 1693 check_expected(expected, msg); 1694 } 1695 try { 1696 Caller.putFieldTargetNotOurHostMH(); 1697 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1698 } 1699 catch (IncompatibleClassChangeError expected) { 1700 check_expected(expected, msg); 1701 } 1702 1703 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 1704 " of InvalidNestHost: current type is not listed as a nest member"; 1705 try { 1706 CallerNotOurHost.putFieldTarget(); 1707 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1708 } 1709 catch (IncompatibleClassChangeError expected) { 1710 check_expected(expected, msg); 1711 } 1712 msg = "Type TestNestmateMembership$CallerNotOurHost is not a nest member" + 1713 " of InvalidNestHost: current type is not listed as a nest member"; 1714 try { 1715 CallerNotOurHost.putFieldTargetNotOurHost(); 1716 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1717 } 1718 catch (IncompatibleClassChangeError expected) { 1719 check_expected(expected, msg); 1720 } 1721 } 1722 1723 static void test_WrongPackageHostPutField() { 1724 System.out.println("Testing for nest-host and nest-member in different packages"); 1725 String msg = "Type P2.PackagedNestHost2$Member is not a nest member of " + 1726 "P1.PackagedNestHost: types are in different packages"; 1727 try { 1728 P1.PackagedNestHost.doPutField(); 1729 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1730 } 1731 catch (IncompatibleClassChangeError expected) { 1732 check_expected(expected, msg); 1733 } 1734 try { 1735 P2.PackagedNestHost2.Member.doPutField(); 1736 throw new Error("Missing IncompatibleClassChangeError: " + msg); 1737 } 1738 catch (IncompatibleClassChangeError expected) { 1739 check_expected(expected, msg); 1740 } 1741 } 1742 1743 // utilities 1744 1745 static void check_expected(Throwable expected, String msg) { 1746 if (!expected.getMessage().contains(msg)) { 1747 throw new Error("Wrong " + expected.getClass().getSimpleName() +": \"" + 1748 expected.getMessage() + "\" does not contain \"" + 1749 msg + "\""); 1750 } 1751 System.out.println("OK - got expected exception: " + expected); 1752 } 1753 1754 static void check_expected(Throwable expected, String msg, String cause_msg) { 1755 if (!expected.getMessage().contains(msg)) { 1756 throw new Error("Wrong " + expected.getClass().getSimpleName() +": \"" + 1757 expected.getMessage() + "\" does not contain \"" + 1758 msg + "\""); |