AccessControlTest.java revision 5109
3681N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 2386N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2386N/A * This code is free software; you can redistribute it and/or modify it 2386N/A * under the terms of the GNU General Public License version 2 only, as 2386N/A * published by the Free Software Foundation. Oracle designates this 2386N/A * particular file as subject to the "Classpath" exception as provided 2386N/A * by Oracle in the LICENSE file that accompanied this code. 2386N/A * This code is distributed in the hope that it will be useful, but WITHOUT 2386N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2386N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2386N/A * version 2 for more details (a copy is included in the LICENSE file that 2386N/A * You should have received a copy of the GNU General Public License version 2386N/A * 2 along with this work; if not, write to the Free Software Foundation, 2386N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2386N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2386N/A * or visit www.oracle.com if you need additional information or have any 2386N/A * @summary test access checking by java.lang.invoke.MethodHandles.Lookup 2386N/A * @build test.java.lang.invoke.AccessControlTest_subpkg.Acquaintance_remote 2386N/A * Test many combinations of Lookup access and cross-class lookupStatic. 2386N/A /** Simulate all assertions in the spec. for Lookup.toString. */ 2386N/A /** Simulate all assertions from the spec. for Lookup.in: 2386N/A * Creates a lookup on the specified new lookup class. 2386N/A * [A1] The resulting object will report the specified 2386N/A * class as its own {@link #lookupClass lookupClass}. 2386N/A * [A2] However, the resulting {@code Lookup} object is guaranteed 2386N/A * to have no more access capabilities than the original. 2816N/A * In particular, access capabilities can be lost as follows:<ul> 2816N/A * <li>[A3] If the new lookup class differs from the old one, 2816N/A * protected members will not be accessible by virtue of inheritance. 2816N/A * (Protected members may continue to be accessible because of package sharing.) 2816N/A * <li>[A4] If the new lookup class is in a different package 2816N/A * than the old one, protected and default (package) members will not be accessible. 2816N/A * <li>[A5] If the new lookup class is not within the same package member 2816N/A * as the old one, private members will not be accessible. 2816N/A * <li>[A6] If the new lookup class is not accessible to the old lookup class, 3681N/A * using the original access modes, 2816N/A * then no members, not even public members, will be accessible. 2816N/A * [A7] (In all other cases, public members will continue to be accessible.) 2816N/A * Other than the above cases, the new lookup will have the same 2816N/A * access capabilities as the original. [A8] 2386N/A // Different package and no access to c2; lose all access. 2386N/A // Different package; lose PACKAGE and lower access. 2816N/A // Different top-level class. Lose PRIVATE and lower access. 2816N/A assert((
m1 |
m2) ==
m1);
// [A2] (no elevation of access) 2386N/A /** Predict the success or failure of accessing this method. */ 2816N/A // privacy is strictly enforced on lookups 2816N/A // protected access is sometimes allowed 2386N/A m2 |=
PUBLIC;
// from a subclass, it acts like a public method also assert((
m1 |
m2) ==
m1);
// [A2] (no elevation of access) // make initial set of lookup test cases // Try to access target method from various contexts. // return "self", "sibling", "nestmate", etc. case PUBLIC:
return "pub_in_";
/** Return one of the ACCESS_CASES. */ // This guy tests access from outside the package: package test.java.lang.invoke.AccessControlTest_subpkg; public class Acquaintance_remote { public static Lookup[] lookups() { ...