--- old/src/java.base/share/classes/java/lang/invoke/MethodHandles.java 2018-01-30 23:31:13.780101738 -0500 +++ new/src/java.base/share/classes/java/lang/invoke/MethodHandles.java 2018-01-30 23:31:12.280016100 -0500 @@ -835,7 +835,7 @@ } // Allow nestmate lookups to be created without special privilege: if ((newModes & PRIVATE) != 0 - && !Reflection.areNestMates(this.lookupClass, requestedLookupClass)) { + && !VerifyAccess.areNestMates(this.lookupClass, requestedLookupClass)) { newModes &= ~(PRIVATE|PROTECTED); } if ((newModes & PUBLIC) != 0 --- old/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java 2018-01-30 23:31:18.208354539 -0500 +++ new/src/java.base/share/classes/sun/invoke/util/VerifyAccess.java 2018-01-30 23:31:16.724269815 -0500 @@ -133,7 +133,7 @@ case PRIVATE: // Rules for privates follows access rules for nestmates. return ((allowedModes & PRIVATE) != 0 && - Reflection.areNestMates(defc, lookupClass)); + areNestMates(defc, lookupClass)); default: throw new IllegalArgumentException("bad modifiers: "+Modifier.toString(mods)); } @@ -336,6 +336,33 @@ return Objects.equals(class1.getPackageName(), class2.getPackageName()); } + /** + * Test if two classes are defined as part of the same package member (top-level class). + * If this is true, they can share private access with each other. + * @param class1 a class + * @param class2 another class + * @return whether they are identical or nested together + */ + public static boolean areNestMates(Class class1, Class class2) { + if (class1 == class2) + return true; + if (!isSamePackage(class1, class2)) + return false; + if (Reflection.areNestMates(class1, class2)) + return true; + // Could be pre-nestmate nested types + if (getOutermostEnclosingClass(class1) != getOutermostEnclosingClass(class2)) + return false; + return true; + } + + private static Class getOutermostEnclosingClass(Class c) { + Class pkgmem = c; + for (Class enc = c; (enc = enc.getEnclosingClass()) != null; ) + pkgmem = enc; + return pkgmem; + } + private static boolean loadersAreRelated(ClassLoader loader1, ClassLoader loader2, boolean loader1MustBeParent) { if (loader1 == loader2 || loader1 == null --- /dev/null 2017-12-22 23:59:43.820328997 -0500 +++ new/test/hotspot/jtreg/runtime/Nestmates/legacy/TestPrivateLookup.java 2018-01-30 23:31:21.024515309 -0500 @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8046171 + * @summary Test that private Lookup works for both nestmate-enabled classes + * and legacy classes + * @run main TestPrivateLookup + * @compile -source 10 -target 10 TestPrivateLookup.java + * @run main TestPrivateLookup noNestmates + */ +import java.lang.invoke.*; +import static java.lang.invoke.MethodHandles.*; +import static java.lang.invoke.MethodType.*; + +public class TestPrivateLookup { + + static boolean VMHasNestmates = + System.getProperty("java.vm.specification.version").equals("10"); + static boolean compiledForNestmates; + + static class C { + static class D { + private void m() { } + } + + static void test() throws Throwable { + MethodType M_T = MethodType.methodType(void.class); + // Direct lookup from C + Lookup l = lookup(); + try { + MethodHandle mh = l.findVirtual(D.class, "m", M_T); + if (compiledForNestmates) { + System.out.println("Lookup of D.m from C succeeded as expected with nestmates"); + } + else { + throw new Error("Unexpected success when not compiled for nestmates!"); + } + } + catch (IllegalAccessException iae) { + if (!compiledForNestmates) { + System.out.println("Lookup of D.m from C failed as expected without nestmates"); + } + else { + throw new Error("Unexpected failure with nestmates", iae); + } + } + // switch lookup class to D + l = l.in(D.class); + try { + MethodHandle mh = l.findVirtual(D.class, "m", M_T); + System.out.println("Lookup of D.m from D succeeded as expected" + + " with" + (compiledForNestmates ? "" : "out") + + " nestmates"); + } + catch (IllegalAccessException iae) { + throw new Error("Lookup of D.m from D failed", iae); + } + } + } + + public static void main(String[] args) throws Throwable { + if (!VMHasNestmates) { + throw new Error("This test is only for JDK 11 with nestmates"); + } + + // If there's no nesthost attribute A.getNestHost() == A + compiledForNestmates = C.D.class.getNestHost() == TestPrivateLookup.class; + // sanity check + boolean expectingNestmates = args.length == 0; + + if (compiledForNestmates && !expectingNestmates) { + throw new Error("Test is being run incorrectly: " + + "nestmates are being used but not expected"); + } + if (expectingNestmates && !compiledForNestmates) { + throw new Error("Test is being run incorrectly: ": + + "nestmates are expected but not being used"); + } + System.out.println("Testing with" + (expectingNestmates ? "" : "out") + + " nestmates"); + + C.test(); + } +}