author | sspitsyn |
Sat, 09 Jul 2016 19:20:38 -0700 | |
changeset 39706 | 8b8fd966478b |
parent 39616 | f82b1f888578 |
child 40013 | 943cf01a6b82 |
permissions | -rw-r--r-- |
36508 | 1 |
/* |
2 |
* Copyright (c) 2016, 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 |
#include "precompiled.hpp" |
|
26 |
#include "classfile/classFileParser.hpp" |
|
27 |
#include "classfile/classLoader.hpp" |
|
28 |
#include "classfile/classLoaderData.inline.hpp" |
|
29 |
#include "classfile/javaAssertions.hpp" |
|
30 |
#include "classfile/javaClasses.hpp" |
|
31 |
#include "classfile/javaClasses.inline.hpp" |
|
32 |
#include "classfile/moduleEntry.hpp" |
|
33 |
#include "classfile/modules.hpp" |
|
34 |
#include "classfile/packageEntry.hpp" |
|
35 |
#include "classfile/stringTable.hpp" |
|
36 |
#include "classfile/symbolTable.hpp" |
|
37 |
#include "classfile/vmSymbols.hpp" |
|
38 |
#include "logging/log.hpp" |
|
37248 | 39 |
#include "memory/resourceArea.hpp" |
36508 | 40 |
#include "oops/instanceKlass.hpp" |
41 |
#include "oops/objArrayKlass.hpp" |
|
42 |
#include "oops/objArrayOop.inline.hpp" |
|
43 |
#include "runtime/arguments.hpp" |
|
44 |
#include "runtime/handles.inline.hpp" |
|
45 |
#include "runtime/javaCalls.hpp" |
|
46 |
#include "runtime/reflection.hpp" |
|
47 |
#include "utilities/utf8.hpp" |
|
48 |
||
49 |
static bool verify_module_name(char *module_name) { |
|
50 |
if (module_name == NULL) return false; |
|
51 |
int len = (int)strlen(module_name); |
|
52 |
return (len > 0 && len <= Symbol::max_length() && |
|
53 |
UTF8::is_legal_utf8((unsigned char *)module_name, len, false) && |
|
54 |
ClassFileParser::verify_unqualified_name(module_name, len, |
|
55 |
ClassFileParser::LegalModule)); |
|
56 |
} |
|
57 |
||
58 |
bool Modules::verify_package_name(char *package_name) { |
|
59 |
if (package_name == NULL) return false; |
|
60 |
int len = (int)strlen(package_name); |
|
61 |
return (len > 0 && len <= Symbol::max_length() && |
|
62 |
UTF8::is_legal_utf8((unsigned char *)package_name, len, false) && |
|
63 |
ClassFileParser::verify_unqualified_name(package_name, len, |
|
64 |
ClassFileParser::LegalClass)); |
|
65 |
} |
|
66 |
||
67 |
static char* get_module_name(oop module, TRAPS) { |
|
68 |
oop name_oop = java_lang_reflect_Module::name(module); |
|
69 |
if (name_oop == NULL) { |
|
70 |
THROW_MSG_NULL(vmSymbols::java_lang_NullPointerException(), "Null module name"); |
|
71 |
} |
|
72 |
char* module_name = java_lang_String::as_utf8_string(name_oop); |
|
73 |
if (!verify_module_name(module_name)) { |
|
74 |
THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), |
|
75 |
err_msg("Invalid module name: %s", |
|
76 |
module_name != NULL ? module_name : "NULL")); |
|
77 |
} |
|
78 |
return module_name; |
|
79 |
} |
|
80 |
||
81 |
static const char* get_module_version(jstring version) { |
|
82 |
if (version == NULL) { |
|
83 |
return NULL; |
|
84 |
} |
|
85 |
return java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(version)); |
|
86 |
} |
|
87 |
||
88 |
static ModuleEntryTable* get_module_entry_table(Handle h_loader, TRAPS) { |
|
89 |
// This code can be called during start-up, before the classLoader's classLoader data got |
|
90 |
// created. So, call register_loader() to make sure the classLoader data gets created. |
|
91 |
ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader, CHECK_NULL); |
|
92 |
return loader_cld->modules(); |
|
93 |
} |
|
94 |
||
95 |
static PackageEntryTable* get_package_entry_table(Handle h_loader, TRAPS) { |
|
96 |
// This code can be called during start-up, before the classLoader's classLoader data got |
|
97 |
// created. So, call register_loader() to make sure the classLoader data gets created. |
|
98 |
ClassLoaderData *loader_cld = SystemDictionary::register_loader(h_loader, CHECK_NULL); |
|
99 |
return loader_cld->packages(); |
|
100 |
} |
|
101 |
||
102 |
static ModuleEntry* get_module_entry(jobject module, TRAPS) { |
|
103 |
Handle module_h(THREAD, JNIHandles::resolve(module)); |
|
104 |
if (!java_lang_reflect_Module::is_instance(module_h())) { |
|
105 |
THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Bad module object"); |
|
106 |
} |
|
107 |
return java_lang_reflect_Module::module_entry(module_h(), CHECK_NULL); |
|
108 |
} |
|
109 |
||
110 |
static PackageEntry* get_package_entry(ModuleEntry* module_entry, jstring package, TRAPS) { |
|
111 |
ResourceMark rm(THREAD); |
|
112 |
if (package == NULL) return NULL; |
|
113 |
const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package)); |
|
114 |
if (package_name == NULL) return NULL; |
|
115 |
TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK_NULL); |
|
39616
f82b1f888578
8159262: Walking PackageEntry Export and ModuleEntry Reads Must Occur Only When Neccessary And Wait Until ClassLoader's Aliveness Determined
lfoltan
parents:
39290
diff
changeset
|
116 |
PackageEntryTable* package_entry_table = module_entry->loader_data()->packages(); |
36508 | 117 |
assert(package_entry_table != NULL, "Unexpected null package entry table"); |
118 |
return package_entry_table->lookup_only(pkg_symbol); |
|
119 |
} |
|
120 |
||
121 |
static PackageEntry* get_package_entry_by_name(Symbol* package, |
|
122 |
Handle h_loader, |
|
123 |
TRAPS) { |
|
124 |
if (package != NULL) { |
|
125 |
ResourceMark rm(THREAD); |
|
126 |
if (Modules::verify_package_name(package->as_C_string())) { |
|
127 |
PackageEntryTable* const package_entry_table = |
|
128 |
get_package_entry_table(h_loader, CHECK_NULL); |
|
129 |
assert(package_entry_table != NULL, "Unexpected null package entry table"); |
|
130 |
return package_entry_table->lookup_only(package); |
|
131 |
} |
|
132 |
} |
|
133 |
return NULL; |
|
134 |
} |
|
135 |
||
37773
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
136 |
// If using exploded build, append <java.home>/modules/module_name, if it exists, |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
137 |
// to the system boot class path in order for the boot loader to locate class files. |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
138 |
static void add_to_exploded_build_list(char *module_name, TRAPS) { |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
139 |
assert(!ClassLoader::has_jimage(), "Exploded build not applicable"); |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
140 |
// java.base is handled by os::set_boot_path |
36508 | 141 |
assert(strcmp(module_name, "java.base") != 0, "Unexpected java.base module name"); |
37773
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
142 |
|
36508 | 143 |
char file_sep = os::file_separator()[0]; |
144 |
size_t module_len = strlen(module_name); |
|
145 |
||
37773
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
146 |
const char* home = Arguments::get_java_home(); |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
147 |
size_t len = strlen(home) + module_len + 32; |
38733 | 148 |
char* path = NEW_C_HEAP_ARRAY(char, len, mtModule); |
37773
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
149 |
jio_snprintf(path, len, "%s%cmodules%c%s", home, file_sep, file_sep, module_name); |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
150 |
struct stat st; |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
151 |
// See if exploded module path exists |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
152 |
if ((os::stat(path, &st) != 0)) { |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
153 |
FREE_C_HEAP_ARRAY(char, path); |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
154 |
path = NULL; |
36508 | 155 |
} |
156 |
||
157 |
if (path != NULL) { |
|
158 |
HandleMark hm; |
|
159 |
Handle loader_lock = Handle(THREAD, SystemDictionary::system_loader_lock()); |
|
160 |
ObjectLocker ol(loader_lock, THREAD); |
|
38151
fffedc5e5cf8
8154110: Update class* and safepoint* logging subsystems
mockner
parents:
38014
diff
changeset
|
161 |
log_info(class, load)("opened: %s", path); |
36508 | 162 |
ClassLoader::add_to_list(path); |
163 |
} |
|
164 |
} |
|
165 |
||
166 |
bool Modules::is_package_defined(Symbol* package, Handle h_loader, TRAPS) { |
|
167 |
PackageEntry* res = get_package_entry_by_name(package, h_loader, CHECK_false); |
|
168 |
return res != NULL; |
|
169 |
} |
|
170 |
||
171 |
static void define_javabase_module(jobject module, jstring version, |
|
172 |
jstring location, jobjectArray packages, TRAPS) { |
|
173 |
ResourceMark rm(THREAD); |
|
174 |
||
175 |
Handle module_handle(THREAD, JNIHandles::resolve(module)); |
|
176 |
||
177 |
// Obtain java.base's module version |
|
178 |
const char* module_version = get_module_version(version); |
|
179 |
TempNewSymbol version_symbol; |
|
180 |
if (module_version != NULL) { |
|
181 |
version_symbol = SymbolTable::new_symbol(module_version, CHECK); |
|
182 |
} else { |
|
183 |
version_symbol = NULL; |
|
184 |
} |
|
185 |
||
186 |
// Obtain java.base's location |
|
187 |
const char* module_location = NULL; |
|
188 |
TempNewSymbol location_symbol = NULL; |
|
189 |
if (location != NULL) { |
|
190 |
module_location = |
|
191 |
java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(location)); |
|
192 |
if (module_location != NULL) { |
|
193 |
location_symbol = SymbolTable::new_symbol(module_location, CHECK); |
|
194 |
} |
|
195 |
} |
|
196 |
||
197 |
objArrayOop packages_oop = objArrayOop(JNIHandles::resolve(packages)); |
|
198 |
objArrayHandle packages_h(THREAD, packages_oop); |
|
199 |
int num_packages = (packages_h == NULL ? 0 : packages_h->length()); |
|
200 |
||
201 |
// Check that the list of packages has no duplicates and that the |
|
202 |
// packages are syntactically ok. |
|
203 |
GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages); |
|
204 |
for (int x = 0; x < num_packages; x++) { |
|
205 |
oop string_obj = packages_h->obj_at(x); |
|
206 |
||
207 |
if (string_obj == NULL || !string_obj->is_a(SystemDictionary::String_klass())) { |
|
208 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
209 |
"Bad package name for module: java.base"); |
|
210 |
} |
|
211 |
char *package_name = java_lang_String::as_utf8_string(string_obj); |
|
212 |
if (!Modules::verify_package_name(package_name)) { |
|
213 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
214 |
err_msg("Invalid package name: %s for module: java.base", package_name)); |
|
215 |
} |
|
216 |
Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, CHECK); |
|
217 |
// append_if_missing() returns FALSE if entry already exists. |
|
218 |
if (!pkg_list->append_if_missing(pkg_symbol)) { |
|
219 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
220 |
err_msg("Duplicate package name: %s for module java.base", |
|
221 |
package_name)); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
// Validate java_base's loader is the boot loader. |
|
226 |
oop loader = java_lang_reflect_Module::loader(module_handle()); |
|
227 |
if (loader != NULL) { |
|
228 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
229 |
"Class loader must be the boot class loader"); |
|
230 |
} |
|
231 |
Handle h_loader = Handle(THREAD, loader); |
|
232 |
||
233 |
// Ensure the boot loader's PackageEntryTable has been created |
|
234 |
PackageEntryTable* package_table = get_package_entry_table(h_loader, CHECK); |
|
235 |
assert(pkg_list->length() == 0 || package_table != NULL, "Bad package_table"); |
|
236 |
||
237 |
// Ensure java.base's ModuleEntry has been created |
|
238 |
assert(ModuleEntryTable::javabase_module() != NULL, "No ModuleEntry for java.base"); |
|
239 |
||
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
240 |
bool duplicate_javabase = false; |
36508 | 241 |
{ |
242 |
MutexLocker m1(Module_lock, THREAD); |
|
243 |
||
38014
8731fa11f766
8152949: Jigsaw crash when Klass in _fixup_module_field_list is unloaded
lfoltan
parents:
37248
diff
changeset
|
244 |
if (ModuleEntryTable::javabase_defined()) { |
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
245 |
duplicate_javabase = true; |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
246 |
} else { |
38014
8731fa11f766
8152949: Jigsaw crash when Klass in _fixup_module_field_list is unloaded
lfoltan
parents:
37248
diff
changeset
|
247 |
|
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
248 |
// Verify that all java.base packages created during bootstrapping are in |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
249 |
// pkg_list. If any are not in pkg_list, than a non-java.base class was |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
250 |
// loaded erroneously pre java.base module definition. |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
251 |
package_table->verify_javabase_packages(pkg_list); |
36508 | 252 |
|
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
253 |
// loop through and add any new packages for java.base |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
254 |
PackageEntry* pkg; |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
255 |
for (int x = 0; x < pkg_list->length(); x++) { |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
256 |
// Some of java.base's packages were added early in bootstrapping, ignore duplicates. |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
257 |
if (package_table->lookup_only(pkg_list->at(x)) == NULL) { |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
258 |
pkg = package_table->locked_create_entry_or_null(pkg_list->at(x), ModuleEntryTable::javabase_module()); |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
259 |
assert(pkg != NULL, "Unable to create a java.base package entry"); |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
260 |
} |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
261 |
// Unable to have a GrowableArray of TempNewSymbol. Must decrement the refcount of |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
262 |
// the Symbol* that was created above for each package. The refcount was incremented |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
263 |
// by SymbolTable::new_symbol and as well by the PackageEntry creation. |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
264 |
pkg_list->at(x)->decrement_refcount(); |
36508 | 265 |
} |
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
266 |
|
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
267 |
// Finish defining java.base's ModuleEntry |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
268 |
ModuleEntryTable::finalize_javabase(module_handle, version_symbol, location_symbol); |
36508 | 269 |
} |
38732
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
270 |
} |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
271 |
if (duplicate_javabase) { |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
272 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
bb77e0dcc7e5
8158060: BasicLayerTest causes fatal error: Thread holding lock at safepoint that vm can block on: Module_lock
hseigel
parents:
38207
diff
changeset
|
273 |
"Module java.base is already defined"); |
36508 | 274 |
} |
275 |
||
276 |
log_debug(modules)("define_javabase_module(): Definition of module: java.base," |
|
277 |
" version: %s, location: %s, package #: %d", |
|
278 |
module_version != NULL ? module_version : "NULL", |
|
279 |
module_location != NULL ? module_location : "NULL", |
|
280 |
pkg_list->length()); |
|
281 |
||
282 |
// packages defined to java.base |
|
283 |
for (int x = 0; x < pkg_list->length(); x++) { |
|
284 |
log_trace(modules)("define_javabase_module(): creation of package %s for module java.base", |
|
285 |
(pkg_list->at(x))->as_C_string()); |
|
286 |
} |
|
287 |
||
288 |
// Patch any previously loaded classes' module field with java.base's jlr.Module. |
|
289 |
ModuleEntryTable::patch_javabase_entries(module_handle); |
|
290 |
} |
|
291 |
||
292 |
void Modules::define_module(jobject module, jstring version, |
|
293 |
jstring location, jobjectArray packages, TRAPS) { |
|
294 |
ResourceMark rm(THREAD); |
|
295 |
||
296 |
if (module == NULL) { |
|
297 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object"); |
|
298 |
} |
|
299 |
Handle module_handle(THREAD, JNIHandles::resolve(module)); |
|
300 |
if (!java_lang_reflect_Module::is_subclass(module_handle->klass())) { |
|
301 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
302 |
"module is not a subclass of java.lang.reflect.Module"); |
|
303 |
} |
|
304 |
||
305 |
char* module_name = get_module_name(module_handle(), CHECK); |
|
306 |
if (module_name == NULL) { |
|
307 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
308 |
"Module name cannot be null"); |
|
309 |
} |
|
310 |
||
311 |
// Special handling of java.base definition |
|
312 |
if (strcmp(module_name, "java.base") == 0) { |
|
313 |
define_javabase_module(module, version, location, packages, CHECK); |
|
314 |
return; |
|
315 |
} |
|
316 |
||
317 |
const char* module_version = get_module_version(version); |
|
318 |
||
319 |
objArrayOop packages_oop = objArrayOop(JNIHandles::resolve(packages)); |
|
320 |
objArrayHandle packages_h(THREAD, packages_oop); |
|
321 |
int num_packages = (packages_h == NULL ? 0 : packages_h->length()); |
|
322 |
||
323 |
// Check that the list of packages has no duplicates and that the |
|
324 |
// packages are syntactically ok. |
|
325 |
GrowableArray<Symbol*>* pkg_list = new GrowableArray<Symbol*>(num_packages); |
|
326 |
for (int x = 0; x < num_packages; x++) { |
|
327 |
oop string_obj = packages_h->obj_at(x); |
|
328 |
||
329 |
if (string_obj == NULL || !string_obj->is_a(SystemDictionary::String_klass())) { |
|
330 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
331 |
err_msg("Bad package name for module: %s", module_name)); |
|
332 |
} |
|
333 |
char *package_name = java_lang_String::as_utf8_string(string_obj); |
|
334 |
if (!verify_package_name(package_name)) { |
|
335 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
336 |
err_msg("Invalid package name: %s for module: %s", |
|
337 |
package_name, module_name)); |
|
338 |
} |
|
339 |
Symbol* pkg_symbol = SymbolTable::new_symbol(package_name, CHECK); |
|
340 |
// append_if_missing() returns FALSE if entry already exists. |
|
341 |
if (!pkg_list->append_if_missing(pkg_symbol)) { |
|
342 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
343 |
err_msg("Duplicate package name: %s for module %s", |
|
344 |
package_name, module_name)); |
|
345 |
} |
|
346 |
} |
|
347 |
||
348 |
oop loader = java_lang_reflect_Module::loader(module_handle()); |
|
349 |
// Make sure loader is not the sun.reflect.DelegatingClassLoader. |
|
350 |
if (loader != java_lang_ClassLoader::non_reflection_class_loader(loader)) { |
|
351 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
352 |
"Class loader is an invalid delegating class loader"); |
|
353 |
} |
|
354 |
Handle h_loader = Handle(THREAD, loader); |
|
355 |
||
356 |
// Check that loader is a subclass of java.lang.ClassLoader. |
|
357 |
if (loader != NULL && !java_lang_ClassLoader::is_subclass(h_loader->klass())) { |
|
358 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
359 |
"Class loader is not a subclass of java.lang.ClassLoader"); |
|
360 |
} |
|
361 |
||
362 |
ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK); |
|
363 |
assert(module_table != NULL, "module entry table shouldn't be null"); |
|
364 |
||
365 |
// Create symbol* entry for module name. |
|
366 |
TempNewSymbol module_symbol = SymbolTable::new_symbol(module_name, CHECK); |
|
367 |
||
368 |
int dupl_pkg_index = -1; |
|
369 |
bool dupl_modules = false; |
|
370 |
||
371 |
// Create symbol* entry for module version. |
|
372 |
TempNewSymbol version_symbol; |
|
373 |
if (module_version != NULL) { |
|
374 |
version_symbol = SymbolTable::new_symbol(module_version, CHECK); |
|
375 |
} else { |
|
376 |
version_symbol = NULL; |
|
377 |
} |
|
378 |
||
379 |
// Create symbol* entry for module location. |
|
380 |
const char* module_location = NULL; |
|
381 |
TempNewSymbol location_symbol = NULL; |
|
382 |
if (location != NULL) { |
|
383 |
module_location = |
|
384 |
java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(location)); |
|
385 |
if (module_location != NULL) { |
|
386 |
location_symbol = SymbolTable::new_symbol(module_location, CHECK); |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
ClassLoaderData* loader_data = ClassLoaderData::class_loader_data_or_null(h_loader()); |
|
391 |
assert(loader_data != NULL, "class loader data shouldn't be null"); |
|
392 |
||
393 |
PackageEntryTable* package_table = NULL; |
|
394 |
{ |
|
395 |
MutexLocker ml(Module_lock, THREAD); |
|
396 |
||
397 |
if (num_packages > 0) { |
|
398 |
package_table = get_package_entry_table(h_loader, CHECK); |
|
399 |
assert(package_table != NULL, "Missing package_table"); |
|
400 |
||
401 |
// Check that none of the packages exist in the class loader's package table. |
|
402 |
for (int x = 0; x < pkg_list->length(); x++) { |
|
403 |
if (package_table->lookup_only(pkg_list->at(x)) != NULL) { |
|
404 |
// This could be because the module was already defined. If so, |
|
405 |
// report that error instead of the package error. |
|
406 |
if (module_table->lookup_only(module_symbol) != NULL) { |
|
407 |
dupl_modules = true; |
|
408 |
} else { |
|
409 |
dupl_pkg_index = x; |
|
410 |
} |
|
411 |
break; |
|
412 |
} |
|
413 |
} |
|
414 |
} // if (num_packages > 0)... |
|
415 |
||
416 |
// Add the module and its packages. |
|
417 |
if (!dupl_modules && dupl_pkg_index == -1) { |
|
418 |
// Create the entry for this module in the class loader's module entry table. |
|
419 |
||
420 |
ModuleEntry* module_entry = module_table->locked_create_entry_or_null(module_handle, module_symbol, |
|
421 |
version_symbol, location_symbol, loader_data); |
|
422 |
||
423 |
if (module_entry == NULL) { |
|
424 |
dupl_modules = true; |
|
425 |
} else { |
|
426 |
// Add the packages. |
|
427 |
assert(pkg_list->length() == 0 || package_table != NULL, "Bad package table"); |
|
428 |
PackageEntry* pkg; |
|
429 |
for (int y = 0; y < pkg_list->length(); y++) { |
|
430 |
pkg = package_table->locked_create_entry_or_null(pkg_list->at(y), module_entry); |
|
431 |
assert(pkg != NULL, "Unable to create a module's package entry"); |
|
432 |
||
433 |
// Unable to have a GrowableArray of TempNewSymbol. Must decrement the refcount of |
|
434 |
// the Symbol* that was created above for each package. The refcount was incremented |
|
435 |
// by SymbolTable::new_symbol and as well by the PackageEntry creation. |
|
436 |
pkg_list->at(y)->decrement_refcount(); |
|
437 |
} |
|
438 |
||
439 |
// Store pointer to ModuleEntry record in java.lang.reflect.Module object. |
|
440 |
java_lang_reflect_Module::set_module_entry(module_handle(), module_entry); |
|
441 |
} |
|
442 |
} |
|
443 |
} // Release the lock |
|
444 |
||
445 |
// any errors ? |
|
446 |
if (dupl_modules) { |
|
447 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
448 |
err_msg("Module %s is already defined", module_name)); |
|
449 |
} |
|
450 |
if (dupl_pkg_index != -1) { |
|
451 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
452 |
err_msg("Package %s for module %s already exists for class loader", |
|
453 |
pkg_list->at(dupl_pkg_index)->as_C_string(), module_name)); |
|
454 |
} |
|
455 |
||
456 |
if (log_is_enabled(Debug, modules)) { |
|
37242 | 457 |
outputStream* logst = Log(modules)::debug_stream(); |
36508 | 458 |
logst->print("define_module(): creation of module: %s, version: %s, location: %s, ", |
459 |
module_name, module_version != NULL ? module_version : "NULL", |
|
460 |
module_location != NULL ? module_location : "NULL"); |
|
461 |
loader_data->print_value_on(logst); |
|
462 |
logst->print_cr(", package #: %d", pkg_list->length()); |
|
463 |
for (int y = 0; y < pkg_list->length(); y++) { |
|
464 |
log_trace(modules)("define_module(): creation of package %s for module %s", |
|
465 |
(pkg_list->at(y))->as_C_string(), module_name); |
|
466 |
} |
|
467 |
} |
|
468 |
||
37773
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
469 |
// If the module is defined to the boot loader and an exploded build is being |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
470 |
// used, prepend <java.home>/modules/modules_name, if it exists, to the system boot class path. |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
471 |
if (loader == NULL && |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
472 |
!Universe::is_module_initialized() && |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
473 |
!ClassLoader::has_jimage()) { |
e5b3e9732c3c
8154956: Module system implementation refresh (4/2016)
alanb
parents:
37248
diff
changeset
|
474 |
add_to_exploded_build_list(module_name, CHECK); |
36508 | 475 |
} |
476 |
} |
|
477 |
||
478 |
void Modules::set_bootloader_unnamed_module(jobject module, TRAPS) { |
|
479 |
ResourceMark rm(THREAD); |
|
480 |
||
481 |
if (module == NULL) { |
|
482 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), "Null module object"); |
|
483 |
} |
|
484 |
Handle module_handle(THREAD, JNIHandles::resolve(module)); |
|
485 |
if (!java_lang_reflect_Module::is_subclass(module_handle->klass())) { |
|
486 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
487 |
"module is not a subclass of java.lang.reflect.Module"); |
|
488 |
} |
|
489 |
||
490 |
// Ensure that this is an unnamed module |
|
491 |
oop name = java_lang_reflect_Module::name(module_handle()); |
|
492 |
if (name != NULL) { |
|
493 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
494 |
"boot loader's unnamed module's java.lang.reflect.Module has a name"); |
|
495 |
} |
|
496 |
||
497 |
// Validate java_base's loader is the boot loader. |
|
498 |
oop loader = java_lang_reflect_Module::loader(module_handle()); |
|
499 |
if (loader != NULL) { |
|
500 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
501 |
"Class loader must be the boot class loader"); |
|
502 |
} |
|
503 |
Handle h_loader = Handle(THREAD, loader); |
|
504 |
||
505 |
log_debug(modules)("set_bootloader_unnamed_module(): recording unnamed module for boot loader"); |
|
506 |
||
507 |
// Ensure the boot loader's PackageEntryTable has been created |
|
508 |
ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK); |
|
509 |
||
510 |
// Set java.lang.reflect.Module for the boot loader's unnamed module |
|
511 |
ModuleEntry* unnamed_module = module_table->unnamed_module(); |
|
512 |
assert(unnamed_module != NULL, "boot loader's unnamed ModuleEntry not defined"); |
|
513 |
unnamed_module->set_module(ClassLoaderData::the_null_class_loader_data()->add_handle(module_handle)); |
|
514 |
// Store pointer to the ModuleEntry in the unnamed module's java.lang.reflect.Module object. |
|
515 |
java_lang_reflect_Module::set_module_entry(module_handle(), unnamed_module); |
|
516 |
} |
|
517 |
||
518 |
void Modules::add_module_exports(jobject from_module, jstring package, jobject to_module, TRAPS) { |
|
519 |
if (package == NULL) { |
|
520 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
521 |
"package is null"); |
|
522 |
} |
|
523 |
if (from_module == NULL) { |
|
524 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
525 |
"from_module is null"); |
|
526 |
} |
|
527 |
ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK); |
|
528 |
if (from_module_entry == NULL) { |
|
529 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
530 |
"from_module cannot be found"); |
|
531 |
} |
|
532 |
||
533 |
// All packages in unnamed are exported by default. |
|
534 |
if (!from_module_entry->is_named()) return; |
|
535 |
||
536 |
ModuleEntry* to_module_entry; |
|
537 |
if (to_module == NULL) { |
|
538 |
to_module_entry = NULL; // It's an unqualified export. |
|
539 |
} else { |
|
540 |
to_module_entry = get_module_entry(to_module, CHECK); |
|
541 |
if (to_module_entry == NULL) { |
|
542 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
543 |
"to_module is invalid"); |
|
544 |
} |
|
545 |
} |
|
546 |
||
547 |
PackageEntry *package_entry = get_package_entry(from_module_entry, package, CHECK); |
|
548 |
ResourceMark rm(THREAD); |
|
549 |
if (package_entry == NULL) { |
|
550 |
const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package)); |
|
551 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
552 |
err_msg("Package %s not found in from_module %s", |
|
553 |
package_name != NULL ? package_name : "", |
|
554 |
from_module_entry->name()->as_C_string())); |
|
555 |
} |
|
556 |
if (package_entry->module() != from_module_entry) { |
|
557 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
558 |
err_msg("Package: %s found in module %s, not in from_module: %s", |
|
559 |
package_entry->name()->as_C_string(), |
|
560 |
package_entry->module()->name()->as_C_string(), |
|
561 |
from_module_entry->name()->as_C_string())); |
|
562 |
} |
|
563 |
||
564 |
log_debug(modules)("add_module_exports(): package %s in module %s is exported to module %s", |
|
565 |
package_entry->name()->as_C_string(), |
|
566 |
from_module_entry->name()->as_C_string(), |
|
567 |
to_module_entry == NULL ? "NULL" : |
|
568 |
to_module_entry->is_named() ? |
|
569 |
to_module_entry->name()->as_C_string() : UNNAMED_MODULE); |
|
570 |
||
39290
0cc9f5028562
8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
38733
diff
changeset
|
571 |
// Do nothing if modules are the same. |
0cc9f5028562
8156871: Possible concurrency issue with JVM_AddModuleExports
lfoltan
parents:
38733
diff
changeset
|
572 |
if (from_module_entry != to_module_entry) { |
36508 | 573 |
package_entry->set_exported(to_module_entry); |
574 |
} |
|
575 |
} |
|
576 |
||
577 |
||
578 |
void Modules::add_module_exports_qualified(jobject from_module, jstring package, |
|
579 |
jobject to_module, TRAPS) { |
|
580 |
if (to_module == NULL) { |
|
581 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
582 |
"to_module is null"); |
|
583 |
} |
|
584 |
add_module_exports(from_module, package, to_module, CHECK); |
|
585 |
} |
|
586 |
||
587 |
void Modules::add_reads_module(jobject from_module, jobject to_module, TRAPS) { |
|
588 |
if (from_module == NULL) { |
|
589 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
590 |
"from_module is null"); |
|
591 |
} |
|
592 |
||
593 |
ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK); |
|
594 |
if (from_module_entry == NULL) { |
|
595 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
596 |
"from_module is not valid"); |
|
597 |
} |
|
598 |
||
599 |
ModuleEntry* to_module_entry; |
|
600 |
if (to_module != NULL) { |
|
601 |
to_module_entry = get_module_entry(to_module, CHECK); |
|
602 |
if (to_module_entry == NULL) { |
|
603 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
604 |
"to_module is invalid"); |
|
605 |
} |
|
606 |
} else { |
|
607 |
to_module_entry = NULL; |
|
608 |
} |
|
609 |
||
610 |
ResourceMark rm(THREAD); |
|
611 |
log_debug(modules)("add_reads_module(): Adding read from module %s to module %s", |
|
612 |
from_module_entry->is_named() ? |
|
613 |
from_module_entry->name()->as_C_string() : UNNAMED_MODULE, |
|
614 |
to_module_entry == NULL ? "all unnamed" : |
|
615 |
(to_module_entry->is_named() ? |
|
616 |
to_module_entry->name()->as_C_string() : UNNAMED_MODULE)); |
|
617 |
||
618 |
// if modules are the same or if from_module is unnamed then no need to add the read. |
|
619 |
if (from_module_entry != to_module_entry && from_module_entry->is_named()) { |
|
620 |
from_module_entry->add_read(to_module_entry); |
|
621 |
} |
|
622 |
} |
|
623 |
||
624 |
jboolean Modules::can_read_module(jobject asking_module, jobject target_module, TRAPS) { |
|
625 |
if (asking_module == NULL) { |
|
626 |
THROW_MSG_(vmSymbols::java_lang_NullPointerException(), |
|
627 |
"asking_module is null", JNI_FALSE); |
|
628 |
} |
|
629 |
||
630 |
ModuleEntry* asking_module_entry = get_module_entry(asking_module, CHECK_false); |
|
631 |
if (asking_module_entry == NULL) { |
|
632 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
633 |
"asking_module is invalid", JNI_FALSE); |
|
634 |
} |
|
635 |
||
636 |
// Calling can_read_all_unnamed() with NULL tests if a module is loose. |
|
637 |
if (target_module == NULL) { |
|
638 |
return asking_module_entry->can_read_all_unnamed(); |
|
639 |
} |
|
640 |
||
641 |
ModuleEntry* target_module_entry = get_module_entry(target_module, CHECK_false); |
|
642 |
if (target_module_entry == NULL) { |
|
643 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
644 |
"target_module is invalid", JNI_FALSE); |
|
645 |
} |
|
646 |
||
647 |
ResourceMark rm(THREAD); |
|
648 |
log_debug(modules)("can_read_module(): module %s trying to read module %s, allowed = %s", |
|
649 |
asking_module_entry->is_named() ? |
|
650 |
asking_module_entry->name()->as_C_string() : UNNAMED_MODULE, |
|
651 |
target_module_entry->is_named() ? |
|
652 |
target_module_entry->name()->as_C_string() : UNNAMED_MODULE, |
|
653 |
BOOL_TO_STR(asking_module_entry == target_module_entry || |
|
654 |
(asking_module_entry->can_read_all_unnamed() && |
|
655 |
!target_module_entry->is_named()) || |
|
656 |
asking_module_entry->can_read(target_module_entry))); |
|
657 |
||
658 |
// Return true if: |
|
659 |
// 1. the modules are the same, or |
|
660 |
// 2. the asking_module is unnamed (because unnamed modules read everybody), or |
|
661 |
// 3. the asking_module is loose and the target module is unnamed, or |
|
662 |
// 4. if can_read() returns true. |
|
663 |
if (asking_module_entry == target_module_entry || |
|
664 |
(asking_module_entry->can_read_all_unnamed() && !target_module_entry->is_named())) { |
|
665 |
return true; |
|
666 |
} |
|
667 |
return asking_module_entry->can_read(target_module_entry); |
|
668 |
} |
|
669 |
||
670 |
jboolean Modules::is_exported_to_module(jobject from_module, jstring package, |
|
671 |
jobject to_module, TRAPS) { |
|
672 |
if (package == NULL) { |
|
673 |
THROW_MSG_(vmSymbols::java_lang_NullPointerException(), |
|
674 |
"package is null", JNI_FALSE); |
|
675 |
} |
|
676 |
if (from_module == NULL) { |
|
677 |
THROW_MSG_(vmSymbols::java_lang_NullPointerException(), |
|
678 |
"from_module is null", JNI_FALSE); |
|
679 |
} |
|
680 |
ModuleEntry* from_module_entry = get_module_entry(from_module, CHECK_false); |
|
681 |
if (from_module_entry == NULL) { |
|
682 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
683 |
"from_module is invalid", JNI_FALSE); |
|
684 |
} |
|
685 |
ModuleEntry* to_module_entry; |
|
686 |
if (to_module == NULL) { |
|
687 |
THROW_MSG_(vmSymbols::java_lang_NullPointerException(), |
|
688 |
"to_module is null", JNI_FALSE); |
|
689 |
} |
|
690 |
to_module_entry = get_module_entry(to_module, CHECK_false); |
|
691 |
if (to_module_entry == NULL) { |
|
692 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
693 |
"to_module is invalid", JNI_FALSE); |
|
694 |
} |
|
695 |
||
696 |
PackageEntry *package_entry = get_package_entry(from_module_entry, package, |
|
697 |
CHECK_false); |
|
698 |
ResourceMark rm(THREAD); |
|
699 |
if (package_entry == NULL) { |
|
700 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
701 |
err_msg("Package not found in from_module: %s", |
|
702 |
from_module_entry->is_named() ? |
|
703 |
from_module_entry->name()->as_C_string() : UNNAMED_MODULE), |
|
704 |
JNI_FALSE); |
|
705 |
} |
|
706 |
if (package_entry->module() != from_module_entry) { |
|
707 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
708 |
err_msg("Package: %s found in module %s, not in from_module: %s", |
|
709 |
package_entry->name()->as_C_string(), |
|
710 |
package_entry->module()->is_named() ? |
|
711 |
package_entry->module()->name()->as_C_string() : UNNAMED_MODULE, |
|
712 |
from_module_entry->is_named() ? |
|
713 |
from_module_entry->name()->as_C_string() : UNNAMED_MODULE), |
|
714 |
JNI_FALSE); |
|
715 |
} |
|
716 |
||
717 |
log_debug(modules)("is_exported_to_module: package %s from module %s checking" |
|
718 |
" if exported to module %s, exported? = %s", |
|
719 |
package_entry->name()->as_C_string(), |
|
720 |
from_module_entry->is_named() ? |
|
721 |
from_module_entry->name()->as_C_string() : UNNAMED_MODULE, |
|
722 |
to_module_entry->is_named() ? |
|
723 |
to_module_entry->name()->as_C_string() : UNNAMED_MODULE, |
|
724 |
BOOL_TO_STR(!from_module_entry->is_named() || |
|
725 |
package_entry->is_unqual_exported() || |
|
726 |
from_module_entry == to_module_entry || |
|
727 |
package_entry->is_qexported_to(to_module_entry))); |
|
728 |
||
729 |
// Return true if: |
|
730 |
// 1. from_module is unnamed because unnamed modules export all their packages (by default), or |
|
731 |
// 2. if the package is unqualifiedly exported, or |
|
732 |
// 3. if the modules are the same, or |
|
733 |
// 4. if the package is exported to to_module |
|
734 |
return (!from_module_entry->is_named() || |
|
735 |
package_entry->is_unqual_exported() || |
|
736 |
from_module_entry == to_module_entry || |
|
737 |
package_entry->is_qexported_to(to_module_entry)); |
|
738 |
} |
|
739 |
||
740 |
// This method is called by JFR and JNI. |
|
741 |
jobject Modules::get_module(jclass clazz, TRAPS) { |
|
742 |
assert(ModuleEntryTable::javabase_defined(), "Attempt to call get_module before java.base is defined"); |
|
743 |
||
744 |
if (clazz == NULL) { |
|
745 |
THROW_MSG_(vmSymbols::java_lang_NullPointerException(), |
|
746 |
"class is null", JNI_FALSE); |
|
747 |
} |
|
748 |
oop mirror = JNIHandles::resolve_non_null(clazz); |
|
749 |
if (mirror == NULL) { |
|
750 |
log_debug(modules)("get_module(): no mirror, returning NULL"); |
|
751 |
return NULL; |
|
752 |
} |
|
753 |
if (!java_lang_Class::is_instance(mirror)) { |
|
754 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
755 |
"Invalid class", JNI_FALSE); |
|
756 |
} |
|
757 |
||
758 |
oop module = java_lang_Class::module(mirror); |
|
759 |
||
760 |
assert(module != NULL, "java.lang.Class module field not set"); |
|
761 |
assert(java_lang_reflect_Module::is_subclass(module->klass()), "Module is not a java.lang.reflect.Module"); |
|
762 |
||
763 |
if (log_is_enabled(Debug, modules)) { |
|
764 |
ResourceMark rm(THREAD); |
|
37242 | 765 |
outputStream* logst = Log(modules)::debug_stream(); |
36508 | 766 |
Klass* klass = java_lang_Class::as_Klass(mirror); |
767 |
oop module_name = java_lang_reflect_Module::name(module); |
|
768 |
if (module_name != NULL) { |
|
769 |
logst->print("get_module(): module "); |
|
770 |
java_lang_String::print(module_name, tty); |
|
771 |
} else { |
|
772 |
logst->print("get_module(): Unamed Module"); |
|
773 |
} |
|
774 |
if (klass != NULL) { |
|
775 |
logst->print_cr(" for class %s", klass->external_name()); |
|
776 |
} else { |
|
777 |
logst->print_cr(" for primitive class"); |
|
778 |
} |
|
779 |
} |
|
780 |
||
781 |
return JNIHandles::make_local(THREAD, module); |
|
782 |
} |
|
783 |
||
784 |
||
785 |
jobject Modules::get_module_by_package_name(jobject loader, jstring package, TRAPS) { |
|
786 |
ResourceMark rm(THREAD); |
|
787 |
assert(ModuleEntryTable::javabase_defined(), |
|
788 |
"Attempt to call get_module_from_pkg before java.base is defined"); |
|
789 |
||
790 |
if (NULL == package) { |
|
791 |
THROW_MSG_(vmSymbols::java_lang_NullPointerException(), |
|
792 |
"package is null", JNI_FALSE); |
|
793 |
} |
|
794 |
const char* package_str = |
|
795 |
java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package)); |
|
796 |
if (NULL == package_str) { |
|
797 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
798 |
"Invalid package", JNI_FALSE); |
|
799 |
} |
|
800 |
||
801 |
Handle h_loader (THREAD, JNIHandles::resolve(loader)); |
|
802 |
// Check that loader is a subclass of java.lang.ClassLoader. |
|
803 |
if (loader != NULL && !java_lang_ClassLoader::is_subclass(h_loader->klass())) { |
|
804 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
|
805 |
"Class loader is not a subclass of java.lang.ClassLoader", JNI_FALSE); |
|
806 |
} |
|
807 |
||
808 |
if (strlen(package_str) == 0) { |
|
809 |
// Return the unnamed module |
|
810 |
ModuleEntryTable* module_table = get_module_entry_table(h_loader, CHECK_NULL); |
|
811 |
if (NULL == module_table) return NULL; |
|
812 |
const ModuleEntry* const unnamed_module = module_table->unnamed_module(); |
|
813 |
return JNIHandles::make_local(THREAD, JNIHandles::resolve(unnamed_module->module())); |
|
814 |
||
815 |
} else { |
|
816 |
TempNewSymbol package_sym = SymbolTable::new_symbol(package_str, CHECK_NULL); |
|
817 |
return get_module(package_sym, h_loader, CHECK_NULL); |
|
818 |
} |
|
819 |
return NULL; |
|
820 |
} |
|
821 |
||
822 |
||
39706 | 823 |
jobject Modules::get_named_module(Handle h_loader, const char* package_str, TRAPS) { |
824 |
assert(ModuleEntryTable::javabase_defined(), |
|
825 |
"Attempt to call get_named_module before java.base is defined"); |
|
826 |
assert(h_loader.is_null() || java_lang_ClassLoader::is_subclass(h_loader->klass()), |
|
827 |
"Class loader is not a subclass of java.lang.ClassLoader"); |
|
828 |
assert(package_str != NULL, "the package_str should not be NULL"); |
|
829 |
||
830 |
if (strlen(package_str) == 0) { |
|
831 |
return NULL; |
|
832 |
} |
|
833 |
TempNewSymbol package_sym = SymbolTable::new_symbol(package_str, CHECK_NULL); |
|
834 |
const PackageEntry* const pkg_entry = |
|
835 |
get_package_entry_by_name(package_sym, h_loader, THREAD); |
|
836 |
const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL); |
|
837 |
||
838 |
if (module_entry != NULL && module_entry->module() != NULL && module_entry->is_named()) { |
|
839 |
return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module())); |
|
840 |
} |
|
841 |
return NULL; |
|
842 |
} |
|
843 |
||
844 |
||
36508 | 845 |
// This method is called by JFR and by the above method. |
846 |
jobject Modules::get_module(Symbol* package_name, Handle h_loader, TRAPS) { |
|
847 |
const PackageEntry* const pkg_entry = |
|
848 |
get_package_entry_by_name(package_name, h_loader, THREAD); |
|
849 |
const ModuleEntry* const module_entry = (pkg_entry != NULL ? pkg_entry->module() : NULL); |
|
850 |
||
851 |
if (module_entry != NULL && |
|
852 |
module_entry->module() != NULL) { |
|
853 |
return JNIHandles::make_local(THREAD, JNIHandles::resolve(module_entry->module())); |
|
854 |
} |
|
855 |
||
856 |
return NULL; |
|
857 |
} |
|
858 |
||
859 |
void Modules::add_module_package(jobject module, jstring package, TRAPS) { |
|
860 |
ResourceMark rm(THREAD); |
|
861 |
||
862 |
if (module == NULL) { |
|
863 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
864 |
"module is null"); |
|
865 |
} |
|
866 |
if (package == NULL) { |
|
867 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
868 |
"package is null"); |
|
869 |
} |
|
870 |
ModuleEntry* module_entry = get_module_entry(module, CHECK); |
|
871 |
if (module_entry == NULL) { |
|
872 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
873 |
"module is invalid"); |
|
874 |
} |
|
875 |
if (!module_entry->is_named()) { |
|
876 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
877 |
"module cannot be an unnamed module"); |
|
878 |
} |
|
879 |
char *package_name = java_lang_String::as_utf8_string( |
|
880 |
JNIHandles::resolve_non_null(package)); |
|
881 |
if (package_name == NULL) { |
|
882 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Bad package"); |
|
883 |
} |
|
884 |
if (!verify_package_name(package_name)) { |
|
885 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
886 |
err_msg("Invalid package name: %s", package_name)); |
|
887 |
} |
|
888 |
||
889 |
log_debug(modules)("add_module_package(): Adding package %s to module %s", |
|
890 |
package_name, module_entry->name()->as_C_string()); |
|
891 |
||
892 |
TempNewSymbol pkg_symbol = SymbolTable::new_symbol(package_name, CHECK); |
|
39616
f82b1f888578
8159262: Walking PackageEntry Export and ModuleEntry Reads Must Occur Only When Neccessary And Wait Until ClassLoader's Aliveness Determined
lfoltan
parents:
39290
diff
changeset
|
893 |
PackageEntryTable* package_table = module_entry->loader_data()->packages(); |
36508 | 894 |
assert(package_table != NULL, "Missing package_table"); |
895 |
||
896 |
bool pkg_exists = false; |
|
897 |
{ |
|
898 |
MutexLocker ml(Module_lock, THREAD); |
|
899 |
||
900 |
// Check that the package does not exist in the class loader's package table. |
|
901 |
if (!package_table->lookup_only(pkg_symbol)) { |
|
902 |
PackageEntry* pkg = package_table->locked_create_entry_or_null(pkg_symbol, module_entry); |
|
903 |
assert(pkg != NULL, "Unable to create a module's package entry"); |
|
904 |
} else { |
|
905 |
pkg_exists = true; |
|
906 |
} |
|
907 |
} |
|
908 |
if (pkg_exists) { |
|
909 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
910 |
err_msg("Package %s already exists for class loader", package_name)); |
|
911 |
} |
|
912 |
} |
|
913 |
||
914 |
// Export package in module to all unnamed modules. |
|
915 |
void Modules::add_module_exports_to_all_unnamed(jobject module, jstring package, TRAPS) { |
|
916 |
if (module == NULL) { |
|
917 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
918 |
"module is null"); |
|
919 |
} |
|
920 |
if (package == NULL) { |
|
921 |
THROW_MSG(vmSymbols::java_lang_NullPointerException(), |
|
922 |
"package is null"); |
|
923 |
} |
|
924 |
ModuleEntry* module_entry = get_module_entry(module, CHECK); |
|
925 |
if (module_entry == NULL) { |
|
926 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
927 |
"module is invalid"); |
|
928 |
} |
|
929 |
||
930 |
if (module_entry->is_named()) { // No-op for unnamed module. |
|
931 |
PackageEntry *package_entry = get_package_entry(module_entry, package, CHECK); |
|
932 |
ResourceMark rm(THREAD); |
|
933 |
if (package_entry == NULL) { |
|
934 |
const char *package_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(package)); |
|
935 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
936 |
err_msg("Package %s not found in module %s", |
|
937 |
package_name != NULL ? package_name : "", |
|
938 |
module_entry->name()->as_C_string())); |
|
939 |
} |
|
940 |
if (package_entry->module() != module_entry) { |
|
941 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), |
|
942 |
err_msg("Package: %s found in module %s, not in module: %s", |
|
943 |
package_entry->name()->as_C_string(), |
|
944 |
package_entry->module()->name()->as_C_string(), |
|
945 |
module_entry->name()->as_C_string())); |
|
946 |
} |
|
947 |
||
948 |
log_debug(modules)("add_module_exports_to_all_unnamed(): package %s in module" |
|
949 |
" %s is exported to all unnamed modules", |
|
950 |
package_entry->name()->as_C_string(), |
|
951 |
module_entry->name()->as_C_string()); |
|
952 |
||
953 |
// Mark package as exported to all unnamed modules, unless already |
|
954 |
// unqualifiedly exported. |
|
955 |
if (!package_entry->is_unqual_exported()) { |
|
956 |
package_entry->set_is_exported_allUnnamed(); |
|
957 |
} |
|
958 |
} |
|
959 |
} |