1 /*
   2  * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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() {
 346             Target.m();
 347         }
 348         public static void invokeTargetNoHost() {
 349             TargetNoHost.m();
 350         }
 351 
 352         // constructor invocations
 353 
 354         private CallerNoHost() {}
 355 
 356         public static void newTarget() {
 357             Object o = new Target();
 358         }
 359         public static void newTargetNoHost() {
 360             Object o = new TargetNoHost();
 361         }
 362 
 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() {
 448             Object o = new Target();
 449         }
 450         public static void newTargetMissingHost() {
 451             Object o = new TargetMissingHost();
 452         }
 453 
 454         // field accesses
 455 
 456         private static int f;
 457 
 458         public static void getFieldTarget() {
 459             int x = Target.f;
 460         }
 461         public static void getFieldTargetMissingHost() {
 462             int x = TargetMissingHost.f;
 463         }
 464         public static void putFieldTarget() {
 465             Target.f = 42;
 466         }
 467         public static void putFieldTargetMissingHost() {
 468             TargetMissingHost.f = 42;
 469         }
 470 
 471     }
 472 
 473     static class CallerNotInstanceHost {
 474         Object[] oa; // create CP entry to use in jcod change
 475 
 476         // method invocations
 477 
 478         private static void m() {
 479             System.out.println("CallerNotInstanceHost.m() - java version");
 480         }
 481         public static void invokeTarget() {
 482             Target.m();
 483         }
 484         public static void invokeTargetNotInstanceHost() {
 485             TargetNotInstanceHost.m();
 486         }
 487 
 488         // constructor invocations
 489 
 490         private CallerNotInstanceHost() {}
 491 
 492         public static void newTarget() {
 493             Object o = new Target();
 494         }
 495         public static void newTargetNotInstanceHost() {
 496             Object o = new TargetNotInstanceHost();
 497         }
 498 
 499         // field accesses
 500 
 501         private static int f;
 502 
 503         public static void getFieldTarget() {
 504             int x = Target.f;
 505         }
 506         public static void getFieldTargetNotInstanceHost() {
 507             int x = TargetNotInstanceHost.f;
 508         }
 509         public static void putFieldTarget() {
 510             Target.f = 42;
 511         }
 512         public static void putFieldTargetNotInstanceHost() {
 513             TargetNotInstanceHost.f = 42;
 514         }
 515     }
 516 
 517     static class CallerNotOurHost {
 518 
 519         // method invocations
 520 
 521         private static void m() {
 522             System.out.println("CallerNotOurHost.m() - java version");
 523         }
 524         public static void invokeTarget() {
 525             Target.m();
 526         }
 527         public static void invokeTargetNotOurHost() {
 528             TargetNotOurHost.m();
 529         }
 530 
 531         // constructor invocations
 532 
 533         private CallerNotOurHost() {}
 534 
 535         public static void newTarget() {
 536             Object o = new Target();
 537         }
 538         public static void newTargetNotOurHost() {
 539             Object o = new TargetNotOurHost();
 540         }
 541 
 542         // field accesses
 543 
 544         private static int f;
 545 
 546         public static void getFieldTarget() {
 547             int x = Target.f;
 548         }
 549         public static void getFieldTargetNotOurHost() {
 550             int x = TargetNotOurHost.f;
 551         }
 552         public static void putFieldTarget() {
 553             Target.f = 42;
 554         }
 555         public static void putFieldTargetNotOurHost() {
 556             TargetNotOurHost.f = 42;
 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 
 671     static void test_NoHostInvoke() throws Throwable {
 672         System.out.println("Testing for missing nest-host attribute");
 673         String msg = "class TestNestmateMembership$Caller tried to access " +
 674             "private method 'void TestNestmateMembership$TargetNoHost.m()'";
 675         try {
 676             Caller.invokeTargetNoHost();
 677             throw new Error("Missing IllegalAccessError: " + msg);
 678         }
 679         catch (IllegalAccessError expected) {
 680             check_expected(expected, msg);
 681         }
 682         msg = "TestNestmateMembership$Caller cannot access a member of class " +
 683             "TestNestmateMembership$TargetNoHost with modifiers \"private static\"";
 684         try {
 685             Caller.invokeTargetNoHostReflectively();
 686             throw new Error("Missing IllegalAccessException: " + msg);
 687         }
 688         catch (IllegalAccessException expected) {
 689             check_expected(expected, msg);
 690         }
 691         msg = "no such method: TestNestmateMembership$TargetNoHost.m()void/invokeStatic";
 692         try {
 693             Caller.invokeTargetNoHostMH();
 694             throw new Error("Missing IllegalAccessException: " + msg);
 695         }
 696         catch (IllegalAccessException expected) {
 697             check_expected(expected, msg);
 698         }
 699 
 700         msg = "class TestNestmateMembership$CallerNoHost tried to access private " +
 701             "method 'void TestNestmateMembership$Target.m()'";
 702         try {
 703             CallerNoHost.invokeTarget();
 704             throw new Error("Missing IllegalAccessError: " + msg);
 705         }
 706         catch (IllegalAccessError expected) {
 707             check_expected(expected, msg);
 708         }
 709         msg = "class TestNestmateMembership$CallerNoHost tried to access private " +
 710             "method 'void TestNestmateMembership$TargetNoHost.m()'";
 711         try {
 712             CallerNoHost.invokeTargetNoHost();
 713             throw new Error("Missing IllegalAccessError: " + msg);
 714         }
 715         catch (IllegalAccessError expected) {
 716             check_expected(expected, msg);
 717         }
 718     }
 719 
 720     static void test_SelfHostInvoke() throws Throwable {
 721         System.out.println("Testing for class that lists itself as nest-host");
 722         String msg = "Type TestNestmateMembership$TargetSelfHost (loader: 'app') is not a nest member" +
 723             " of type TestNestmateMembership$TargetSelfHost (loader: 'app'): current type is not listed as a nest member)";
 724         try {
 725             Caller.invokeTargetSelfHost();
 726             throw new Error("Missing IllegalAccessError: " + msg);
 727         }
 728         catch (IllegalAccessError expected) {
 729             check_expected(expected, msg);
 730         }
 731         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
 732             "TestNestmateMembership$TargetSelfHost with modifiers \"private static\"";
 733         try {
 734             Caller.invokeTargetSelfHostReflectively();
 735             throw new Error("Missing IllegalAccessError: " + msg);
 736         }
 737         catch (IllegalAccessException expected) {
 738             check_expected(expected, msg);
 739         }
 740         msg = "no such method: TestNestmateMembership$TargetSelfHost.m()void/invokeStatic";
 741         try {
 742             Caller.invokeTargetSelfHostMH();
 743             throw new Error("Missing IllegalAccessException: " + msg);
 744         }
 745         catch (IllegalAccessException expected) {
 746             check_expected(expected, msg);
 747         }
 748 
 749         msg = "Type TestNestmateMembership$CallerSelfHost (loader: 'app') is not a nest member" +
 750             " of type TestNestmateMembership$CallerSelfHost (loader: 'app'): current type is not listed as a nest member";
 751         try {
 752             CallerSelfHost.invokeTarget();
 753             throw new Error("Missing IllegalAccessError: " + msg);
 754         }
 755         catch (IllegalAccessError expected) {
 756             check_expected(expected, msg);
 757         }
 758         try {
 759             CallerSelfHost.invokeTargetSelfHost();
 760             throw new Error("Missing IllegalAccessError: " + msg);
 761         }
 762         catch (IllegalAccessError expected) {
 763             check_expected(expected, msg);
 764         }
 765     }
 766 
 767     static void test_MissingHostInvoke() throws Throwable {
 768         System.out.println("Testing for nest-host class that does not exist");
 769         String msg = "Nest host resolution of TestNestmateMembership$TargetMissingHost with host" +
 770             " NoTargetMissingHost failed: java.lang.NoClassDefFoundError: NoTargetMissingHost";
 771         try {
 772             Caller.invokeTargetMissingHost();
 773             throw new Error("Missing IllegalAccessError: " + msg);
 774         }
 775         catch (IllegalAccessError expected) {
 776             check_expected(expected, msg);
 777         }
 778         msg = "class TestNestmateMembership$Caller cannot access a member of class" +
 779             " TestNestmateMembership$TargetMissingHost with modifiers \"private static\"";
 780         try {
 781             Caller.invokeTargetMissingHostReflectively();
 782             throw new Error("Missing IllegalAccessException: " + msg);
 783         }
 784         catch (IllegalAccessException expected) {
 785             check_expected(expected, msg);
 786         }
 787         msg = "no such method: TestNestmateMembership$TargetMissingHost.m()void/invokeStatic";
 788         try {
 789             Caller.invokeTargetMissingHostMH();
 790             throw new Error("Missing IllegalAccessException: " + msg);
 791         }
 792         catch (IllegalAccessException expected) {
 793             check_expected(expected, msg);
 794         }
 795         msg = "no such method: TestNestmateMembership$TargetMissingHost.m()void/invokeStatic";
 796         try {
 797             Caller.invokeTargetMissingHostMH();
 798             throw new Error("Missing IllegalAccessException: " + msg);
 799         }
 800         catch (IllegalAccessException expected) {
 801             check_expected(expected, msg);
 802         }
 803 
 804         msg = "Nest host resolution of TestNestmateMembership$CallerMissingHost with host" +
 805             " NoCallerMissingHost failed: java.lang.NoClassDefFoundError: NoCallerMissingHost";
 806         try {
 807             CallerMissingHost.invokeTarget();
 808             throw new Error("Missing IllegalAccessError: " + msg);
 809         }
 810         catch (IllegalAccessError expected) {
 811             check_expected(expected, msg);
 812         }
 813         try {
 814             CallerMissingHost.invokeTargetMissingHost();
 815             throw new Error("Missing IllegalAccessError: " + msg);
 816         }
 817         catch (IllegalAccessError expected) {
 818             check_expected(expected, msg);
 819         }
 820     }
 821 
 822     static void test_NotInstanceHostInvoke() throws Throwable {
 823         System.out.println("Testing for nest-host class that is not an instance class");
 824         String msg = "Type TestNestmateMembership$TargetNotInstanceHost (loader: 'app') is not a "+
 825             "nest member of type [LInvalidNestHost; (loader: 'app'): host is not an instance class";
 826         try {
 827             Caller.invokeTargetNotInstanceHost();
 828             throw new Error("Missing IllegalAccessError: " + msg);
 829         }
 830         catch (IllegalAccessError expected) {
 831             check_expected(expected, msg);
 832         }
 833         msg = "class TestNestmateMembership$Caller cannot access a member of class "+
 834             "TestNestmateMembership$TargetNotInstanceHost with modifiers \"private static\"";
 835         try {
 836             Caller.invokeTargetNotInstanceHostReflectively();
 837             throw new Error("Missing IllegalAccessException: " + msg);
 838         }
 839         catch (IllegalAccessException expected) {
 840             check_expected(expected, msg);
 841         }
 842         msg = "no such method: TestNestmateMembership$TargetNotInstanceHost.m()void/invokeStatic";
 843         try {
 844             Caller.invokeTargetNotInstanceHostMH();
 845             throw new Error("Missing IllegalAccessException: " + msg);
 846         }
 847         catch (IllegalAccessException expected) {
 848             check_expected(expected, msg);
 849         }
 850 
 851         msg = "Type TestNestmateMembership$CallerNotInstanceHost (loader: 'app') is not a " +
 852             "nest member of type [LInvalidNestHost; (loader: 'app'): host is not an instance class";
 853         try {
 854             CallerNotInstanceHost.invokeTarget();
 855             throw new Error("Missing IllegalAccessError: " + msg);
 856         }
 857         catch (IllegalAccessError expected) {
 858             check_expected(expected, msg);
 859         }
 860         try {
 861             CallerNotInstanceHost.invokeTargetNotInstanceHost();
 862             throw new Error("Missing IllegalAccessError: " + msg);
 863         }
 864         catch (IllegalAccessError expected) {
 865             check_expected(expected, msg);
 866         }
 867     }
 868 
 869     static void test_NotOurHostInvoke() throws Throwable {
 870         System.out.println("Testing for nest-host class that does not list us in its nest");
 871         String msg = "Type TestNestmateMembership$TargetNotOurHost (loader: 'app') is not a " +
 872             "nest member of type InvalidNestHost (loader: 'app'): current type is not listed as a nest member";
 873         try {
 874             Caller.invokeTargetNotOurHost();
 875             throw new Error("Missing IllegalAccessError: " + msg);
 876         }
 877         catch (IllegalAccessError expected) {
 878             check_expected(expected, msg);
 879         }
 880         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
 881             "TestNestmateMembership$TargetNotOurHost with modifiers \"private static\"";
 882         try {
 883             Caller.invokeTargetNotOurHostReflectively();
 884             throw new Error("Missing IllegalAccessException: " + msg);
 885         }
 886         catch (IllegalAccessException expected) {
 887             check_expected(expected, msg);
 888         }
 889         msg = "no such method: TestNestmateMembership$TargetNotOurHost.m()void/invokeStatic";
 890         try {
 891             Caller.invokeTargetNotOurHostMH();
 892             throw new Error("Missing IllegalAccessException: " + msg);
 893         }
 894         catch (IllegalAccessException expected) {
 895             check_expected(expected, msg);
 896         }
 897 
 898         msg = "Type TestNestmateMembership$CallerNotOurHost (loader: 'app') is not a nest member" +
 899             " of type InvalidNestHost (loader: 'app'): current type is not listed as a nest member";
 900         try {
 901             CallerNotOurHost.invokeTarget();
 902             throw new Error("Missing IllegalAccessError: " + msg);
 903         }
 904         catch (IllegalAccessError expected) {
 905             check_expected(expected, msg);
 906         }
 907         try {
 908             CallerNotOurHost.invokeTargetNotOurHost();
 909             throw new Error("Missing IllegalAccessError: " + msg);
 910         }
 911         catch (IllegalAccessError expected) {
 912             check_expected(expected, msg);
 913         }
 914     }
 915 
 916     static void test_WrongPackageHostInvoke() {
 917         System.out.println("Testing for nest-host and nest-member in different packages");
 918         String msg = "Type P2.PackagedNestHost2$Member (loader: 'app') is not a nest member of " +
 919             "type P1.PackagedNestHost (loader: 'app'): types are in different packages";
 920         try {
 921             P1.PackagedNestHost.doInvoke();
 922             throw new Error("Missing IllegalAccessError: " + msg);
 923         }
 924         catch (IllegalAccessError expected) {
 925             check_expected(expected, msg);
 926         }
 927         try {
 928             P2.PackagedNestHost2.Member.doInvoke();
 929             throw new Error("Missing IllegalAccessError: " + msg);
 930         }
 931         catch (IllegalAccessError expected) {
 932             check_expected(expected, msg);
 933         }
 934     }
 935 
 936     // constructor tests
 937 
 938    static void test_GoodConstruct(){
 939         try {
 940             Caller.newTarget();
 941         }
 942         catch (Exception e) {
 943             throw new Error("Unexpected exception on good construction: " + e);
 944         }
 945     }
 946 
 947     static void test_NoHostConstruct() throws Throwable {
 948         System.out.println("Testing for missing nest-host attribute");
 949         String msg = "class TestNestmateMembership$Caller tried to access private " +
 950             "method 'void TestNestmateMembership$TargetNoHost.<init>()'";
 951         try {
 952             Caller.newTargetNoHost();
 953             throw new Error("Missing IllegalAccessError: " + msg);
 954         }
 955         catch (IllegalAccessError expected) {
 956             check_expected(expected, msg);
 957         }
 958         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
 959             "TestNestmateMembership$TargetNoHost with modifiers \"private\"";
 960         try {
 961             Caller.newTargetNoHostReflectively();
 962             throw new Error("Missing IllegalAccessException: " + msg);
 963         }
 964         catch (IllegalAccessException expected) {
 965             check_expected(expected, msg);
 966         }
 967         msg = "no such constructor: TestNestmateMembership$TargetNoHost.<init>()void/newInvokeSpecial";
 968         try {
 969             Caller.newTargetNoHostMH();
 970             throw new Error("Missing IllegalAccessException: " + msg);
 971         }
 972         catch (IllegalAccessException expected) {
 973             check_expected(expected, msg);
 974         }
 975 
 976         msg = "class TestNestmateMembership$CallerNoHost tried to access private " +
 977             "method 'void TestNestmateMembership$Target.<init>()'";
 978         try {
 979             CallerNoHost.newTarget();
 980             throw new Error("Missing IllegalAccessError: " + msg);
 981         }
 982         catch (IllegalAccessError expected) {
 983             check_expected(expected, msg);
 984         }
 985         msg = "class TestNestmateMembership$CallerNoHost tried to access private " +
 986             "method 'void TestNestmateMembership$TargetNoHost.<init>()'";
 987         try {
 988             CallerNoHost.newTargetNoHost();
 989             throw new Error("Missing IllegalAccessError: " + msg);
 990         }
 991         catch (IllegalAccessError expected) {
 992             check_expected(expected, msg);
 993         }
 994     }
 995 
 996     static void test_SelfHostConstruct() throws Throwable {
 997         System.out.println("Testing for class that lists itself as nest-host");
 998         String msg = "Type TestNestmateMembership$TargetSelfHost (loader: 'app') is not a nest member" +
 999             " of type TestNestmateMembership$TargetSelfHost (loader: 'app'): current type is not listed as a nest member";
1000         try {
1001             Caller.newTargetSelfHost();
1002             throw new Error("Missing IllegalAccessError: " + msg);
1003         }
1004         catch (IllegalAccessError expected) {
1005             check_expected(expected, msg);
1006         }
1007         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1008             "TestNestmateMembership$TargetSelfHost with modifiers \"private\"";
1009         try {
1010             Caller.newTargetSelfHostReflectively();
1011             throw new Error("Missing IllegalAccessException: " + msg);
1012         }
1013         catch (IllegalAccessException expected) {
1014             check_expected(expected, msg);
1015         }
1016         msg = "no such constructor: TestNestmateMembership$TargetSelfHost.<init>()void/newInvokeSpecial";
1017         try {
1018             Caller.newTargetSelfHostMH();
1019             throw new Error("Missing IllegalAccessException: " + msg);
1020         }
1021         catch (IllegalAccessException expected) {
1022             check_expected(expected, msg);
1023         }
1024 
1025         msg = "Type TestNestmateMembership$CallerSelfHost (loader: 'app') is not a nest member" +
1026             " of type TestNestmateMembership$CallerSelfHost (loader: 'app'): current type is not listed as a nest member";
1027         try {
1028             CallerSelfHost.newTarget();
1029             throw new Error("Missing IllegalAccessError: " + msg);
1030         }
1031         catch (IllegalAccessError expected) {
1032             check_expected(expected, msg);
1033         }
1034         try {
1035             CallerSelfHost.newTargetSelfHost();
1036             throw new Error("Missing IllegalAccessError: " + msg);
1037         }
1038         catch (IllegalAccessError expected) {
1039             check_expected(expected, msg);
1040         }
1041     }
1042 
1043     static void test_MissingHostConstruct() throws Throwable {
1044         System.out.println("Testing for nest-host class that does not exist");
1045         String msg = "Nest host resolution of TestNestmateMembership$TargetMissingHost with " +
1046             "host NoTargetMissingHost failed: java.lang.NoClassDefFoundError: NoTargetMissingHost";
1047         try {
1048             Caller.newTargetMissingHost();
1049             throw new Error("Missing IllegalAccessError: " + msg);
1050         }
1051         catch (IllegalAccessError expected) {
1052             check_expected(expected, msg);
1053         }
1054         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1055             "TestNestmateMembership$TargetMissingHost with modifiers \"private\"";
1056         try {
1057             Caller.newTargetMissingHostReflectively();
1058             throw new Error("Missing IllegalAccessException: " + msg);
1059         }
1060         catch (IllegalAccessException expected) {
1061             check_expected(expected, msg);
1062         }
1063         msg = "no such constructor: TestNestmateMembership$TargetMissingHost.<init>()void/newInvokeSpecial";
1064         try {
1065             Caller.newTargetMissingHostMH();
1066             throw new Error("Missing IllegalAccessException: " + msg);
1067         }
1068         catch (IllegalAccessException expected) {
1069             check_expected(expected, msg);
1070         }
1071 
1072         msg = "Nest host resolution of TestNestmateMembership$CallerMissingHost with host " +
1073             "NoCallerMissingHost failed: java.lang.NoClassDefFoundError: NoCallerMissingHost";
1074         try {
1075             CallerMissingHost.newTarget();
1076             throw new Error("Missing IllegalAccessError: " + msg);
1077         }
1078         catch (IllegalAccessError expected) {
1079             check_expected(expected, msg);
1080         }
1081         try {
1082             CallerMissingHost.newTargetMissingHost();
1083             throw new Error("Missing IllegalAccessError: " + msg);
1084         }
1085         catch (IllegalAccessError expected) {
1086             check_expected(expected, msg);
1087         }
1088     }
1089 
1090     static void test_NotInstanceHostConstruct() throws Throwable {
1091         System.out.println("Testing for nest-host class that is not an instance class");
1092         String msg = "Type TestNestmateMembership$TargetNotInstanceHost (loader: 'app') is not a "+
1093             "nest member of type [LInvalidNestHost; (loader: 'app'): host is not an instance class";
1094         try {
1095             Caller.newTargetNotInstanceHost();
1096             throw new Error("Missing IllegalAccessError: " + msg);
1097         }
1098         catch (IllegalAccessError expected) {
1099             check_expected(expected, msg);
1100         }
1101         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1102             "TestNestmateMembership$TargetNotInstanceHost with modifiers \"private\"";
1103         try {
1104             Caller.newTargetNotInstanceHostReflectively();
1105             throw new Error("Missing IllegalAccessException: " + msg);
1106         }
1107         catch (IllegalAccessException expected) {
1108             check_expected(expected, msg);
1109         }
1110         msg = "no such constructor: TestNestmateMembership$TargetNotInstanceHost.<init>()void/newInvokeSpecial";
1111         try {
1112             Caller.newTargetNotInstanceHostMH();
1113             throw new Error("Missing IllegalAccessException: " + msg);
1114         }
1115         catch (IllegalAccessException expected) {
1116             check_expected(expected, msg);
1117         }
1118 
1119         msg = "Type TestNestmateMembership$CallerNotInstanceHost (loader: 'app') is not a "+
1120             "nest member of type [LInvalidNestHost; (loader: 'app'): host is not an instance class";
1121         try {
1122             CallerNotInstanceHost.newTarget();
1123             throw new Error("Missing IllegalAccessError: " + msg);
1124         }
1125         catch (IllegalAccessError expected) {
1126             check_expected(expected, msg);
1127         }
1128         try {
1129             CallerNotInstanceHost.newTargetNotInstanceHost();
1130             throw new Error("Missing IllegalAccessError: " + msg);
1131         }
1132         catch (IllegalAccessError expected) {
1133             check_expected(expected, msg);
1134         }
1135     }
1136 
1137     static void test_NotOurHostConstruct() throws Throwable {
1138         System.out.println("Testing for nest-host class that does not list us in its nest");
1139         String msg = "Type TestNestmateMembership$TargetNotOurHost (loader: 'app') is not a nest member" +
1140             " of type InvalidNestHost (loader: 'app'): current type is not listed as a nest member";
1141         try {
1142             Caller.newTargetNotOurHost();
1143             throw new Error("Missing IllegalAccessError: " + msg);
1144         }
1145         catch (IllegalAccessError expected) {
1146             check_expected(expected, msg);
1147         }
1148         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1149             "TestNestmateMembership$TargetNotOurHost with modifiers \"private\"";
1150         try {
1151             Caller.newTargetNotOurHostReflectively();
1152             throw new Error("Missing IllegalAccessException: " + msg);
1153         }
1154         catch (IllegalAccessException expected) {
1155             check_expected(expected, msg);
1156         }
1157         msg = "no such constructor: TestNestmateMembership$TargetNotOurHost.<init>()void/newInvokeSpecial";
1158         try {
1159             Caller.newTargetNotOurHostMH();
1160             throw new Error("Missing IllegalAccessException: " + msg);
1161         }
1162         catch (IllegalAccessException expected) {
1163             check_expected(expected, msg);
1164         }
1165 
1166         msg = "Type TestNestmateMembership$CallerNotOurHost (loader: 'app') is not a nest member" +
1167             " of type InvalidNestHost (loader: 'app'): current type is not listed as a nest member";
1168         try {
1169             CallerNotOurHost.newTarget();
1170             throw new Error("Missing IllegalAccessError: " + msg);
1171         }
1172         catch (IllegalAccessError expected) {
1173             check_expected(expected, msg);
1174         }
1175         msg = "Type TestNestmateMembership$CallerNotOurHost (loader: 'app') is not a nest member" +
1176             " of type InvalidNestHost (loader: 'app'): current type is not listed as a nest member";
1177         try {
1178             CallerNotOurHost.newTargetNotOurHost();
1179             throw new Error("Missing IllegalAccessError: " + msg);
1180         }
1181         catch (IllegalAccessError expected) {
1182             check_expected(expected, msg);
1183         }
1184     }
1185 
1186     static void test_WrongPackageHostConstruct() {
1187         System.out.println("Testing for nest-host and nest-member in different packages");
1188         String msg = "Type P2.PackagedNestHost2$Member (loader: 'app') is not a nest member of " +
1189             "type P1.PackagedNestHost (loader: 'app'): types are in different packages";
1190         try {
1191             P1.PackagedNestHost.doConstruct();
1192             throw new Error("Missing IllegalAccessError: " + msg);
1193         }
1194         catch (IllegalAccessError expected) {
1195             check_expected(expected, msg);
1196         }
1197         try {
1198             P2.PackagedNestHost2.Member.doConstruct();
1199             throw new Error("Missing IllegalAccessError: " + msg);
1200         }
1201         catch (IllegalAccessError expected) {
1202             check_expected(expected, msg);
1203         }
1204     }
1205 
1206     // field tests
1207 
1208    static void test_GoodGetField(){
1209         try {
1210             Caller.getFieldTarget();
1211         }
1212         catch (Exception e) {
1213             throw new Error("Unexpected exception on good field access: " + e);
1214         }
1215     }
1216 
1217     static void test_NoHostGetField() throws Throwable {
1218         System.out.println("Testing for missing nest-host attribute");
1219         String msg = "class TestNestmateMembership$Caller tried to access private " +
1220             "field TestNestmateMembership$TargetNoHost.f";
1221         try {
1222             Caller.getFieldTargetNoHost();
1223             throw new Error("Missing IllegalAccessError: " + msg);
1224         }
1225         catch (IllegalAccessError expected) {
1226             check_expected(expected, msg);
1227         }
1228         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1229             "TestNestmateMembership$TargetNoHost with modifiers \"private static\"";
1230         try {
1231             Caller.getFieldTargetNoHostReflectively();
1232             throw new Error("Missing IllegalAccessException: " + msg);
1233         }
1234         catch (IllegalAccessException expected) {
1235             check_expected(expected, msg);
1236         }
1237         msg = "member is private: TestNestmateMembership$TargetNoHost.f/int/getStatic";
1238         try {
1239             Caller.getFieldTargetNoHostMH();
1240             throw new Error("Missing IllegalAccessException: " + msg);
1241         }
1242         catch (IllegalAccessException expected) {
1243             check_expected(expected, msg);
1244         }
1245 
1246         msg = "class TestNestmateMembership$CallerNoHost tried to access private " +
1247             "field TestNestmateMembership$Target.f";
1248         try {
1249             CallerNoHost.getFieldTarget();
1250             throw new Error("Missing IllegalAccessError: " + msg);
1251         }
1252         catch (IllegalAccessError expected) {
1253             check_expected(expected, msg);
1254         }
1255         msg = "class TestNestmateMembership$CallerNoHost tried to access private " +
1256             "field TestNestmateMembership$TargetNoHost.f";
1257         try {
1258             CallerNoHost.getFieldTargetNoHost();
1259             throw new Error("Missing IllegalAccessError: " + msg);
1260         }
1261         catch (IllegalAccessError expected) {
1262             check_expected(expected, msg);
1263         }
1264     }
1265 
1266     static void test_SelfHostGetField() throws Throwable {
1267         System.out.println("Testing for class that lists itself as nest-host");
1268         String msg = "Type TestNestmateMembership$TargetSelfHost (loader: 'app') is not a nest member" +
1269             " of type TestNestmateMembership$TargetSelfHost (loader: 'app'): current type is not listed as a nest member";
1270         try {
1271             Caller.getFieldTargetSelfHost();
1272             throw new Error("Missing IllegalAccessError: " + msg);
1273         }
1274         catch (IllegalAccessError expected) {
1275             check_expected(expected, msg);
1276         }
1277         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1278             "TestNestmateMembership$TargetSelfHost with modifiers \"private static\"";
1279         try {
1280             Caller.getFieldTargetSelfHostReflectively();
1281             throw new Error("Missing IllegalAccessException: " + msg);
1282         }
1283         catch (IllegalAccessException expected) {
1284             check_expected(expected, msg);
1285         }
1286         msg = "member is private: TestNestmateMembership$TargetSelfHost.f/int/getStatic, " +
1287             "from class TestNestmateMembership$Caller";
1288         try {
1289             Caller.getFieldTargetSelfHostMH();
1290             throw new Error("Missing IllegalAccessException: " + msg);
1291         }
1292         catch (IllegalAccessException expected) {
1293             check_expected(expected, msg);
1294         }
1295 
1296         msg = "Type TestNestmateMembership$CallerSelfHost (loader: 'app') is not a nest member" +
1297             " of type TestNestmateMembership$CallerSelfHost (loader: 'app'): current type is not listed as a nest member";
1298         try {
1299             CallerSelfHost.getFieldTarget();
1300             throw new Error("Missing IllegalAccessError: " + msg);
1301         }
1302         catch (IllegalAccessError expected) {
1303             check_expected(expected, msg);
1304         }
1305         try {
1306             CallerSelfHost.getFieldTargetSelfHost();
1307             throw new Error("Missing IllegalAccessError: " + msg);
1308         }
1309         catch (IllegalAccessError expected) {
1310             check_expected(expected, msg);
1311         }
1312     }
1313 
1314     static void test_MissingHostGetField() throws Throwable {
1315         System.out.println("Testing for nest-host class that does not exist");
1316         String msg = "Nest host resolution of TestNestmateMembership$TargetMissingHost with " +
1317             "host NoTargetMissingHost failed: java.lang.NoClassDefFoundError: NoTargetMissingHost";
1318         try {
1319             Caller.getFieldTargetMissingHost();
1320             throw new Error("Missing IllegalAccessError: " + msg);
1321         }
1322         catch (IllegalAccessError expected) {
1323             check_expected(expected, msg);
1324         }
1325         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1326             "TestNestmateMembership$TargetMissingHost with modifiers \"private static\"";
1327         try {
1328             Caller.getFieldTargetMissingHostReflectively();
1329             throw new Error("Missing IllegalAccessException: " + msg);
1330         }
1331         catch (IllegalAccessException expected) {
1332             check_expected(expected, msg);
1333         }
1334         msg = "member is private: TestNestmateMembership$TargetMissingHost.f/int/getStatic, " +
1335             "from class TestNestmateMembership$Caller";
1336         try {
1337             Caller.getFieldTargetMissingHostMH();
1338             throw new Error("Missing IllegalAccessException: " + msg);
1339         }
1340         catch (IllegalAccessException expected) {
1341             check_expected(expected, msg);
1342         }
1343 
1344         msg = "Nest host resolution of TestNestmateMembership$CallerMissingHost with " +
1345             "host NoCallerMissingHost failed: java.lang.NoClassDefFoundError: NoCallerMissingHost";
1346         try {
1347             CallerMissingHost.getFieldTarget();
1348             throw new Error("Missing IllegalAccessError: " + msg);
1349         }
1350         catch (IllegalAccessError expected) {
1351             check_expected(expected, msg);
1352         }
1353         try {
1354             CallerMissingHost.getFieldTargetMissingHost();
1355             throw new Error("Missing IllegalAccessError: " + msg);
1356         }
1357         catch (IllegalAccessError expected) {
1358             check_expected(expected, msg);
1359         }
1360     }
1361 
1362     static void test_NotInstanceHostGetField() throws Throwable {
1363         System.out.println("Testing for nest-host class that is not an instance class");
1364         String msg = "Type TestNestmateMembership$TargetNotInstanceHost (loader: 'app') is not a "+
1365             "nest member of type [LInvalidNestHost; (loader: 'app'): host is not an instance class";
1366         try {
1367             Caller.getFieldTargetNotInstanceHost();
1368             throw new Error("Missing IllegalAccessError: " + msg);
1369         }
1370         catch (IllegalAccessError expected) {
1371             check_expected(expected, msg);
1372         }
1373         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1374             "TestNestmateMembership$TargetNotInstanceHost with modifiers \"private static\"";
1375         try {
1376             Caller.getFieldTargetNotInstanceHostReflectively();
1377             throw new Error("Missing IllegalAccessException: " + msg);
1378         }
1379         catch (IllegalAccessException expected) {
1380             check_expected(expected, msg);
1381         }
1382         msg = "member is private: TestNestmateMembership$TargetNotInstanceHost.f/int/getStatic, " +
1383             "from class TestNestmateMembership$Caller";
1384         try {
1385             Caller.getFieldTargetNotInstanceHostMH();
1386             throw new Error("Missing IllegalAccessException: " + msg);
1387         }
1388         catch (IllegalAccessException expected) {
1389             check_expected(expected, msg);
1390         }
1391 
1392         msg = "Type TestNestmateMembership$CallerNotInstanceHost (loader: 'app') is not a "+
1393             "nest member of type [LInvalidNestHost; (loader: 'app'): host is not an instance class";
1394         try {
1395             CallerNotInstanceHost.getFieldTarget();
1396             throw new Error("Missing IllegalAccessError: " + msg);
1397         }
1398         catch (IllegalAccessError expected) {
1399             check_expected(expected, msg);
1400         }
1401         try {
1402             CallerNotInstanceHost.getFieldTargetNotInstanceHost();
1403             throw new Error("Missing IllegalAccessError: " + msg);
1404         }
1405         catch (IllegalAccessError expected) {
1406             check_expected(expected, msg);
1407         }
1408     }
1409 
1410     static void test_NotOurHostGetField() throws Throwable {
1411         System.out.println("Testing for nest-host class that does not list us in its nest");
1412         String msg = "Type TestNestmateMembership$TargetNotOurHost (loader: 'app') is not a nest member" +
1413             " of type InvalidNestHost (loader: 'app'): current type is not listed as a nest member";
1414         try {
1415             Caller.getFieldTargetNotOurHost();
1416             throw new Error("Missing IllegalAccessError: " + msg);
1417         }
1418         catch (IllegalAccessError expected) {
1419             check_expected(expected, msg);
1420         }
1421         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1422             "TestNestmateMembership$TargetNotOurHost with modifiers \"private static\"";
1423         try {
1424             Caller.getFieldTargetNotOurHostReflectively();
1425             throw new Error("Missing IllegalAccessException: " + msg);
1426         }
1427         catch (IllegalAccessException expected) {
1428             check_expected(expected, msg);
1429         }
1430         msg = "member is private: TestNestmateMembership$TargetNotOurHost.f/int/getStatic, " +
1431             "from class TestNestmateMembership$Caller";
1432         try {
1433             Caller.getFieldTargetNotOurHostMH();
1434             throw new Error("Missing IllegalAccessException: " + msg);
1435         }
1436         catch (IllegalAccessException expected) {
1437             check_expected(expected, msg);
1438         }
1439 
1440         msg = "Type TestNestmateMembership$CallerNotOurHost (loader: 'app') is not a nest member" +
1441             " of type InvalidNestHost (loader: 'app'): current type is not listed as a nest member";
1442         try {
1443             CallerNotOurHost.getFieldTarget();
1444             throw new Error("Missing IllegalAccessError: " + msg);
1445         }
1446         catch (IllegalAccessError expected) {
1447             check_expected(expected, msg);
1448         }
1449         try {
1450             CallerNotOurHost.getFieldTargetNotOurHost();
1451             throw new Error("Missing IllegalAccessError: " + msg);
1452         }
1453         catch (IllegalAccessError 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 (loader: 'app') is not a nest member of " +
1461             "type P1.PackagedNestHost (loader: 'app'): types are in different packages";
1462         try {
1463             P1.PackagedNestHost.doGetField();
1464             throw new Error("Missing IllegalAccessError: " + msg);
1465         }
1466         catch (IllegalAccessError expected) {
1467             check_expected(expected, msg);
1468         }
1469         try {
1470             P2.PackagedNestHost2.Member.doGetField();
1471             throw new Error("Missing IllegalAccessError: " + msg);
1472         }
1473         catch (IllegalAccessError 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 = "class TestNestmateMembership$Caller tried to access private " +
1490             "field TestNestmateMembership$TargetNoHost.f";
1491         try {
1492             Caller.putFieldTargetNoHost();
1493             throw new Error("Missing IllegalAccessError: " + msg);
1494         }
1495         catch (IllegalAccessError 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 = "class TestNestmateMembership$CallerNoHost tried to access private " +
1517             "field TestNestmateMembership$Target.f";
1518         try {
1519             CallerNoHost.putFieldTarget();
1520             throw new Error("Missing IllegalAccessError: " + msg);
1521         }
1522         catch (IllegalAccessError expected) {
1523             check_expected(expected, msg);
1524         }
1525         msg = "class TestNestmateMembership$CallerNoHost tried to access private " +
1526             "field TestNestmateMembership$TargetNoHost.f";
1527         try {
1528             CallerNoHost.putFieldTargetNoHost();
1529             throw new Error("Missing IllegalAccessError: " + msg);
1530         }
1531         catch (IllegalAccessError 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 (loader: 'app') is not a nest member" +
1539             " of type TestNestmateMembership$TargetSelfHost (loader: 'app'): current type is not listed as a nest member";
1540         try {
1541             Caller.putFieldTargetSelfHost();
1542             throw new Error("Missing IllegalAccessError: " + msg);
1543         }
1544         catch (IllegalAccessError expected) {
1545             check_expected(expected, msg);
1546         }
1547         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1548             "TestNestmateMembership$TargetSelfHost with modifiers \"private static\"";
1549         try {
1550             Caller.putFieldTargetSelfHostReflectively();
1551             throw new Error("Missing IllegalAccessException: " + msg);
1552         }
1553         catch (IllegalAccessException expected) {
1554             check_expected(expected, msg);
1555         }
1556         msg = "member is private: TestNestmateMembership$TargetSelfHost.f/int/putStatic, " +
1557             "from class TestNestmateMembership$Caller";
1558         try {
1559             Caller.putFieldTargetSelfHostMH();
1560             throw new Error("Missing IllegalAccessException: " + msg);
1561         }
1562         catch (IllegalAccessException expected) {
1563             check_expected(expected, msg);
1564         }
1565 
1566         msg = "Type TestNestmateMembership$CallerSelfHost (loader: 'app') is not a nest member" +
1567             " of type TestNestmateMembership$CallerSelfHost (loader: 'app'): current type is not listed as a nest member";
1568         try {
1569             CallerSelfHost.putFieldTarget();
1570             throw new Error("Missing IllegalAccessError: " + msg);
1571         }
1572         catch (IllegalAccessError expected) {
1573             check_expected(expected, msg);
1574         }
1575         try {
1576             CallerSelfHost.putFieldTargetSelfHost();
1577             throw new Error("Missing IllegalAccessError: " + msg);
1578         }
1579         catch (IllegalAccessError expected) {
1580             check_expected(expected, msg);
1581         }
1582     }
1583 
1584     static void test_MissingHostPutField() throws Throwable {
1585         System.out.println("Testing for nest-host class that does not exist");
1586         String msg = "Nest host resolution of TestNestmateMembership$TargetMissingHost with " +
1587             "host NoTargetMissingHost failed: java.lang.NoClassDefFoundError: NoTargetMissingHost";
1588         try {
1589             Caller.putFieldTargetMissingHost();
1590             throw new Error("Missing IllegalAccessError: " + msg);
1591         }
1592         catch (IllegalAccessError expected) {
1593             check_expected(expected, msg);
1594         }
1595         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1596             "TestNestmateMembership$TargetMissingHost with modifiers \"private static\"";
1597         try {
1598             Caller.putFieldTargetMissingHostReflectively();
1599             throw new Error("Missing IllegalAccessException: " + msg);
1600         }
1601         catch (IllegalAccessException expected) {
1602             check_expected(expected, msg);
1603         }
1604         msg = "member is private: TestNestmateMembership$TargetMissingHost.f/int/putStatic, " +
1605             "from class TestNestmateMembership$Caller";
1606         try {
1607             Caller.putFieldTargetMissingHostMH();
1608             throw new Error("Missing IllegalAccessException: " + msg);
1609         }
1610         catch (IllegalAccessException expected) {
1611             check_expected(expected, msg);
1612         }
1613         msg = "Nest host resolution of TestNestmateMembership$CallerMissingHost with host " +
1614             "NoCallerMissingHost failed: java.lang.NoClassDefFoundError: NoCallerMissingHost";
1615         try {
1616             CallerMissingHost.putFieldTarget();
1617             throw new Error("Missing IllegalAccessError: " + msg);
1618         }
1619         catch (IllegalAccessError expected) {
1620             check_expected(expected, msg);
1621         }
1622         try {
1623             CallerMissingHost.putFieldTargetMissingHost();
1624             throw new Error("Missing IllegalAccessError: " + msg);
1625         }
1626         catch (IllegalAccessError expected) {
1627             check_expected(expected, 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 (loader: 'app') is not a "+
1634             "nest member of type [LInvalidNestHost; (loader: 'app'): host is not an instance class";
1635         try {
1636             Caller.putFieldTargetNotInstanceHost();
1637             throw new Error("Missing IllegalAccessError: " + msg);
1638         }
1639         catch (IllegalAccessError expected) {
1640             check_expected(expected, msg);
1641         }
1642         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1643             "TestNestmateMembership$TargetNotInstanceHost with modifiers \"private static\"";
1644         try {
1645             Caller.putFieldTargetNotInstanceHostReflectively();
1646             throw new Error("Missing IllegalAccessException: " + msg);
1647         }
1648         catch (IllegalAccessException expected) {
1649             check_expected(expected, msg);
1650         }
1651         msg = "member is private: TestNestmateMembership$TargetNotInstanceHost.f/int/putStatic, " +
1652             "from class TestNestmateMembership$Caller";
1653         try {
1654             Caller.putFieldTargetNotInstanceHostMH();
1655             throw new Error("Missing IllegalAccessException: " + msg);
1656         }
1657         catch (IllegalAccessException expected) {
1658             check_expected(expected, msg);
1659         }
1660 
1661         msg = "Type TestNestmateMembership$CallerNotInstanceHost (loader: 'app') is not a "+
1662             "nest member of type [LInvalidNestHost; (loader: 'app'): host is not an instance class";
1663         try {
1664             CallerNotInstanceHost.putFieldTarget();
1665             throw new Error("Missing IllegalAccessError: " + msg);
1666         }
1667         catch (IllegalAccessError expected) {
1668             check_expected(expected, msg);
1669         }
1670         try {
1671             CallerNotInstanceHost.putFieldTargetNotInstanceHost();
1672             throw new Error("Missing IllegalAccessError: " + msg);
1673         }
1674         catch (IllegalAccessError expected) {
1675             check_expected(expected, msg);
1676         }
1677     }
1678 
1679     static void test_NotOurHostPutField() throws Throwable {
1680         System.out.println("Testing for nest-host class that does not list us in its nest");
1681         String msg = "Type TestNestmateMembership$TargetNotOurHost (loader: 'app') is not a nest member" +
1682             " of type InvalidNestHost (loader: 'app'): current type is not listed as a nest member";
1683         try {
1684             Caller.putFieldTargetNotOurHost();
1685             throw new Error("Missing IllegalAccessError: " + msg);
1686         }
1687         catch (IllegalAccessError expected) {
1688             check_expected(expected, msg);
1689         }
1690         msg = "class TestNestmateMembership$Caller cannot access a member of class " +
1691             "TestNestmateMembership$TargetNotOurHost with modifiers \"private static\"";
1692         try {
1693             Caller.putFieldTargetNotOurHostReflectively();
1694             throw new Error("Missing IllegalAccessException: " + msg);
1695         }
1696         catch (IllegalAccessException expected) {
1697             check_expected(expected, msg);
1698         }
1699         msg = "member is private: TestNestmateMembership$TargetNotOurHost.f/int/putStatic, " +
1700             "from class TestNestmateMembership$Caller";
1701         try {
1702             Caller.putFieldTargetNotOurHostMH();
1703             throw new Error("Missing IllegalAccessException: " + msg);
1704         }
1705         catch (IllegalAccessException expected) {
1706             check_expected(expected, msg);
1707         }
1708         msg = "Type TestNestmateMembership$CallerNotOurHost (loader: 'app') is not a nest member" +
1709             " of type InvalidNestHost (loader: 'app'): current type is not listed as a nest member";
1710         try {
1711             CallerNotOurHost.putFieldTarget();
1712             throw new Error("Missing IllegalAccessError: " + msg);
1713         }
1714         catch (IllegalAccessError expected) {
1715             check_expected(expected, msg);
1716         }
1717         try {
1718             CallerNotOurHost.putFieldTargetNotOurHost();
1719             throw new Error("Missing IllegalAccessError: " + msg);
1720         }
1721         catch (IllegalAccessError expected) {
1722             check_expected(expected, msg);
1723         }
1724     }
1725 
1726     static void test_WrongPackageHostPutField() {
1727         System.out.println("Testing for nest-host and nest-member in different packages");
1728         String msg = "Type P2.PackagedNestHost2$Member (loader: 'app') is not a nest member of " +
1729             "type P1.PackagedNestHost (loader: 'app'): types are in different packages";
1730         try {
1731             P1.PackagedNestHost.doPutField();
1732             throw new Error("Missing IllegalAccessError: " + msg);
1733         }
1734         catch (IllegalAccessError expected) {
1735             check_expected(expected, msg);
1736         }
1737         try {
1738             P2.PackagedNestHost2.Member.doPutField();
1739             throw new Error("Missing IllegalAccessError: " + msg);
1740         }
1741         catch (IllegalAccessError expected) {
1742             check_expected(expected, msg);
1743         }
1744     }
1745 
1746     // utilities
1747 
1748     static void check_expected(Throwable expected, String msg) {
1749         if (!expected.getMessage().contains(msg)) {
1750             throw new Error("Wrong " + expected.getClass().getSimpleName() +": \"" +
1751                             expected.getMessage() + "\" does not contain \"" +
1752                             msg + "\"", expected);
1753         }
1754         System.out.println("OK - got expected exception: " + expected);
1755     }
1756 }