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