2
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
* @summary Class p1.c1 in module first_mod cannot read p2.c2 in module second_mod,
26
* even after a read edge is added between first_mod and second_mod.
27
* Ensures constant access check answers when not accessible due to readability.
29
* @modules java.base/jdk.internal.misc
30
* @modules java.base/jdk.internal.module
34
* @run main/othervm AccessReadTwice
37
import static jdk.test.lib.Asserts.*;
39
import java.lang.module.Configuration;
40
import java.lang.module.ModuleDescriptor;
41
import java.lang.module.ModuleFinder;
42
import java.util.HashMap;
47
// ClassLoader1 --> defines first_mod --> packages p1, p4
48
// defines second_mod --> package p2
50
// package p2 in second_mod is exported to first_mod
52
// class p1.c1 defined in first_mod tries to access p2.c2 defined in second_mod
53
// Access is not allowed, even after a read edge is added between first_mod and second_mod.
55
public class AccessReadTwice {
57
// Create a layer over the boot layer.
58
// Define modules within this layer to test access between
59
// publicly defined classes within packages of those modules.
60
public void createLayerOnBoot() throws Throwable {
62
// Define module: first_mod
63
// Can read: java.base
65
// Packages exported: none
66
ModuleDescriptor descriptor_first_mod =
67
ModuleDescriptor.newModule("first_mod")
68
.requires("java.base")
69
.packages(Set.of("p1", "p4"))
72
// Define module: second_mod
73
// Can read: java.base
75
// Packages exported: p2 is exported to first_mod
76
ModuleDescriptor descriptor_second_mod =
77
ModuleDescriptor.newModule("second_mod")
78
.requires("java.base")
79
.exports("p2", Set.of("first_mod"))
82
// Set up a ModuleFinder containing all modules for this layer
83
ModuleFinder finder = ModuleLibrary.of(descriptor_first_mod, descriptor_second_mod);
85
// Resolves "first_mod" and "second_mod"
86
Configuration cf = ModuleLayer.boot()
88
.resolve(finder, ModuleFinder.of(), Set.of("first_mod", "second_mod"));
90
// Map each module to this class loader
91
Map<String, ClassLoader> map = new HashMap<>();
92
ClassLoader loader = AccessReadTwice.class.getClassLoader();
93
map.put("first_mod", loader);
94
map.put("second_mod", loader);
96
// Create layer that contains first_mod & second_mod
97
ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get);
99
assertTrue(layer.findLoader("first_mod") == loader);
100
assertTrue(layer.findLoader("second_mod") == loader);
101
assertTrue(layer.findLoader("java.base") == null);
103
Class p2_c2_class = loader.loadClass("p2.c2");
104
Class p1_c1_class = loader.loadClass("p1.c1");
105
Class p4_c4_class = loader.loadClass("p4.c4");
107
Module first_mod = p1_c1_class.getModule();
108
Module second_mod = p2_c2_class.getModule();
110
// Export first_mod/p1 and first_mod/p4 to all unnamed modules so that
111
// this test can use them
112
jdk.internal.module.Modules.addExportsToAllUnnamed(first_mod, "p1");
113
jdk.internal.module.Modules.addExportsToAllUnnamed(first_mod, "p4");
115
// First access check for p1.c1
117
p1_c1_class.newInstance();
118
throw new RuntimeException("Test Failed, module first_mod should not have access to p2.c2");
119
} catch (IllegalAccessError e) {
120
String message = e.getMessage();
121
if (!(message.contains("cannot access") &&
122
message.contains("because module first_mod does not read module second_mod"))) {
123
throw new RuntimeException("Wrong message: " + message);
125
System.out.println("Test Succeeded at attempt #1");
129
// Add a read edge from p4/c4's module (first_mod) to second_mod
130
p4.c4 c4_obj = new p4.c4();
131
c4_obj.addReads(second_mod);
133
// Second access check for p1.c1, should have same result as first
135
p1_c1_class.newInstance();
136
throw new RuntimeException("Test Failed, access should have been cached above");
137
} catch (IllegalAccessError e) {
138
String message = e.getMessage();
139
if (!(message.contains("cannot access") &&
140
message.contains("because module first_mod does not read module second_mod"))) {
141
throw new RuntimeException("Wrong message: " + message);
143
System.out.println("Test Succeeded at attempt #2");
148
public static void main(String args[]) throws Throwable {
149
AccessReadTwice test = new AccessReadTwice();
150
test.createLayerOnBoot();