/* * Copyright (c) 2015, 2019, 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.
*/
// m1 needs to read unnamed module
Main.class.getModule().addReads(unnamedModule);
}
/** * MethodHandles.lookup() * * [A0] has module access * [A1] can access all public types in m1 * [A2] can access public types in packages exported by modules that m1 reads * [A3] cannot access public types in non-exported modules of modules that m1 reads
*/ publicvoid testLookup() throws Exception {
Lookup lookup = MethodHandles.lookup();
assertTrue((lookup.lookupModes() & MODULE) == MODULE); // [A0]
/** * Hop to lookup class in the same module * * [A0] module and public access is not lost
*/ publicvoid testToSameModule() throws Exception {
Lookup lookup = MethodHandles.lookup().in(p2_Type2);
assertTrue(lookup.lookupModes() == (MODULE|PUBLIC)); // [A0]
/** * Hop to lookup class in another named module * * [A0] has PUBLIC access if accessible; otherwise no access * [A1] old lookup class becomes previous lookup class
*/ publicvoid testFromNamedToNamedModule() throws Exception { // m2/q1_Type1 is accessible to m1 whereas m2/q_Type2 is not accessible
Lookup lookup = MethodHandles.lookup().in(q1_Type1);
assertTrue(lookup.lookupModes() == PUBLIC); // [A0]
assertTrue(lookup.previousLookupClass() == Main.class); // [A1]
/** * Hop to lookup class in an unnamed module * * [A0] has PUBLIC access
*/ publicvoid testFromNamedToUnnamedModule() throws Exception {
Lookup lookup = MethodHandles.lookup().in(unnamedClass);
assertTrue(lookup.lookupModes() == PUBLIC); // [A0]
/** * Hop from publicLookup to accessible type in named module. * * [A0] has UNCONDITIONAL access
*/ publicvoid testPublicLookupToAccessibleTypeInNamedModule() throws Exception {
Lookup lookup = MethodHandles.publicLookup().in(p1_Type1);
assertTrue(lookup.lookupModes() == UNCONDITIONAL); // A0
/** * Teleport from publicLookup to inaccessible type in named module. * * [A0] has no access
*/ publicvoid testPublicLookupToInaccessibleTypeInNamedModule() throws Exception {
Lookup lookup = MethodHandles.publicLookup().in(p2_Type2);
assertTrue(lookup.lookupModes() == 0); // A0
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.