src/jdk.jdeps/share/classes/com/sun/tools/jdeps/JdepsConfiguration.java
changeset 48253 82767203606e
parent 47239 2088dbaa2282
child 50598 8d9d4d91be7f
equal deleted inserted replaced
48252:77b88d8f8380 48253:82767203606e
    36 import java.io.IOException;
    36 import java.io.IOException;
    37 import java.io.InputStream;
    37 import java.io.InputStream;
    38 import java.io.UncheckedIOException;
    38 import java.io.UncheckedIOException;
    39 import java.lang.module.Configuration;
    39 import java.lang.module.Configuration;
    40 import java.lang.module.ModuleDescriptor;
    40 import java.lang.module.ModuleDescriptor;
    41 import java.lang.module.ModuleDescriptor.Exports;
       
    42 import java.lang.module.ModuleDescriptor.Opens;
       
    43 import java.lang.module.ModuleFinder;
    41 import java.lang.module.ModuleFinder;
    44 import java.lang.module.ModuleReader;
    42 import java.lang.module.ModuleReader;
    45 import java.lang.module.ModuleReference;
    43 import java.lang.module.ModuleReference;
    46 import java.lang.module.ResolvedModule;
    44 import java.lang.module.ResolvedModule;
    47 import java.net.URI;
    45 import java.net.URI;
    69 public class JdepsConfiguration implements AutoCloseable {
    67 public class JdepsConfiguration implements AutoCloseable {
    70     // the token for "all modules on the module path"
    68     // the token for "all modules on the module path"
    71     public static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
    69     public static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
    72     public static final String ALL_DEFAULT = "ALL-DEFAULT";
    70     public static final String ALL_DEFAULT = "ALL-DEFAULT";
    73     public static final String ALL_SYSTEM = "ALL-SYSTEM";
    71     public static final String ALL_SYSTEM = "ALL-SYSTEM";
       
    72 
    74     public static final String MODULE_INFO = "module-info.class";
    73     public static final String MODULE_INFO = "module-info.class";
    75 
    74 
    76     private final SystemModuleFinder system;
    75     private final SystemModuleFinder system;
    77     private final ModuleFinder finder;
    76     private final ModuleFinder finder;
    78 
    77 
    89     private JdepsConfiguration(SystemModuleFinder systemModulePath,
    88     private JdepsConfiguration(SystemModuleFinder systemModulePath,
    90                                ModuleFinder finder,
    89                                ModuleFinder finder,
    91                                Set<String> roots,
    90                                Set<String> roots,
    92                                List<Path> classpaths,
    91                                List<Path> classpaths,
    93                                List<Archive> initialArchives,
    92                                List<Archive> initialArchives,
    94                                boolean allDefaultModules,
    93                                Set<String> tokens,
    95                                boolean allSystemModules,
       
    96                                Runtime.Version version)
    94                                Runtime.Version version)
    97         throws IOException
    95         throws IOException
    98     {
    96     {
    99         trace("root: %s%n", roots);
    97         trace("root: %s%n", roots);
   100 
    98 
   102         this.finder = finder;
   100         this.finder = finder;
   103         this.version = version;
   101         this.version = version;
   104 
   102 
   105         // build root set for resolution
   103         // build root set for resolution
   106         Set<String> mods = new HashSet<>(roots);
   104         Set<String> mods = new HashSet<>(roots);
   107 
   105         if (tokens.contains(ALL_SYSTEM)) {
   108         // add all system modules to the root set for unnamed module or set explicitly
       
   109         boolean unnamed = !initialArchives.isEmpty() || !classpaths.isEmpty();
       
   110         if (allSystemModules || (unnamed && !allDefaultModules)) {
       
   111             systemModulePath.findAll().stream()
   106             systemModulePath.findAll().stream()
   112                 .map(mref -> mref.descriptor().name())
   107                 .map(mref -> mref.descriptor().name())
   113                 .forEach(mods::add);
   108                 .forEach(mods::add);
   114         }
   109         }
   115 
   110 
   116         if (allDefaultModules) {
   111         if (tokens.contains(ALL_DEFAULT)) {
   117             mods.addAll(systemModulePath.defaultSystemRoots());
   112             mods.addAll(systemModulePath.defaultSystemRoots());
   118         }
   113         }
   119 
   114 
   120         this.configuration = Configuration.empty()
   115         this.configuration = Configuration.empty()
   121                 .resolve(finder, ModuleFinder.of(), mods);
   116                 .resolve(finder, ModuleFinder.of(), mods);
   198         Objects.requireNonNull(name);
   193         Objects.requireNonNull(name);
   199         Module m = nameToModule.get(name);
   194         Module m = nameToModule.get(name);
   200         return m!= null ? Optional.of(m.descriptor()) : Optional.empty();
   195         return m!= null ? Optional.of(m.descriptor()) : Optional.empty();
   201     }
   196     }
   202 
   197 
   203     boolean isValidToken(String name) {
   198     public static boolean isToken(String name) {
   204         return ALL_MODULE_PATH.equals(name) ||
   199         return ALL_MODULE_PATH.equals(name) ||
   205                 ALL_DEFAULT.equals(name) ||
   200                ALL_DEFAULT.equals(name) ||
   206                 ALL_SYSTEM.equals(name);
   201                ALL_SYSTEM.equals(name);
   207     }
   202     }
   208 
   203 
   209     /**
   204     /**
   210      * Returns the list of packages that split between resolved module and
   205      * Returns the list of packages that split between resolved module and
   211      * unnamed module
   206      * unnamed module
   480         final SystemModuleFinder systemModulePath;
   475         final SystemModuleFinder systemModulePath;
   481         final Set<String> rootModules = new HashSet<>();
   476         final Set<String> rootModules = new HashSet<>();
   482         final List<Archive> initialArchives = new ArrayList<>();
   477         final List<Archive> initialArchives = new ArrayList<>();
   483         final List<Path> paths = new ArrayList<>();
   478         final List<Path> paths = new ArrayList<>();
   484         final List<Path> classPaths = new ArrayList<>();
   479         final List<Path> classPaths = new ArrayList<>();
       
   480         final Set<String> tokens = new HashSet<>();
   485 
   481 
   486         ModuleFinder upgradeModulePath;
   482         ModuleFinder upgradeModulePath;
   487         ModuleFinder appModulePath;
   483         ModuleFinder appModulePath;
   488         boolean addAllApplicationModules;
       
   489         boolean addAllDefaultModules;
       
   490         boolean addAllSystemModules;
       
   491         boolean allModules;
       
   492         Runtime.Version version;
   484         Runtime.Version version;
   493 
   485 
   494         public Builder() {
   486         public Builder() {
   495             this.systemModulePath = new SystemModuleFinder();
   487             this.systemModulePath = new SystemModuleFinder();
   496         }
   488         }
   511             return this;
   503             return this;
   512         }
   504         }
   513 
   505 
   514         public Builder addmods(Set<String> addmods) {
   506         public Builder addmods(Set<String> addmods) {
   515             for (String mn : addmods) {
   507             for (String mn : addmods) {
   516                 switch (mn) {
   508                 if (isToken(mn)) {
   517                     case ALL_MODULE_PATH:
   509                     tokens.add(mn);
   518                         this.addAllApplicationModules = true;
   510                 } else {
   519                         break;
   511                     rootModules.add(mn);
   520                     case ALL_DEFAULT:
       
   521                         this.addAllDefaultModules = true;
       
   522                         break;
       
   523                     case ALL_SYSTEM:
       
   524                         this.addAllSystemModules = true;
       
   525                         break;
       
   526                     default:
       
   527                         this.rootModules.add(mn);
       
   528                 }
   512                 }
   529             }
   513             }
   530             return this;
       
   531         }
       
   532 
       
   533         /*
       
   534          * This method is for --check option to find all target modules specified
       
   535          * in qualified exports.
       
   536          *
       
   537          * Include all system modules and modules found on modulepath
       
   538          */
       
   539         public Builder allModules() {
       
   540             this.allModules = true;
       
   541             return this;
   514             return this;
   542         }
   515         }
   543 
   516 
   544         public Builder multiRelease(Runtime.Version version) {
   517         public Builder multiRelease(Runtime.Version version) {
   545             this.version = version;
   518             this.version = version;
   577                 otherModulePath.findAll().stream()
   550                 otherModulePath.findAll().stream()
   578                         .map(mref -> mref.descriptor().name())
   551                         .map(mref -> mref.descriptor().name())
   579                         .forEach(rootModules::add);
   552                         .forEach(rootModules::add);
   580             }
   553             }
   581 
   554 
   582             if ((addAllApplicationModules || allModules) && appModulePath != null) {
   555             // add all modules to the root set for unnamed module or set explicitly
       
   556             boolean unnamed = !initialArchives.isEmpty() || !classPaths.isEmpty();
       
   557             if ((unnamed || tokens.contains(ALL_MODULE_PATH)) && appModulePath != null) {
   583                 appModulePath.findAll().stream()
   558                 appModulePath.findAll().stream()
   584                     .map(mref -> mref.descriptor().name())
   559                     .map(mref -> mref.descriptor().name())
   585                     .forEach(rootModules::add);
   560                     .forEach(rootModules::add);
   586             }
   561             }
   587 
   562 
   588             // no archive is specified for analysis
   563             // no archive is specified for analysis
   589             // add all system modules as root if --add-modules ALL-SYSTEM is specified
   564             // add all system modules as root if --add-modules ALL-SYSTEM is specified
   590             if (addAllSystemModules && rootModules.isEmpty() &&
   565             if (tokens.contains(ALL_SYSTEM) && rootModules.isEmpty() &&
   591                     initialArchives.isEmpty() && classPaths.isEmpty()) {
   566                     initialArchives.isEmpty() && classPaths.isEmpty()) {
   592                 systemModulePath.findAll()
   567                 systemModulePath.findAll()
   593                     .stream()
   568                     .stream()
   594                     .map(mref -> mref.descriptor().name())
   569                     .map(mref -> mref.descriptor().name())
   595                     .forEach(rootModules::add);
   570                     .forEach(rootModules::add);
   596             }
   571             }
   597 
   572 
       
   573             if (unnamed && !tokens.contains(ALL_DEFAULT)) {
       
   574                 tokens.add(ALL_SYSTEM);
       
   575             }
       
   576 
   598             return new JdepsConfiguration(systemModulePath,
   577             return new JdepsConfiguration(systemModulePath,
   599                                           finder,
   578                                           finder,
   600                                           rootModules,
   579                                           rootModules,
   601                                           classPaths,
   580                                           classPaths,
   602                                           initialArchives,
   581                                           initialArchives,
   603                                           addAllDefaultModules,
   582                                           tokens,
   604                                           allModules,
       
   605                                           version);
   583                                           version);
   606         }
   584         }
   607 
   585 
   608         private static ModuleFinder createModulePathFinder(String mpaths) {
   586         private static ModuleFinder createModulePathFinder(String mpaths) {
   609             if (mpaths == null) {
   587             if (mpaths == null) {