jdk/src/java.base/share/classes/jdk/internal/module/Modules.java
changeset 42338 a60f280f803c
parent 37779 7c84df693837
child 43712 5dfd0950317c
equal deleted inserted replaced
42148:7a4a59859ac0 42338:a60f280f803c
    24  */
    24  */
    25 
    25 
    26 package jdk.internal.module;
    26 package jdk.internal.module;
    27 
    27 
    28 import java.lang.module.ModuleDescriptor;
    28 import java.lang.module.ModuleDescriptor;
       
    29 import java.lang.reflect.Layer;
    29 import java.lang.reflect.Module;
    30 import java.lang.reflect.Module;
    30 import java.net.URI;
    31 import java.net.URI;
       
    32 import java.security.AccessController;
       
    33 import java.security.PrivilegedAction;
    31 import java.util.Set;
    34 import java.util.Set;
    32 
    35 
    33 import jdk.internal.loader.BootLoader;
    36 import jdk.internal.loader.BootLoader;
    34 import jdk.internal.loader.ClassLoaders;
    37 import jdk.internal.loader.ClassLoaders;
    35 import jdk.internal.misc.JavaLangReflectModuleAccess;
    38 import jdk.internal.misc.JavaLangReflectModuleAccess;
    36 import jdk.internal.misc.SharedSecrets;
    39 import jdk.internal.misc.SharedSecrets;
    37 
       
    38 
    40 
    39 /**
    41 /**
    40  * A helper class to allow JDK classes create dynamic modules and to update
    42  * A helper class to allow JDK classes create dynamic modules and to update
    41  * modules, exports and the readability graph. It is also invoked by the VM
    43  * modules, exports and the readability graph. It is also invoked by the VM
    42  * to add read edges when agents are instrumenting code that need to link
    44  * to add read edges when agents are instrumenting code that need to link
    51     private Modules() { }
    53     private Modules() { }
    52 
    54 
    53     private static final JavaLangReflectModuleAccess JLRMA
    55     private static final JavaLangReflectModuleAccess JLRMA
    54         = SharedSecrets.getJavaLangReflectModuleAccess();
    56         = SharedSecrets.getJavaLangReflectModuleAccess();
    55 
    57 
    56 
       
    57     /**
    58     /**
    58      * Creates a new Module. The module has the given ModuleDescriptor and
    59      * Creates a new Module. The module has the given ModuleDescriptor and
    59      * is defined to the given class loader.
    60      * is defined to the given class loader.
    60      *
    61      *
    61      * The resulting Module is in a larval state in that it does not not read
    62      * The resulting Module is in a larval state in that it does not not read
    70         return JLRMA.defineModule(loader, descriptor, uri);
    71         return JLRMA.defineModule(loader, descriptor, uri);
    71     }
    72     }
    72 
    73 
    73     /**
    74     /**
    74      * Define a new module to the VM. The module has the given set of
    75      * Define a new module to the VM. The module has the given set of
    75      * concealed packages and is defined to the given class loader.
    76      * packages and is defined to the given class loader.
    76      *
    77      *
    77      * The resulting Module is in a larval state in that it does not not read
    78      * The resulting Module is in a larval state in that it does not not read
    78      * any other module and does not have any exports.
    79      * any other module and does not have any exports.
    79      */
    80      */
    80     public static Module defineModule(ClassLoader loader,
    81     public static Module defineModule(ClassLoader loader,
    81                                       String name,
    82                                       String name,
    82                                       Set<String> packages)
    83                                       Set<String> packages)
    83     {
    84     {
    84         ModuleDescriptor descriptor
    85         ModuleDescriptor descriptor = ModuleDescriptor.module(name)
    85             = new ModuleDescriptor.Builder(name).conceals(packages).build();
    86                 .contains(packages)
       
    87                 .build();
    86 
    88 
    87         return JLRMA.defineModule(loader, descriptor, null);
    89         return JLRMA.defineModule(loader, descriptor, null);
    88     }
    90     }
    89 
    91 
    90     /**
    92     /**
   102         JLRMA.addReadsAllUnnamed(m);
   104         JLRMA.addReadsAllUnnamed(m);
   103     }
   105     }
   104 
   106 
   105     /**
   107     /**
   106      * Updates module m1 to export a package to module m2.
   108      * Updates module m1 to export a package to module m2.
   107      * Same as m1.addExports(pkg, m2) but without a caller check.
   109      * Same as m1.addExports(pn, m2) but without a caller check.
   108      */
   110      */
   109     public static void addExports(Module m1, String pn, Module m2) {
   111     public static void addExports(Module m1, String pn, Module m2) {
   110         JLRMA.addExports(m1, pn, m2);
   112         JLRMA.addExports(m1, pn, m2);
   111     }
   113     }
   112 
   114 
   113     /**
   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     /**
   114      * Updates a module m to export a package to all modules.
   124      * Updates a module m to export a package to all modules.
   115      */
   125      */
   116     public static void addExportsToAll(Module m, String pn) {
   126     public static void addExportsToAll(Module m, String pn) {
   117         JLRMA.addExportsToAll(m, pn);
   127         JLRMA.addExportsToAll(m, pn);
   118     }
   128     }
   119 
   129 
   120     /**
   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     /**
   121      * Updates module m to export a package to all unnamed modules.
   138      * Updates module m to export a package to all unnamed modules.
   122      */
   139      */
   123     public static void addExportsToAllUnnamed(Module m, String pn) {
   140     public static void addExportsToAllUnnamed(Module m, String pn) {
   124         JLRMA.addExportsToAllUnnamed(m, 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         }
   125     }
   183     }
   126 
   184 
   127     /**
   185     /**
   128      * Adds a package to a module's content.
   186      * Adds a package to a module's content.
   129      *
   187      *
   140      */
   198      */
   141     public static void transformedByAgent(Module m) {
   199     public static void transformedByAgent(Module m) {
   142         addReads(m, BootLoader.getUnnamedModule());
   200         addReads(m, BootLoader.getUnnamedModule());
   143         addReads(m, ClassLoaders.appClassLoader().getUnnamedModule());
   201         addReads(m, ClassLoaders.appClassLoader().getUnnamedModule());
   144     }
   202     }
   145 
       
   146 }
   203 }