1 /*
   2  * Copyright (c) 2017, 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 = "tried to access method " +
 674             "TestNestmateMembership$TargetNoHost.m()V from class " +
 675             "TestNestmateMembership$Caller";
 676         try {
 677             Caller.invokeTargetNoHost();
 678             throw new Error("Missing IllegalAccessError: " + msg);
 679         }
 680         catch (IllegalAccessError expected) {
 681             check_expected(expected, msg);
 682         }
 683         msg = "TestNestmateMembership$Caller cannot access a member of class " +
 684             "TestNestmateMembership$TargetNoHost with modifiers \"private static\"";
 685         try {
 686             Caller.invokeTargetNoHostReflectively();
 687             throw new Error("Missing IllegalAccessException: " + msg);
 688         }
 689         catch (IllegalAccessException expected) {
 690             check_expected(expected, msg);
 691         }
 692         msg = "no such method: TestNestmateMembership$TargetNoHost.m()void/invokeStatic";
 693         try {
 694             Caller.invokeTargetNoHostMH();
 695             throw new Error("Missing IllegalAccessException: " + msg);
 696         }
 697         catch (IllegalAccessException expected) {
 698             check_expected(expected, msg);
 699         }
 700 
 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";
 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 = "Unable to load nest-host class (NoCallerMissingHost) of " +
 805             "TestNestmateMembership$CallerMissingHost";
 806         cause_msg = "NoCallerMissingHost";
 807         try {
 808             CallerMissingHost.invokeTarget();
 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         }
1063         catch (NoClassDefFoundError expected) {
1064             check_expected(expected, msg, cause_msg);
1065         }
1066         msg = "no such constructor: TestNestmateMembership$TargetMissingHost.<init>()void/newInvokeSpecial";
1067         try {
1068             Caller.newTargetMissingHostMH();
1069             throw new Error("Missing IllegalAccessException: " + msg);
1070         }
1071         catch (IllegalAccessException expected) {
1072             check_expected(expected, msg);
1073         }
1074 
1075         msg = "Unable to load nest-host class (NoCallerMissingHost) of " +
1076             "TestNestmateMembership$CallerMissingHost";
1077         cause_msg = "NoCallerMissingHost";
1078         try {
1079             CallerMissingHost.newTarget();
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         }
1333         catch (NoClassDefFoundError expected) {
1334             check_expected(expected, msg, cause_msg);
1335         }
1336         try {
1337             Caller.getFieldTargetMissingHostMH();
1338             throw new Error("Missing NoClassDefFoundError: " + msg);
1339         }
1340         catch (NoClassDefFoundError expected) {
1341             check_expected(expected, msg, cause_msg);
1342         }
1343 
1344         msg = "Unable to load nest-host class (NoCallerMissingHost) of " +
1345             "TestNestmateMembership$CallerMissingHost";
1346         cause_msg = "NoCallerMissingHost";
1347         try {
1348             CallerMissingHost.getFieldTarget();
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         }
1598         catch (NoClassDefFoundError expected) {
1599             check_expected(expected, msg, cause_msg);
1600         }
1601         try {
1602             Caller.putFieldTargetMissingHostMH();
1603             throw new Error("Missing NoClassDefFoundError: " + msg);
1604         }
1605         catch (NoClassDefFoundError expected) {
1606             check_expected(expected, msg, cause_msg);
1607         }
1608 
1609         msg = "Unable to load nest-host class (NoCallerMissingHost) of " +
1610             "TestNestmateMembership$CallerMissingHost";
1611         cause_msg = "NoCallerMissingHost";
1612         try {
1613             CallerMissingHost.putFieldTarget();
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 + "\"");
1759         }
1760         Throwable cause = expected.getCause();
1761         if (cause instanceof NoClassDefFoundError) {
1762             if (!cause.getMessage().contains(cause_msg)) {
1763                 throw new Error(expected.getClass().getSimpleName() +
1764                                 " has wrong cause " + cause.getClass().getSimpleName() +": \"" +
1765                                 cause.getMessage() + "\" does not contain \"" +
1766                                 cause_msg + "\"");
1767             }
1768         }
1769         else throw new Error(expected.getClass().getSimpleName() +
1770                              " has wrong cause " + cause.getClass().getSimpleName());
1771 
1772         System.out.println("OK - got expected exception: " + expected +
1773                            " with cause " + cause);
1774     }
1775 
1776 }