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 #ifndef SHARE_VM_CLASSFILE_PACKAGEENTRY_HPP 26 #define SHARE_VM_CLASSFILE_PACKAGEENTRY_HPP 27 28 #include "classfile/moduleEntry.hpp" 29 #include "oops/symbol.hpp" 30 #include "utilities/growableArray.hpp" 31 #include "utilities/hashtable.hpp" 32 #include "utilities/ostream.hpp" 33 34 // A PackageEntry basically represents a Java package. It contains: 35 // - Symbol* containing the package's name. 36 // - ModuleEntry* for this package's containing module. 37 // - a flag indicating if package is exported unqualifiedly 38 // - a flag indicating if this package is exported to all unnamed modules. 39 // - a growable array containing other module entries that this 40 // package is exported to. 41 // 42 // Packages can be exported in the following 3 ways: 43 // - not exported: the package does not have qualified or unqualified exports. 44 // - qualified exports: the package has been explicitly qualified to at least 45 // one particular module or has been qualifiedly exported 46 // to all unnamed modules. 47 // Note: _is_exported_allUnnamed is a form of a qualified 48 // export. It is equivalent to the package being 49 // explicitly exported to all current and future unnamed modules. 50 // - unqualified exports: the package is exported to all modules. 51 // 52 // A package can transition from: 53 // - being not exported, to being exported either in a qualified or unqualified manner 54 // - being qualifiedly exported, to unqualifiedly exported. Its exported scope is widened. 55 // 56 // A package cannot transition from: 57 // - being unqualifiedly exported, to exported qualifiedly to a specific module. 58 // This transition attempt is silently ignored in set_exported. 59 // 60 // The Mutex Module_lock is shared between ModuleEntry and PackageEntry, to lock either 61 // data structure. 62 class PackageEntry : public HashtableEntry<Symbol*, mtModule> { 63 private: 64 ModuleEntry* _module; 65 // Used to indicate for packages with classes loaded by the boot loader that 66 // a class in that package has been loaded. And, for packages with classes 67 // loaded by the boot loader from -Xbootclasspath/a in an unnamed module, it 68 // indicates from which class path entry. 69 s2 _classpath_index; 70 bool _is_exported_unqualified; 71 bool _is_exported_allUnnamed; 72 bool _must_walk_exports; 73 GrowableArray<ModuleEntry*>* _qualified_exports; 74 TRACE_DEFINE_TRACE_ID_FIELD; 75 76 // Initial size of a package entry's list of qualified exports. 77 enum {QUAL_EXP_SIZE = 43}; 78 79 public: 80 void init() { 81 _module = NULL; 82 _classpath_index = -1; 83 _is_exported_unqualified = false; 84 _is_exported_allUnnamed = false; 85 _must_walk_exports = false; 86 _qualified_exports = NULL; 87 } 88 89 // package name 90 Symbol* name() const { return literal(); } 91 void set_name(Symbol* n) { set_literal(n); } 92 93 // the module containing the package definition 94 ModuleEntry* module() const { return _module; } 95 void set_module(ModuleEntry* m) { _module = m; } 96 97 // package's export state 98 bool is_exported() const { // qualifiedly or unqualifiedly exported 99 return (is_unqual_exported() || has_qual_exports_list() || is_exported_allUnnamed()); 100 } 101 // Returns true if the package has any explicit qualified exports or is exported to all unnamed 102 bool is_qual_exported() const { 103 return (has_qual_exports_list() || is_exported_allUnnamed()); 104 } 105 // Returns true if there are any explicit qualified exports 106 bool has_qual_exports_list() const { 107 return (!is_unqual_exported() && _qualified_exports != NULL); 108 } 109 bool is_exported_allUnnamed() const { 110 return (!is_unqual_exported() && _is_exported_allUnnamed); 111 } 112 bool is_unqual_exported() const { 113 return _is_exported_unqualified; 114 } 115 void set_unqual_exported() { 116 assert(Module_lock->owned_by_self(), "should have the Module_lock"); 117 _is_exported_unqualified = true; 118 _is_exported_allUnnamed = false; 119 } 120 bool exported_pending_delete() const { return (is_unqual_exported() && _qualified_exports != NULL); } 121 122 void set_exported(ModuleEntry* m); 123 124 void set_is_exported_allUnnamed(); 125 126 void set_classpath_index(s2 classpath_index) { 127 _classpath_index = classpath_index; 128 } 129 s2 classpath_index() const { return _classpath_index; } 130 131 bool has_loaded_class() const { return _classpath_index != -1; } 132 133 // returns true if the package is defined in the unnamed module 134 bool in_unnamed_module() const { return !_module->is_named(); } 135 136 // returns true if the package specifies m as a qualified export, including through an unnamed export 137 bool is_qexported_to(ModuleEntry* m) const; 138 139 // add the module to the package's qualified exports 140 void add_qexport(ModuleEntry* m); 141 void set_export_walk_required(ClassLoaderData* m_loader_data); 142 143 PackageEntry* next() const { 144 return (PackageEntry*)HashtableEntry<Symbol*, mtModule>::next(); 145 } 146 147 PackageEntry** next_addr() { 148 return (PackageEntry**)HashtableEntry<Symbol*, mtModule>::next_addr(); 149 } 150 151 // iteration of qualified exports 152 void package_exports_do(ModuleClosure* const f); 153 154 TRACE_DEFINE_TRACE_ID_METHODS; 155 156 // Purge dead weak references out of exported list when any given class loader is unloaded. 157 void purge_qualified_exports(); 158 void delete_qualified_exports(); 159 160 void print(outputStream* st = tty); 161 void verify(); 162 }; 163 164 // The PackageEntryTable is a Hashtable containing a list of all packages defined 165 // by a particular class loader. Each package is represented as a PackageEntry node. 166 // The PackageEntryTable's lookup is lock free. 167 // 168 class PackageEntryTable : public Hashtable<Symbol*, mtModule> { 169 friend class VMStructs; 170 public: 171 enum Constants { 172 _packagetable_entry_size = 1009 // number of entries in package entry table 173 }; 174 175 private: 176 PackageEntry* new_entry(unsigned int hash, Symbol* name, ModuleEntry* module); 177 void add_entry(int index, PackageEntry* new_entry); 178 179 int entry_size() const { return BasicHashtable<mtModule>::entry_size(); } 180 181 PackageEntry** bucket_addr(int i) { 182 return (PackageEntry**)Hashtable<Symbol*, mtModule>::bucket_addr(i); 183 } 184 185 static unsigned int compute_hash(Symbol* name) { return (unsigned int)(name->identity_hash()); } 186 int index_for(Symbol* name) const { return hash_to_index(compute_hash(name)); } 187 188 public: 189 PackageEntryTable(int table_size); 190 ~PackageEntryTable(); 191 192 PackageEntry* bucket(int i) { 193 return (PackageEntry*)Hashtable<Symbol*, mtModule>::bucket(i); 194 } 195 196 // Create package in loader's package entry table and return the entry. 197 // If entry already exists, return null. Assume Module lock was taken by caller. 198 PackageEntry* locked_create_entry_or_null(Symbol* name, ModuleEntry* module); 199 200 // lookup Package with loader's package entry table, if not found add 201 PackageEntry* lookup(Symbol* name, ModuleEntry* module); 202 203 // Only lookup Package within loader's package entry table. The table read is lock-free. 204 PackageEntry* lookup_only(Symbol* Package); 205 206 void verify_javabase_packages(GrowableArray<Symbol*> *pkg_list); 207 208 // purge dead weak references out of exported list 209 void purge_all_package_exports(); 210 211 void print(outputStream* st = tty); 212 void verify(); 213 }; 214 215 #endif // SHARE_VM_CLASSFILE_PACKAGEENTRY_HPP