1 /*
  2 * Copyright (c) 2016, 2026, 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_CLASSFILE_MODULES_HPP
 26 #define SHARE_CLASSFILE_MODULES_HPP
 27 
 28 #include "memory/allStatic.hpp"
 29 #include "runtime/handles.hpp"
 30 
 31 class ModuleEntryTable;
 32 class SerializeClosure;
 33 class Symbol;
 34 
 35 class Modules : AllStatic {
 36   static void check_cds_restrictions(Handle module1, Handle module2, TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
 37 
 38 public:
 39   // define_module defines a module containing the specified packages. It binds the
 40   // module to its class loader by creating the ModuleEntry record in the
 41   // ClassLoader's ModuleEntry table, and creates PackageEntry records in the class
 42   // loader's PackageEntry table.  The jstring for all package names will convert "."
 43   // to "/"
 44   //
 45   //  IllegalArgumentExceptions are thrown for the following :
 46   // * Module's Class loader is not a subclass of java.lang.ClassLoader
 47   // * Module's Class loader already has a module with that name
 48   // * Module's Class loader has already defined types for any of the module's packages
 49   // * Module_name is syntactically bad
 50   // * Packages contains an illegal package name or a non-String object
 51   // * A package already exists in another module for this class loader
 52   // * Module is an unnamed module
 53   //  NullPointerExceptions are thrown if module is null.
 54   static void define_module(Handle module, jboolean is_open, jstring version,
 55                             jstring location, jobjectArray packages, TRAPS);
 56 
 57   static void check_archived_module_oop(oop orig_module_obj) NOT_CDS_JAVA_HEAP_RETURN;
 58   static void define_archived_modules(Handle h_platform_loader, Handle h_system_loader,
 59                                       TRAPS) NOT_CDS_JAVA_HEAP_RETURN;
 60   static void init_archived_modules(JavaThread* current, Handle h_platform_loader, Handle h_system_loader)
 61                                    NOT_CDS_JAVA_HEAP_RETURN;
 62   static void dump_archived_module_info() NOT_CDS_JAVA_HEAP_RETURN;
 63   static void serialize_archived_module_info(SerializeClosure* soc) NOT_CDS_JAVA_HEAP_RETURN;
 64 
 65   static bool is_dynamic_proxy_module(Handle module);
 66   static bool is_dynamic_proxy_module(ModuleEntry* module_entry);
 67 
 68   static void check_archived_flag_consistency(char* archived_flag, const char* runtime_flag, const char* property) NOT_CDS_JAVA_HEAP_RETURN;
 69 
 70 #if INCLUDE_CDS_JAVA_HEAP
 71 private:
 72   class ArchivedProperty;
 73 
 74   static ArchivedProperty _archived_props[];
 75   static constexpr size_t num_archived_props();
 76   static ArchivedProperty& archived_prop(size_t i);
 77 public:
 78 #endif
 79 
 80   // Provides the java.lang.Module for the unnamed module defined
 81   // to the boot loader.
 82   //
 83   //  IllegalArgumentExceptions are thrown for the following :
 84   //  * Module has a name
 85   //  * Module is not a subclass of java.lang.Module
 86   //  * Module's class loader is not the boot loader
 87   //  NullPointerExceptions are thrown if module is null.
 88   static void set_bootloader_unnamed_module(Handle module, TRAPS);
 89 
 90   // This either does a qualified export of package in module from_module to module
 91   // to_module or, if to_module is null, does an unqualified export of package.
 92   // Any "." in the package name will be converted to "/"
 93   //
 94   // Error conditions causing IlegalArgumentException to be throw :
 95   // * Module from_module does not exist
 96   // * Module to_module is not null and does not exist
 97   // * Package is not syntactically correct
 98   // * Package is not defined for from_module's class loader
 99   // * Package is not in module from_module.
100   static void add_module_exports(Handle from_module, jstring package, Handle to_module, TRAPS);
101 
102   // This does a qualified export of package in module from_module to module
103   // to_module.  Any "." in the package name will be converted to "/"
104   //
105   // Error conditions causing IlegalArgumentException to be throw :
106   // * Module from_module does not exist
107   // * Module to_module does not exist
108   // * Package is not syntactically correct
109   // * Package is not defined for from_module's class loader
110   // * Package is not in module from_module.
111   static void add_module_exports_qualified(Handle from_module, jstring package, Handle to_module, TRAPS);
112 
113   // add_reads_module adds module to_module to the list of modules that from_module
114   // can read.  If from_module is the same as to_module then this is a no-op.
115   // If to_module is null then from_module is marked as a loose module (meaning that
116   // from_module can read all current and future unnamed  modules).
117   // An IllegalArgumentException is thrown if from_module is null or either (non-null)
118   // module does not exist.
119   static void add_reads_module(Handle from_module, Handle to_module, TRAPS);
120 
121   // Return the java.lang.Module object for this class object.
122   static jobject get_module(jclass clazz, TRAPS);
123 
124   // Return the java.lang.Module object for this class loader and package.
125   // Returns null if the package name is empty, if the resulting package
126   // entry is null, if the module is not found or is unnamed.
127   // The package should contain /'s, not .'s, as in java/lang, not java.lang.
128   static oop get_named_module(Handle h_loader, const char* package);
129 
130   // Marks the specified package as exported to all unnamed modules.
131   // If either module or package is null then NullPointerException is thrown.
132   // If module or package is bad, or module is unnamed, or package is not in
133   // module then IllegalArgumentException is thrown.
134   static void add_module_exports_to_all_unnamed(Handle module, jstring package, TRAPS);
135 
136   // Return TRUE iff package is defined by loader
137   static bool is_package_defined(Symbol* package_name, Handle h_loader);
138   static ModuleEntryTable* get_module_entry_table(Handle h_loader);
139 };
140 
141 #endif // SHARE_CLASSFILE_MODULES_HPP