< prev index next >

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

Print this page
rev 51675 : 8207690: Parsing API for classpath and similar path strings


   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 package jdk.internal.module;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.PrintStream;
  31 import java.lang.module.FindException;
  32 import java.lang.module.ModuleDescriptor;
  33 import java.lang.module.ModuleFinder;
  34 import java.lang.module.ModuleReference;
  35 import java.net.URI;
  36 import java.nio.file.DirectoryStream;
  37 import java.nio.file.Files;
  38 import java.nio.file.NoSuchFileException;
  39 import java.nio.file.Path;

  40 import java.nio.file.attribute.BasicFileAttributes;
  41 import java.util.Comparator;
  42 import java.util.HashMap;
  43 import java.util.Map;
  44 import java.util.Optional;
  45 import java.util.stream.Stream;
  46 
  47 /**
  48  * A validator to check for errors and conflicts between modules.
  49  */
  50 
  51 class ModulePathValidator {
  52     private static final String MODULE_INFO = "module-info.class";
  53     private static final String INDENT = "    ";
  54 
  55     private final Map<String, ModuleReference> nameToModule;
  56     private final Map<String, ModuleReference> packageToModule;
  57     private final PrintStream out;
  58 
  59     private int errorCount;
  60 
  61     private ModulePathValidator(PrintStream out) {
  62         this.nameToModule = new HashMap<>();
  63         this.packageToModule = new HashMap<>();
  64         this.out = out;
  65     }
  66 
  67     /**
  68      * Scans and the validates all modules on the module path. The module path
  69      * comprises the upgrade module path, system modules, and the application
  70      * module path.
  71      *
  72      * @param out the print stream for output messages
  73      * @return the number of errors found
  74      */
  75     static int scanAllModules(PrintStream out) {
  76         ModulePathValidator validator = new ModulePathValidator(out);
  77 
  78         // upgrade module path
  79         String value = System.getProperty("jdk.module.upgrade.path");
  80         if (value != null) {
  81             Stream.of(value.split(File.pathSeparator))

  82                     .map(Path::of)
  83                     .forEach(validator::scan);
  84         }
  85 
  86         // system modules
  87         ModuleFinder.ofSystem().findAll().stream()
  88                 .sorted(Comparator.comparing(ModuleReference::descriptor))
  89                 .forEach(validator::process);
  90 
  91         // application module path
  92         value = System.getProperty("jdk.module.path");
  93         if (value != null) {
  94             Stream.of(value.split(File.pathSeparator))

  95                     .map(Path::of)
  96                     .forEach(validator::scan);
  97         }
  98 
  99         return validator.errorCount;
 100     }
 101 
 102     /**
 103      * Prints the module location and name.
 104      */
 105     private void printModule(ModuleReference mref) {
 106         mref.location()
 107                 .filter(uri -> !isJrt(uri))
 108                 .ifPresent(uri -> out.print(uri + " "));
 109         ModuleDescriptor descriptor = mref.descriptor();
 110         out.print(descriptor.name());
 111         if (descriptor.isAutomatic())
 112             out.print(" automatic");
 113         out.println();
 114     }




   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 package jdk.internal.module;
  27 

  28 import java.io.IOException;
  29 import java.io.PrintStream;
  30 import java.lang.module.FindException;
  31 import java.lang.module.ModuleDescriptor;
  32 import java.lang.module.ModuleFinder;
  33 import java.lang.module.ModuleReference;
  34 import java.net.URI;
  35 import java.nio.file.DirectoryStream;
  36 import java.nio.file.Files;
  37 import java.nio.file.NoSuchFileException;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import java.nio.file.attribute.BasicFileAttributes;
  41 import java.util.Comparator;
  42 import java.util.HashMap;
  43 import java.util.Map;
  44 import java.util.Optional;

  45 
  46 /**
  47  * A validator to check for errors and conflicts between modules.
  48  */
  49 
  50 class ModulePathValidator {
  51     private static final String MODULE_INFO = "module-info.class";
  52     private static final String INDENT = "    ";
  53 
  54     private final Map<String, ModuleReference> nameToModule;
  55     private final Map<String, ModuleReference> packageToModule;
  56     private final PrintStream out;
  57 
  58     private int errorCount;
  59 
  60     private ModulePathValidator(PrintStream out) {
  61         this.nameToModule = new HashMap<>();
  62         this.packageToModule = new HashMap<>();
  63         this.out = out;
  64     }
  65 
  66     /**
  67      * Scans and the validates all modules on the module path. The module path
  68      * comprises the upgrade module path, system modules, and the application
  69      * module path.
  70      *
  71      * @param out the print stream for output messages
  72      * @return the number of errors found
  73      */
  74     static int scanAllModules(PrintStream out) {
  75         ModulePathValidator validator = new ModulePathValidator(out);
  76 
  77         // upgrade module path
  78         String value = System.getProperty("jdk.module.upgrade.path");
  79         if (value != null) {
  80             Paths.pathToStrings(value)
  81                     .stream()
  82                     .map(Path::of)
  83                     .forEach(validator::scan);
  84         }
  85 
  86         // system modules
  87         ModuleFinder.ofSystem().findAll().stream()
  88                 .sorted(Comparator.comparing(ModuleReference::descriptor))
  89                 .forEach(validator::process);
  90 
  91         // application module path
  92         value = System.getProperty("jdk.module.path");
  93         if (value != null) {
  94             Paths.pathToStrings(value)
  95                     .stream()
  96                     .map(Path::of)
  97                     .forEach(validator::scan);
  98         }
  99 
 100         return validator.errorCount;
 101     }
 102 
 103     /**
 104      * Prints the module location and name.
 105      */
 106     private void printModule(ModuleReference mref) {
 107         mref.location()
 108                 .filter(uri -> !isJrt(uri))
 109                 .ifPresent(uri -> out.print(uri + " "));
 110         ModuleDescriptor descriptor = mref.descriptor();
 111         out.print(descriptor.name());
 112         if (descriptor.isAutomatic())
 113             out.print(" automatic");
 114         out.println();
 115     }


< prev index next >