< prev index next >

src/java.base/share/classes/jdk/internal/module/Modules.java

Print this page




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.module;
  27 
  28 import java.lang.module.ModuleDescriptor;
  29 import java.lang.reflect.Layer;
  30 import java.lang.reflect.Module;
  31 import java.net.URI;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;
  34 import java.util.Set;
  35 
  36 import jdk.internal.loader.BootLoader;
  37 import jdk.internal.loader.ClassLoaders;
  38 import jdk.internal.misc.JavaLangReflectModuleAccess;
  39 import jdk.internal.misc.SharedSecrets;
  40 
  41 /**
  42  * A helper class to allow JDK classes create dynamic modules and to update
  43  * modules, exports and the readability graph. It is also invoked by the VM
  44  * to add read edges when agents are instrumenting code that need to link
  45  * to supporting classes.
  46  *
  47  * The parameters that are package names in this API are the fully-qualified
  48  * names of the packages as defined in section 6.5.3 of <cite>The Java&trade;
  49  * Language Specification </cite>, for example, {@code "java.lang"}.
  50  */
  51 
  52 public class Modules {
  53     private Modules() { }
  54 
  55     private static final JavaLangReflectModuleAccess JLRMA
  56         = SharedSecrets.getJavaLangReflectModuleAccess();
  57 
  58     /**
  59      * Creates a new Module. The module has the given ModuleDescriptor and
  60      * is defined to the given class loader.
  61      *
  62      * The resulting Module is in a larval state in that it does not not read
  63      * any other module and does not have any exports.
  64      *
  65      * The URI is for information purposes only.
  66      */
  67     public static Module defineModule(ClassLoader loader,
  68                                       ModuleDescriptor descriptor,
  69                                       URI uri)
  70     {
  71         return JLRMA.defineModule(loader, descriptor, uri);
  72     }
  73 
  74     /**
  75      * Define a new module to the VM. The module has the given set of
  76      * packages and is defined to the given class loader.
  77      *
  78      * The resulting Module is in a larval state in that it does not not read
  79      * any other module and does not have any exports.
  80      */
  81     public static Module defineModule(ClassLoader loader,
  82                                       String name,
  83                                       Set<String> packages)
  84     {
  85         ModuleDescriptor descriptor = ModuleDescriptor.newModule(name)
  86                 .packages(packages)
  87                 .build();
  88 
  89         return JLRMA.defineModule(loader, descriptor, null);
  90     }
  91 
  92     /**
  93      * Adds a read-edge so that module {@code m1} reads module {@code m1}.
  94      * Same as m1.addReads(m2) but without a caller check.
  95      */
  96     public static void addReads(Module m1, Module m2) {
  97         JLRMA.addReads(m1, m2);
  98     }
  99 
 100     /**
 101      * Update module {@code m} to read all unnamed modules.
 102      */
 103     public static void addReadsAllUnnamed(Module m) {
 104         JLRMA.addReadsAllUnnamed(m);
 105     }
 106 
 107     /**
 108      * Updates module m1 to export a package to module m2.
 109      * Same as m1.addExports(pn, m2) but without a caller check.

 110      */
 111     public static void addExports(Module m1, String pn, Module m2) {
 112         JLRMA.addExports(m1, pn, m2);
 113     }
 114 
 115     /**
 116      * Updates module m1 to open a package to module m2.
 117      * Same as m1.addOpens(pn, m2) but without a caller check.
 118      */
 119     public static void addOpens(Module m1, String pn, Module m2) {
 120         JLRMA.addOpens(m1, pn, m2);
 121     }
 122 
 123     /**
 124      * Updates a module m to export a package to all modules.
 125      */
 126     public static void addExportsToAll(Module m, String pn) {
 127         JLRMA.addExportsToAll(m, pn);
 128     }
 129 
 130     /**
 131      * Updates a module m to open a package to all modules.


 132      */
 133     public static void addOpensToAll(Module m, String pn) {
 134         JLRMA.addOpensToAll(m, pn);
 135     }
 136 
 137     /**
 138      * Updates module m to export a package to all unnamed modules.

 139      */
 140     public static void addExportsToAllUnnamed(Module m, String pn) {
 141         JLRMA.addExportsToAllUnnamed(m, pn);
 142     }
 143 
 144     /**
 145      * Updates module m to open a package to all unnamed modules.
 146      */
 147     public static void addOpensToAllUnnamed(Module m, String pn) {
 148         JLRMA.addOpensToAllUnnamed(m, pn);
 149     }
 150 
 151     /**
 152      * Updates module m to use a service

 153      */
 154     public static void addUses(Module m, Class<?> service) {
 155         JLRMA.addUses(m, service);
 156     }
 157 
 158     /**
 159      * Updates module m to provide a service
 160      */
 161     public static void addProvides(Module m, Class<?> service, Class<?> impl) {
 162         Layer layer = m.getLayer();
 163 
 164         if (layer == null || layer == Layer.boot()) {
 165             // update ClassLoader catalog
 166             PrivilegedAction<ClassLoader> pa = m::getClassLoader;
 167             ClassLoader loader = AccessController.doPrivileged(pa);
 168             ServicesCatalog catalog;
 169             if (loader == null) {
 170                 catalog = BootLoader.getServicesCatalog();
 171             } else {
 172                 catalog = ServicesCatalog.getServicesCatalog(loader);
 173             }
 174             catalog.addProvider(m, service, impl);
 175         }
 176 
 177         if (layer != null) {
 178             // update Layer catalog
 179             SharedSecrets.getJavaLangReflectModuleAccess()
 180                     .getServicesCatalog(layer)
 181                     .addProvider(m, service, impl);
 182         }
 183     }
 184 
 185     /**
 186      * Adds a package to a module's content.
 187      *
 188      * This method is a no-op if the module already contains the package or the
 189      * module is an unnamed module.
 190      */
 191     public static void addPackage(Module m, String pn) {
 192         JLRMA.addPackage(m, pn);
 193     }
 194 
 195     /**
 196      * Called by the VM when code in the given Module has been transformed by
 197      * an agent and so may have been instrumented to call into supporting
 198      * classes on the boot class path or application class path.
 199      */
 200     public static void transformedByAgent(Module m) {
 201         addReads(m, BootLoader.getUnnamedModule());
 202         addReads(m, ClassLoaders.appClassLoader().getUnnamedModule());
 203     }
 204 }


  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.module;
  27 
  28 import java.lang.module.ModuleDescriptor;
  29 import java.lang.reflect.Layer;
  30 import java.lang.reflect.Module;
  31 import java.net.URI;
  32 import java.security.AccessController;
  33 import java.security.PrivilegedAction;

  34 
  35 import jdk.internal.loader.BootLoader;
  36 import jdk.internal.loader.ClassLoaders;
  37 import jdk.internal.misc.JavaLangReflectModuleAccess;
  38 import jdk.internal.misc.SharedSecrets;
  39 
  40 /**
  41  * A helper class for creating and updating modules. This class is intended to
  42  * support command-line options, tests, and the instrumentation API. It is also
  43  * used by the VM to add read edges when agents are instrumenting code that
  44  * need to link to supporting classes.
  45  *
  46  * The parameters that are package names in this API are the fully-qualified
  47  * names of the packages as defined in section 6.5.3 of <cite>The Java&trade;
  48  * Language Specification </cite>, for example, {@code "java.lang"}.
  49  */
  50 
  51 public class Modules {
  52     private Modules() { }
  53 
  54     private static final JavaLangReflectModuleAccess JLRMA
  55         = SharedSecrets.getJavaLangReflectModuleAccess();
  56 
  57     /**
  58      * Creates a new Module. The module has the given ModuleDescriptor and
  59      * is defined to the given class loader.
  60      *
  61      * The resulting Module is in a larval state in that it does not not read
  62      * any other module and does not have any exports.
  63      *
  64      * The URI is for information purposes only.
  65      */
  66     public static Module defineModule(ClassLoader loader,
  67                                       ModuleDescriptor descriptor,
  68                                       URI uri)
  69     {
  70         return JLRMA.defineModule(loader, descriptor, uri);
  71     }
  72 
  73     /**
  74      * Updates m1 to read m2.


















  75      * Same as m1.addReads(m2) but without a caller check.
  76      */
  77     public static void addReads(Module m1, Module m2) {
  78         JLRMA.addReads(m1, m2);
  79     }
  80 
  81     /**
  82      * Update module m to read all unnamed modules.
  83      */
  84     public static void addReadsAllUnnamed(Module m) {
  85         JLRMA.addReadsAllUnnamed(m);
  86     }
  87 
  88     /**
  89      * Update module m to export a package to all modules.
  90      *
  91      * This method is for intended for use by tests only.
  92      */
  93     public static void addExports(Module m, String pn) {
  94         JLRMA.addExports(m, pn);
  95     }
  96 
  97     /**
  98      * Updates module m1 to export a package to module m2.
  99      * Same as m1.addExports(pn, m2) but without a caller check
 100      */
 101     public static void addExports(Module m1, String pn, Module m2) {
 102         JLRMA.addExports(m1, pn, m2);
 103     }
 104 
 105     /**
 106      * Updates module m to export a package to all unnamed modules.
 107      */
 108     public static void addExportsToAllUnnamed(Module m, String pn) {
 109         JLRMA.addExportsToAllUnnamed(m, pn);
 110     }
 111 
 112     /**
 113      * Update module m to open a package to all modules.
 114      *
 115      * This method is for intended for use by tests only.
 116      */
 117     public static void addOpens(Module m, String pn) {
 118         JLRMA.addOpens(m, pn);
 119     }
 120 
 121     /**
 122      * Updates module m1 to open a package to module m2.
 123      * Same as m1.addOpens(pn, m2) but without a caller check.
 124      */
 125     public static void addOpens(Module m1, String pn, Module m2) {
 126         JLRMA.addOpens(m1, pn, m2);
 127     }
 128 
 129     /**
 130      * Updates module m to open a package to all unnamed modules.
 131      */
 132     public static void addOpensToAllUnnamed(Module m, String pn) {
 133         JLRMA.addOpensToAllUnnamed(m, pn);
 134     }
 135 
 136     /**
 137      * Updates module m to use a service.
 138      * Same as m2.addUses(service) but without a caller check.
 139      */
 140     public static void addUses(Module m, Class<?> service) {
 141         JLRMA.addUses(m, service);
 142     }
 143 
 144     /**
 145      * Updates module m to provide a service
 146      */
 147     public static void addProvides(Module m, Class<?> service, Class<?> impl) {
 148         Layer layer = m.getLayer();
 149 
 150         if (layer == null || layer == Layer.boot()) {
 151             // update ClassLoader catalog
 152             PrivilegedAction<ClassLoader> pa = m::getClassLoader;
 153             ClassLoader loader = AccessController.doPrivileged(pa);
 154             ServicesCatalog catalog;
 155             if (loader == null) {
 156                 catalog = BootLoader.getServicesCatalog();
 157             } else {
 158                 catalog = ServicesCatalog.getServicesCatalog(loader);
 159             }
 160             catalog.addProvider(m, service, impl);
 161         }
 162 
 163         if (layer != null) {
 164             // update Layer catalog
 165             SharedSecrets.getJavaLangReflectModuleAccess()
 166                     .getServicesCatalog(layer)
 167                     .addProvider(m, service, impl);
 168         }
 169     }
 170 
 171     /**










 172      * Called by the VM when code in the given Module has been transformed by
 173      * an agent and so may have been instrumented to call into supporting
 174      * classes on the boot class path or application class path.
 175      */
 176     public static void transformedByAgent(Module m) {
 177         addReads(m, BootLoader.getUnnamedModule());
 178         addReads(m, ClassLoaders.appClassLoader().getUnnamedModule());
 179     }
 180 }
< prev index next >