1 /* 2 * Copyright (c) 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 */ 23 24 /* 25 * @test 26 * @bug 8046171 27 * @summary Test that private Lookup works for both nestmate-enabled classes 28 * and legacy classes 29 * @run main TestPrivateLookup 30 * @compile -source 10 -target 10 TestPrivateLookup.java 31 * @run main TestPrivateLookup noNestmates 32 */ 33 import java.lang.invoke.*; 34 import static java.lang.invoke.MethodHandles.*; 35 import static java.lang.invoke.MethodType.*; 36 37 public class TestPrivateLookup { 38 39 static boolean VMHasNestmates = 40 System.getProperty("java.vm.specification.version").equals("10"); 41 static boolean compiledForNestmates; 42 43 static class C { 44 static class D { 45 private void m() { } 46 } 47 48 static void test() throws Throwable { 49 MethodType M_T = MethodType.methodType(void.class); 50 // Direct lookup from C 51 Lookup l = lookup(); 52 try { 53 MethodHandle mh = l.findVirtual(D.class, "m", M_T); 54 if (compiledForNestmates) { 55 System.out.println("Lookup of D.m from C succeeded as expected with nestmates"); 56 } 57 else { 58 throw new Error("Unexpected success when not compiled for nestmates!"); 59 } 60 } 61 catch (IllegalAccessException iae) { 62 if (!compiledForNestmates) { 63 System.out.println("Lookup of D.m from C failed as expected without nestmates"); 64 } 65 else { 66 throw new Error("Unexpected failure with nestmates", iae); 67 } 68 } 69 // switch lookup class to D 70 l = l.in(D.class); 71 try { 72 MethodHandle mh = l.findVirtual(D.class, "m", M_T); 73 System.out.println("Lookup of D.m from D succeeded as expected" + 74 " with" + (compiledForNestmates ? "" : "out") + 75 " nestmates"); 76 } 77 catch (IllegalAccessException iae) { 78 throw new Error("Lookup of D.m from D failed", iae); 79 } 80 } 81 } 82 83 public static void main(String[] args) throws Throwable { 84 if (!VMHasNestmates) { 85 throw new Error("This test is only for JDK 11 with nestmates"); 86 } 87 88 // If there's no nesthost attribute A.getNestHost() == A 89 compiledForNestmates = C.D.class.getNestHost() == TestPrivateLookup.class; 90 // sanity check 91 boolean expectingNestmates = args.length == 0; 92 93 if (compiledForNestmates && !expectingNestmates) { 94 throw new Error("Test is being run incorrectly: " + 95 "nestmates are being used but not expected"); 96 } 97 if (expectingNestmates && !compiledForNestmates) { 98 throw new Error("Test is being run incorrectly: ": + 99 "nestmates are expected but not being used"); 100 } 101 System.out.println("Testing with" + (expectingNestmates ? "" : "out") + 102 " nestmates"); 103 104 C.test(); 105 } 106 }