< prev index next >

test/jdk/java/lang/reflect/Nestmates/TestReflectionAPI.java

Print this page


   1 /*
   2  * Copyright (c) 2017, 2018, 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  */


 159 
 160     static void test_getNestMembers() {
 161         // Sampling of "good" checks
 162         Class<?>[] good = { Object.class, Object[].class, int.class};
 163         checkSingletonNests(good);
 164 
 165         // More thorough correctness check
 166         checkNest(SampleNest.class, SampleNest.nestedTypes(), false);
 167 
 168         // Special cases - legal but not produced by javac
 169         checkNest(HostWithSelfMember.class,
 170                   new Class<?>[] { HostWithSelfMember.class,
 171                           HostWithSelfMember.Member.class },
 172                   true);
 173         checkNest(HostWithDuplicateMembers.class,
 174                   new Class<?>[] { HostWithDuplicateMembers.class,
 175                           HostWithDuplicateMembers.Member1.class,
 176                           HostWithDuplicateMembers.Member2.class },
 177                   true);
 178 
 179         // Hosts with "bad" members
 180         Class<?>[] bad = {
 181             HostOfMemberNoHost.class,
 182             HostOfMemberMissingHost.class,
 183             HostOfMemberNotOurHost.class,
 184             HostOfMemberNotInstanceHost.class,
 185             HostOfMemberMalformedHost.class,
 186         };
 187         Class<?>[] exceptions = {
 188             IncompatibleClassChangeError.class,
 189             NoClassDefFoundError.class,
 190             IncompatibleClassChangeError.class,
 191             IncompatibleClassChangeError.class,
 192             ClassFormatError.class,
 193         };
 194         String[] messages = {
 195             "Nest member HostOfMemberNoHost$MemberNoHost in HostOfMemberNoHost " +
 196             "declares a different nest host of HostOfMemberNoHost$MemberNoHost",
 197             "Unable to load nest-host class (NestHost) of " +
 198             "HostOfMemberMissingHost$MemberMissingHost",
 199             "Type HostOfMemberNotOurHost$MemberNotOurHost (loader: 'app') is not a nest member " +
 200             "of InvalidNestHost (loader: 'app'): current type is not listed as a nest member",
 201             "Type HostOfMemberNotInstanceHost$MemberNotInstanceHost (loader: 'app') is not a nest " +
 202             "member of [LInvalidNestHost; (loader: 'app'): current type is not listed as a nest member",
 203             "Incompatible magic value 3735928559 in class file MalformedHost",
 204         };
 205         for (int i = 0; i < bad.length; i++) {
 206             try {
 207                 bad[i].getNestMembers();
 208                 throw new Error("getNestMembers() succeeded for class " +
 209                                bad[i].getName());
 210             } catch (LinkageError e) {
 211                 checkException(e, messages[i], exceptions[i]);
 212             }
 213         }
 214     }
 215 
 216     static void checkException(Throwable actual, String msg, Class<?> expected) {
 217         if (!actual.getClass().equals(expected))
 218             throw new Error("Unexpected exception: got " + actual.getClass().getName()
 219                             + " but expected " + expected.getName());
 220         if (!actual.getMessage().contains(msg))
 221             throw new Error("Wrong " + actual.getClass().getSimpleName() +": \"" +
 222                             actual.getMessage() + "\" does not contain \"" +
 223                             msg + "\"");
 224         System.out.println("OK - got expected exception: " + actual);
 225     }
 226 
 227     static void checkHost(Class<?> target, Class<?> expected) {
 228         System.out.println("Checking nest host of " + target.getName());
 229         Class<?> host = target.getNestHost();
 230         if (host != expected)
 231             throw new Error("Class " + target.getName() +
 232                             " has nest host " + host.getName() +
 233                             " but expected " + expected.getName());
 234     }
 235 
 236     static void checkNestmates(Class<?> a, Class<?> b, boolean mates) {
 237         System.out.println("Checking if " + a.getName() +
 238                            " isNestmateOf " + b.getName());
 239 
 240         if (a.isNestmateOf(b) != mates)
 241             throw new Error("Class " + a.getName() + " is " +
 242                             (mates ? "not " : "") +
 243                             "a nestmate of " + b.getName() + " but should " +
 244                             (mates ? "" : "not ") + "be");


   1 /*
   2  * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


 159 
 160     static void test_getNestMembers() {
 161         // Sampling of "good" checks
 162         Class<?>[] good = { Object.class, Object[].class, int.class};
 163         checkSingletonNests(good);
 164 
 165         // More thorough correctness check
 166         checkNest(SampleNest.class, SampleNest.nestedTypes(), false);
 167 
 168         // Special cases - legal but not produced by javac
 169         checkNest(HostWithSelfMember.class,
 170                   new Class<?>[] { HostWithSelfMember.class,
 171                           HostWithSelfMember.Member.class },
 172                   true);
 173         checkNest(HostWithDuplicateMembers.class,
 174                   new Class<?>[] { HostWithDuplicateMembers.class,
 175                           HostWithDuplicateMembers.Member1.class,
 176                           HostWithDuplicateMembers.Member2.class },
 177                   true);
 178 
 179         // Hosts with only "bad" members
 180         Class<?>[] bad = {
 181             HostOfMemberNoHost.class,
 182             HostOfMemberMissingHost.class,
 183             HostOfMemberNotOurHost.class,
 184             HostOfMemberNotInstanceHost.class,
 185             HostOfMemberMalformedHost.class,
 186         };
 187         checkSingletonNests(bad);





































 188     }
 189 
 190     static void checkHost(Class<?> target, Class<?> expected) {
 191         System.out.println("Checking nest host of " + target.getName());
 192         Class<?> host = target.getNestHost();
 193         if (host != expected)
 194             throw new Error("Class " + target.getName() +
 195                             " has nest host " + host.getName() +
 196                             " but expected " + expected.getName());
 197     }
 198 
 199     static void checkNestmates(Class<?> a, Class<?> b, boolean mates) {
 200         System.out.println("Checking if " + a.getName() +
 201                            " isNestmateOf " + b.getName());
 202 
 203         if (a.isNestmateOf(b) != mates)
 204             throw new Error("Class " + a.getName() + " is " +
 205                             (mates ? "not " : "") +
 206                             "a nestmate of " + b.getName() + " but should " +
 207                             (mates ? "" : "not ") + "be");


< prev index next >