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 28 * @compile TestNestmateMembership.java 29 * PackagedNestHost.java 30 * PackagedNestHost2.java 31 * NotAMember2.java 32 * @compile MissingNestHost.jcod 33 * ArrayNestHost.jcod 34 * NotAMember.jcod 35 * NotAMember2.jcod 36 * PackagedNestHost.jcod 37 * PackagedNestHost2Member.jcod 38 * @run main/othervm TestNestmateMembership 39 */ 40 41 // We test all the "illegal" relationships between a nest member and its nest-host 42 // except for the case where the name of the nest-member matches the name listed 43 // in the nest-host, but resolves to a different class. There doesn't seem to 44 // be a way to construct that scenario. 45 // For each nested class below there is a corresponding .jcod file which breaks one 46 // of the rules regarding nest membership. For the package related tests we have 47 // additional PackageNestHost*.java sources. We also have NotAMember2.java. 48 // Note that all the .java files must be compiled in the same step, while all 49 // .jcod files must be compiled in a later step. 50 51 public class TestNestmateMembership { 52 53 // jcod modified to have non-existent nest-host 54 static class MissingNestHost { 55 private static void m() { 56 System.out.println("MissingNestHost.m() - java version"); 57 } 58 } 59 60 // jcod modified to have non-instance class Object[] as nest-host 61 static class ArrayNestHost { 62 Object[] oa; // create CP entry 63 private static void m() { 64 System.out.println("ArrayNestHost.m() - java version"); 65 } 66 } 67 68 // jcod modified to have Object as nest-host, which has no nest-members 69 static class NotAMember { 70 private static void m() { 71 System.out.println("NotAMember.m() - java version"); 72 } 73 } 74 75 public static void main(String[] args) throws Throwable { 76 test_MissingNestHost(); 77 test_ArrayNestHost(); 78 test_WrongPackageForNestMember(); 79 test_NotAMember(); 80 test_NotAMember2(); 81 } 82 83 static void test_WrongPackageForNestMember() { 84 System.out.println("Testing for nest-host and nest-member in different packages"); 85 String msg = "Class P2.PackagedNestHost2$Member is in a different" + 86 " package to its nest-host class P1.PackagedNestHost"; 87 try { 88 P1.PackagedNestHost.doAccess(); 89 throw new Error("Missing IncompatibleClassChangeError: " + msg); 90 } 91 catch (IncompatibleClassChangeError expected) { 92 if (!expected.getMessage().contains(msg)) 93 throw new Error("Wrong IncompatibleClassChangeError: \"" + 94 expected.getMessage() + "\" does not contain \"" + 95 msg + "\""); 96 System.out.println("OK - got expected exception: " + expected); 97 } 98 } 99 100 static void test_MissingNestHost() throws Throwable { 101 System.out.println("Testing for nest-host class that does not exist"); 102 String msg = "NoSuchClass"; 103 try { 104 MissingNestHost.m(); 105 throw new Error("Missing NoClassDefFoundError: " + msg); 106 } 107 catch (NoClassDefFoundError expected) { 108 if (!expected.getMessage().contains(msg)) 109 throw new Error("Wrong NoClassDefFoundError: \"" + 110 expected.getMessage() + "\" does not contain \"" + 111 msg + "\""); 112 System.out.println("OK - got expected exception: " + expected); 113 } 114 } 115 116 static void test_ArrayNestHost() throws Throwable { 117 System.out.println("Testing for nest-host class that is not an instance class"); 118 String msg = "ArrayNestHost has non-instance class [Ljava.lang.Object; as nest-host"; 119 try { 120 ArrayNestHost.m(); 121 throw new Error("Missing IncompatibleClassChangeError: " + msg); 122 } 123 catch (IncompatibleClassChangeError expected) { 124 if (!expected.getMessage().contains(msg)) 125 throw new Error("Wrong IncompatibleClassChangeError: \"" + 126 expected.getMessage() + "\" does not contain \"" + 127 msg + "\""); 128 System.out.println("OK - got expected exception: " + expected); 129 } 130 } 131 132 static void test_NotAMember() throws Throwable { 133 System.out.println("Testing for nest-host class that has no nest"); 134 String msg = "NotAMember is not a nest member of java.lang.Object"; 135 try { 136 NotAMember.m(); 137 throw new Error("Missing IncompatibleClassChangeError: " + msg); 138 } 139 catch (IncompatibleClassChangeError expected) { 140 if (!expected.getMessage().contains(msg)) 141 throw new Error("Wrong IncompatibleClassChangeError: \"" + 142 expected.getMessage() + "\" does not contain \"" + 143 msg + "\""); 144 System.out.println("OK - got expected exception: " + expected); 145 } 146 } 147 148 static void test_NotAMember2() throws Throwable { 149 System.out.println("Testing for nest-host class that doesn't list this class as a member"); 150 String msg = "NotAMember2$Member is not a nest member of TestNestmateMembership"; 151 try { 152 NotAMember2.Member.m(); 153 throw new Error("Missing IncompatibleClassChangeError: " + msg); 154 } 155 catch (IncompatibleClassChangeError expected) { 156 if (!expected.getMessage().contains(msg)) 157 throw new Error("Wrong IncompatibleClassChangeError: \"" + 158 expected.getMessage() + "\" does not contain \"" + 159 msg + "\""); 160 System.out.println("OK - got expected exception: " + expected); 161 } 162 } 163 }