1 /*
   2  * Copyright (c) 2013, 2017, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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 /**
  27  * Classes to support module descriptors and creating configurations of modules
  28  * by means of resolution and service binding.
  29  *
  30  * <p> Unless otherwise noted, passing a {@code null} argument to a constructor
  31  * or method of any class or interface in this package will cause a {@link
  32  * java.lang.NullPointerException NullPointerException} to be thrown. Additionally,
  33  * invoking a method with an array or collection containing a {@code null} element
  34  * will cause a {@code NullPointerException}, unless otherwise specified. </p>
  35  *
  36  *
  37  * <h1><a id="resolution">{@index "Module Resolution"}</a></h1>
  38  *
  39  * <p> Resolution is the process of computing how modules depend on each other.
  40  * The process occurs at compile time and run time. </p>
  41  *
  42  * <p> Resolution is a two-step process. The first step recursively enumerates
  43  * the 'requires' directives of a set of root modules. If all the enumerated
  44  * modules are observable, then the second step computes their readability graph.
  45  * The readability graph embodies how modules depend on each other, which in
  46  * turn controls access across module boundaries. </p>
  47  *
  48  * <h2> Step 1: Recursive enumeration </h2>
  49  *
  50  * <p> Recursive enumeration takes a set of module names, looks up each of their
  51  * module declarations, and for each module declaration, recursively enumerates:
  52  *
  53  * <ul>
  54  *   <li> <p> the module names given by the 'requires' directives with the
  55  *   'transitive' modifier, and </p></li>
  56  *   <li> <p> at the discretion of the host system, the module names given by
  57  *   the 'requires' directives without the 'transitive' modifier. </p></li>
  58  * </ul>
  59  *
  60  * <p> Module declarations are looked up in a set of observable modules. The set
  61  * of observable modules is determined in an implementation specific manner. The
  62  * set of observable modules may include modules with explicit declarations
  63  * (that is, with a {@code module-info.java} source file or {@code module-info.class}
  64  * file) and modules with implicit declarations (that is,
  65  * <a href="ModuleFinder.html#automatic-modules">automatic modules</a>).
  66  * Because an automatic module has no explicit module declaration, it has no
  67  * 'requires' directives of its own, although its name may be given by a
  68  * 'requires' directive of an explicit module declaration. </p>
  69 
  70  * <p> The set of root modules, whose names are the initial input to this
  71  * algorithm, is determined in an implementation specific manner. The set of
  72  * root modules may include automatic modules. </p>
  73  *
  74  * <p> If at least one automatic module is enumerated by this algorithm, then
  75  * every observable automatic module must be enumerated, regardless of whether
  76  * any of their names are given by 'requires' directives of explicit module
  77  * declarations. </p>
  78  *
  79  * <p> If any of the following conditions occur, then resolution fails:
  80  * <ul>
  81  *   <li><p> Any root module is not observable. </p></li>
  82  *   <li><p> Any module whose name is given by a 'requires' directive with the
  83  *   'transitive' modifier is not observable. </p></li>
  84  *   <li><p> At the discretion of the host system, any module whose name is given
  85  *   by a 'requires' directive without the 'transitive' modifier is not
  86  *   observable. </p></li>
  87  *   <li><p> The algorithm in this step enumerates the same module name twice. This
  88  *   indicates a cycle in the 'requires' directives, disregarding any 'transitive'
  89  *   modifiers. </p></li>
  90  * </ul>
  91  *
  92  * <p> Otherwise, resolution proceeds to step 2. </p>
  93  *
  94  * <h2> Step 2: Computing the readability graph </h2>
  95  *
  96  * <p> A 'requires' directive (irrespective of 'transitive') expresses that
  97  * one module depends on some other module. The effect of the 'transitive'
  98  * modifier is to cause additional modules to also depend on the other module.
  99  * If module M 'requires transitive N', then not only does M depend on N, but
 100  * any module that depends on M also depends on N. This allows M to be
 101  * refactored so that some or all of its content can be moved to a new module N
 102  * without breaking modules that have a 'requires M' directive. </p>
 103  *
 104  * <p> Module dependencies are represented by the readability graph. The
 105  * readability graph is a directed graph whose vertices are the modules
 106  * enumerated in step 1 and whose edges represent readability between pairs of
 107  * modules. The edges are specified as follows:
 108  *
 109  * <p> First, readability is determined by the 'requires' directives of the
 110  * enumerated modules, disregarding any 'transitive' modifiers:
 111  *
 112  * <ul>
 113  *   <li><p> For each enumerated module A that 'requires' B: A "reads" B. </p></li>
 114  *   <li><p> For each enumerated module X that is automatic: X "reads" every
 115  *   other enumerated module (it is "as if" an automatic module has 'requires'
 116  *   directives for every other enumerated module). </p></li>
 117  * </ul>
 118  *
 119  * <p> Second, readability is augmented to account for 'transitive' modifiers:
 120  * <ul>
 121  *   <li> <p> For each enumerated module A that "reads" B: </p>
 122  *     <ul>
 123  *     <li><p> If B 'requires transitive' C, then A "reads" C as well as B. This
 124  *     augmentation is recursive: since A "reads" C, if C 'requires transitive'
 125  *     D, then A "reads" D as well as C and B. </p></li>
 126  *     <li><p> If B is an automatic module, then A "reads" every other enumerated
 127  *     automatic module. (It is "as if" an automatic module has 'requires transitive'
 128  *     directives for every other enumerated automatic module).</p> </li>
 129  *     </ul>
 130  *   </li>
 131  * </ul>
 132  *
 133  * <p> Finally, every module "reads" itself. </p>
 134  *
 135  * <p> If any of the following conditions occur in the readability graph, then
 136  * resolution fails:
 137  * <ul>
 138  *   <li><p> A module "reads" two or more modules with the same name. This includes
 139  *   the case where a module "reads" another with the same name as itself. </p></li>
 140  *   <li><p> Two or more modules export a package with the same name to a module
 141  *   that "reads" both. This includes the case where a module M containing package
 142  *   p "reads" another module that exports p to M. </p></li>
 143  *   <li><p> A module M declares that it 'uses p.S' or 'provides p.S with ...' but
 144  *   package p is neither in module M nor exported to M by any module that M
 145  *   "reads". </p></li>
 146  * </ul>
 147  * <p> Otherwise, resolution succeeds, and the result of resolution is the
 148  * readability graph.
 149  *
 150  * <h2> Root modules </h2>
 151  *
 152  * <p> The set of root modules at compile-time is usually the set of modules
 153  * being compiled. At run-time, the set of root modules is usually the
 154  * application module specified to the 'java' launcher. When compiling code in
 155  * the unnamed module, or at run-time when the main application class is loaded
 156  * from the class path, then the default set of root modules is implementation
 157  * specific (In the JDK implementation it is the module "java.se", if observable,
 158  * and every observable module that exports an API). </p>
 159  *
 160  * <h2> Observable modules </h2>
 161  *
 162  * <p> The set of observable modules at both compile-time and run-time is
 163  * determined by searching several different paths, and also by searching
 164  * the compiled modules built in to the environment. The search order is as
 165  * follows: </p>
 166  *
 167  * <ol>
 168  *   <li><p> At compile time only, the compilation module path. This path
 169  *   contains module definitions in source form.  </p></li>
 170  *
 171  *   <li><p> The upgrade module path. This path contains compiled definitions of
 172  *   modules that will be observed in preference to the compiled definitions of
 173  *   any <i>upgradeable modules</i> that are present in (3) and (4). See the Java
 174  *   SE Platform for the designation of which standard modules are upgradeable.
 175  *   </p></li>
 176  *
 177  *   <li><p> The system modules, which are the compiled definitions built in to
 178  *   the environment. </p></li>
 179  *
 180  *   <li><p> The application module path. This path contains compiled definitions
 181  *   of library and application modules. </p></li>
 182  *
 183  * </ol>
 184  *
 185  * <h2> 'requires' directives with 'static' modifier </h2>
 186  *
 187  * <p> 'requires' directives that have the 'static' modifier express an optional
 188  * dependence at run time. If a module declares that it 'requires static M' then
 189  * resolution does not search the observable modules for M to satisfy the dependency.
 190  * However, if M is recursively enumerated at step 1 then all modules that are
 191  * enumerated and `requires static M` will read M. </p>
 192  *
 193  * <h2> Completeness </h2>
 194  *
 195  * <p> Resolution may be partial at compile-time in that the complete transitive
 196  * closure may not be required to compile a set of modules. Minimally, the
 197  * readability graph that is constructed and validated at compile-time includes
 198  * the modules being compiled, their direct dependences, and all implicitly
 199  * declared dependences (requires transitive). </p>
 200  *
 201  * <p> At run-time, resolution is an additive process. The recursive enumeration
 202  * at step 1 may be relative to previous resolutions so that a root module,
 203  * or a module named in a 'requires' directive, is not enumerated when it was
 204  * enumerated by a previous (or parent) resolution. The readability graph that
 205  * is the result of resolution may therefore have a vertex for a module enumerated
 206  * in step 1 but with an edge to represent that the module reads a module that
 207  * was enumerated by previous (or parent) resolution. </p>
 208  *
 209  * @since 9
 210  * @spec JPMS
 211  */
 212 
 213 package java.lang.module;