src/jdk.jdeps/share/classes/com/sun/tools/jdeps/JdepsConfiguration.java
author serb
Sat, 09 Jun 2018 13:33:35 -0700
changeset 50647 a98ff7c2103d
parent 48253 82767203606e
child 50598 8d9d4d91be7f
permissions -rw-r--r--
6608234: SwingWorker.get throws CancellationException Reviewed-by: psadhukhan, kaddepalli, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43767
diff changeset
     2
 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    25
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package com.sun.tools.jdeps;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    27
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    28
import static com.sun.tools.jdeps.Module.trace;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    29
import static java.util.stream.Collectors.*;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    30
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    31
import com.sun.tools.classfile.Dependency;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    32
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    33
import java.io.BufferedInputStream;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.io.File;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.io.FileNotFoundException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.io.IOException;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    37
import java.io.InputStream;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.io.UncheckedIOException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.lang.module.Configuration;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.lang.module.ModuleDescriptor;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.lang.module.ModuleFinder;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    42
import java.lang.module.ModuleReader;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.lang.module.ModuleReference;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.lang.module.ResolvedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.net.URI;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    46
import java.nio.file.DirectoryStream;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.nio.file.FileSystem;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.nio.file.FileSystems;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.nio.file.Files;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.nio.file.Path;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import java.nio.file.Paths;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    52
import java.util.ArrayList;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    53
import java.util.Collections;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    54
import java.util.HashMap;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    55
import java.util.HashSet;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    56
import java.util.LinkedHashMap;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    57
import java.util.LinkedHashSet;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    58
import java.util.List;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    59
import java.util.Map;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    60
import java.util.Objects;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    61
import java.util.Optional;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    62
import java.util.Set;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    63
import java.util.function.Function;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    64
import java.util.function.Supplier;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    65
import java.util.stream.Stream;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    66
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
    67
public class JdepsConfiguration implements AutoCloseable {
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
    68
    // the token for "all modules on the module path"
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
    69
    public static final String ALL_MODULE_PATH = "ALL-MODULE-PATH";
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
    70
    public static final String ALL_DEFAULT = "ALL-DEFAULT";
42840
dfe1a03d4db4 8171418: Remove jdeps internal --include-system-modules option
mchung
parents: 42827
diff changeset
    71
    public static final String ALL_SYSTEM = "ALL-SYSTEM";
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
    72
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
    73
    public static final String MODULE_INFO = "module-info.class";
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    74
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    75
    private final SystemModuleFinder system;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    76
    private final ModuleFinder finder;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    77
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    78
    private final Map<String, Module> nameToModule = new LinkedHashMap<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    79
    private final Map<String, Module> packageToModule = new HashMap<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    80
    private final Map<String, List<Archive>> packageToUnnamedModule = new HashMap<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    81
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    82
    private final List<Archive> classpathArchives = new ArrayList<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    83
    private final List<Archive> initialArchives = new ArrayList<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    84
    private final Set<Module> rootModules = new HashSet<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    85
    private final Configuration configuration;
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
    86
    private final Runtime.Version version;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    87
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    88
    private JdepsConfiguration(SystemModuleFinder systemModulePath,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    89
                               ModuleFinder finder,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    90
                               Set<String> roots,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    91
                               List<Path> classpaths,
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
    92
                               List<Archive> initialArchives,
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
    93
                               Set<String> tokens,
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
    94
                               Runtime.Version version)
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    95
        throws IOException
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    96
    {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    97
        trace("root: %s%n", roots);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    98
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    99
        this.system = systemModulePath;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   100
        this.finder = finder;
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   101
        this.version = version;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   103
        // build root set for resolution
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   104
        Set<String> mods = new HashSet<>(roots);
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   105
        if (tokens.contains(ALL_SYSTEM)) {
41998
feae61dc2280 8169606: jdeps --list-reduced-deps should not show java.base as all modules require it
mchung
parents: 41859
diff changeset
   106
            systemModulePath.findAll().stream()
feae61dc2280 8169606: jdeps --list-reduced-deps should not show java.base as all modules require it
mchung
parents: 41859
diff changeset
   107
                .map(mref -> mref.descriptor().name())
feae61dc2280 8169606: jdeps --list-reduced-deps should not show java.base as all modules require it
mchung
parents: 41859
diff changeset
   108
                .forEach(mods::add);
feae61dc2280 8169606: jdeps --list-reduced-deps should not show java.base as all modules require it
mchung
parents: 41859
diff changeset
   109
        }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   110
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   111
        if (tokens.contains(ALL_DEFAULT)) {
43026
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   112
            mods.addAll(systemModulePath.defaultSystemRoots());
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   113
        }
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   114
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   115
        this.configuration = Configuration.empty()
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43026
diff changeset
   116
                .resolve(finder, ModuleFinder.of(), mods);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   117
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   118
        this.configuration.modules().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   119
                .map(ResolvedModule::reference)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   120
                .forEach(this::addModuleReference);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   121
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   122
        // packages in unnamed module
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   123
        initialArchives.forEach(archive -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   124
            addPackagesInUnnamedModule(archive);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   125
            this.initialArchives.add(archive);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   126
        });
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   127
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   128
        // classpath archives
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   129
        for (Path p : classpaths) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   130
            if (Files.exists(p)) {
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   131
                Archive archive = Archive.getInstance(p, version);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   132
                addPackagesInUnnamedModule(archive);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   133
                classpathArchives.add(archive);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   134
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   135
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   136
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40308
diff changeset
   137
        // all roots specified in --add-modules or -m are included
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   138
        // as the initial set for analysis.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   139
        roots.stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   140
             .map(nameToModule::get)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   141
             .forEach(this.rootModules::add);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   142
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   143
        initProfiles();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   144
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   145
        trace("resolved modules: %s%n", nameToModule.keySet().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   146
                .sorted().collect(joining("\n", "\n", "")));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   147
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   148
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   149
    private void initProfiles() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   150
        // other system modules are not observed and not added in nameToModule map
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   151
        Map<String, Module> systemModules =
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   152
            system.moduleNames()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   153
                .collect(toMap(Function.identity(), (mn) -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   154
                    Module m = nameToModule.get(mn);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   155
                    if (m == null) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   156
                        ModuleReference mref = finder.find(mn).get();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   157
                        m = toModule(mref);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   158
                    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   159
                    return m;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   160
                }));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   161
        Profile.init(systemModules);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   162
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   163
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   164
    private void addModuleReference(ModuleReference mref) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   165
        Module module = toModule(mref);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   166
        nameToModule.put(mref.descriptor().name(), module);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   167
        mref.descriptor().packages()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   168
            .forEach(pn -> packageToModule.putIfAbsent(pn, module));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   169
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   170
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   171
    private void addPackagesInUnnamedModule(Archive archive) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   172
        archive.reader().entries().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   173
               .filter(e -> e.endsWith(".class") && !e.equals(MODULE_INFO))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   174
               .map(this::toPackageName)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   175
               .distinct()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   176
               .forEach(pn -> packageToUnnamedModule
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   177
                   .computeIfAbsent(pn, _n -> new ArrayList<>()).add(archive));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   178
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   179
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   180
    private String toPackageName(String name) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   181
        int i = name.lastIndexOf('/');
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   182
        return i > 0 ? name.replace('/', '.').substring(0, i) : "";
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   183
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   184
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   185
    public Optional<Module> findModule(String name) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   186
        Objects.requireNonNull(name);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   187
        Module m = nameToModule.get(name);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   188
        return m!= null ? Optional.of(m) : Optional.empty();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   189
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   190
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   191
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   192
    public Optional<ModuleDescriptor> findModuleDescriptor(String name) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   193
        Objects.requireNonNull(name);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   194
        Module m = nameToModule.get(name);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   195
        return m!= null ? Optional.of(m.descriptor()) : Optional.empty();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   196
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   197
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   198
    public static boolean isToken(String name) {
42840
dfe1a03d4db4 8171418: Remove jdeps internal --include-system-modules option
mchung
parents: 42827
diff changeset
   199
        return ALL_MODULE_PATH.equals(name) ||
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   200
               ALL_DEFAULT.equals(name) ||
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   201
               ALL_SYSTEM.equals(name);
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   202
    }
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   203
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   204
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   205
     * Returns the list of packages that split between resolved module and
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   206
     * unnamed module
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   207
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   208
    public Map<String, Set<String>> splitPackages() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   209
        Set<String> splitPkgs = packageToModule.keySet().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   210
                                       .filter(packageToUnnamedModule::containsKey)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   211
                                       .collect(toSet());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   212
        if (splitPkgs.isEmpty())
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   213
            return Collections.emptyMap();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   214
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   215
        return splitPkgs.stream().collect(toMap(Function.identity(), (pn) -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   216
            Set<String> sources = new LinkedHashSet<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   217
            sources.add(packageToModule.get(pn).getModule().location().toString());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   218
            packageToUnnamedModule.get(pn).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   219
                .map(Archive::getPathName)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   220
                .forEach(sources::add);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   221
            return sources;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   222
        }));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   223
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   224
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   225
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   226
     * Returns an optional archive containing the given Location
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   227
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   228
    public Optional<Archive> findClass(Dependency.Location location) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   229
        String name = location.getName();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   230
        int i = name.lastIndexOf('/');
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   231
        String pn = i > 0 ? name.substring(0, i).replace('/', '.') : "";
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   232
        Archive archive = packageToModule.get(pn);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   233
        if (archive != null) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   234
            return archive.contains(name + ".class")
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   235
                        ? Optional.of(archive)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   236
                        : Optional.empty();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   237
        }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   238
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   239
        if (packageToUnnamedModule.containsKey(pn)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   240
            return packageToUnnamedModule.get(pn).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   241
                    .filter(a -> a.contains(name + ".class"))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   242
                    .findFirst();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   244
        return Optional.empty();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   245
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   246
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   247
    /**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   248
     * Returns the list of Modules that can be found in the specified
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   249
     * module paths.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   250
     */
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   251
    public Map<String, Module> getModules() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   252
        return nameToModule;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   253
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   254
43873
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43767
diff changeset
   255
    /**
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43767
diff changeset
   256
     * Returns Configuration with the given roots
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43767
diff changeset
   257
     */
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43767
diff changeset
   258
    public Configuration resolve(Set<String> roots) {
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43767
diff changeset
   259
        if (roots.isEmpty())
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43767
diff changeset
   260
            throw new IllegalArgumentException("empty roots");
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43767
diff changeset
   261
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43767
diff changeset
   262
        return Configuration.empty()
705d732d3715 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43767
diff changeset
   263
                    .resolve(finder, ModuleFinder.of(), roots);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   264
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   265
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   266
    public List<Archive> classPathArchives() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   267
        return classpathArchives;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   268
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   269
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   270
    public List<Archive> initialArchives() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   271
        return initialArchives;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   272
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   273
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   274
    public Set<Module> rootModules() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   275
        return rootModules;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   276
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   277
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   278
    public Module toModule(ModuleReference mref) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   279
        try {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   280
            String mn = mref.descriptor().name();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   281
            URI location = mref.location().orElseThrow(FileNotFoundException::new);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   282
            ModuleDescriptor md = mref.descriptor();
47239
2088dbaa2282 8187449: jdeps fails when an upgradeable module is upgraded with an automatic module
mchung
parents: 47216
diff changeset
   283
            // is this module from the system module path?
2088dbaa2282 8187449: jdeps fails when an upgradeable module is upgraded with an automatic module
mchung
parents: 47216
diff changeset
   284
            URI loc = system.find(mn).flatMap(ModuleReference::location).orElse(null);
2088dbaa2282 8187449: jdeps fails when an upgradeable module is upgraded with an automatic module
mchung
parents: 47216
diff changeset
   285
            boolean isSystem = location.equals(loc);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   286
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   287
            final ClassFileReader reader;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   288
            if (location.getScheme().equals("jrt")) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   289
                reader = system.getClassReader(mn);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   290
            } else {
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   291
                reader = ClassFileReader.newInstance(Paths.get(location), version);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   292
            }
47239
2088dbaa2282 8187449: jdeps fails when an upgradeable module is upgraded with an automatic module
mchung
parents: 47216
diff changeset
   293
            Module.Builder builder = new Module.Builder(md, isSystem);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   294
            builder.classes(reader);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   295
            builder.location(location);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   296
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   297
            return builder.build();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   298
        } catch (IOException e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   299
            throw new UncheckedIOException(e);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   300
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   301
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   302
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   303
    public Runtime.Version getVersion() {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   304
        return version;
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   305
    }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   306
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   307
    /*
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   308
     * Close all archives e.g. JarFile
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   309
     */
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   310
    @Override
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   311
    public void close() throws IOException {
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   312
        for (Archive archive : initialArchives)
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   313
            archive.close();
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   314
        for (Archive archive : classpathArchives)
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   315
            archive.close();
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   316
        for (Module module : nameToModule.values())
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   317
            module.close();
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   318
    }
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38529
diff changeset
   319
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   320
    static class SystemModuleFinder implements ModuleFinder {
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   321
        private static final String JAVA_HOME = System.getProperty("java.home");
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   322
        private static final String JAVA_SE = "java.se";
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   323
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   324
        private final FileSystem fileSystem;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   325
        private final Path root;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   326
        private final Map<String, ModuleReference> systemModules;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   327
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   328
        SystemModuleFinder() {
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   329
            if (Files.isRegularFile(Paths.get(JAVA_HOME, "lib", "modules"))) {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   330
                // jrt file system
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   331
                this.fileSystem = FileSystems.getFileSystem(URI.create("jrt:/"));
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   332
                this.root = fileSystem.getPath("/modules");
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   333
                this.systemModules = walk(root);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   334
            } else {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   335
                // exploded image
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   336
                this.fileSystem = FileSystems.getDefault();
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   337
                root = Paths.get(JAVA_HOME, "modules");
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   338
                this.systemModules = ModuleFinder.ofSystem().findAll().stream()
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   339
                    .collect(toMap(mref -> mref.descriptor().name(), Function.identity()));
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   340
            }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   341
        }
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   342
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   343
        SystemModuleFinder(String javaHome) throws IOException {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   344
            if (javaHome == null) {
40762
f8883aa0053c 8160851: Remove old launcher module-related options
mchung
parents: 40308
diff changeset
   345
                // --system none
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   346
                this.fileSystem = null;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   347
                this.root = null;
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   348
                this.systemModules = Collections.emptyMap();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   349
            } else {
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   350
                if (Files.isRegularFile(Paths.get(javaHome, "lib", "modules")))
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   351
                    throw new IllegalArgumentException("Invalid java.home: " + javaHome);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   352
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   353
                // alternate java.home
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   354
                Map<String, String> env = new HashMap<>();
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   355
                env.put("java.home", javaHome);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   356
                // a remote run-time image
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   357
                this.fileSystem = FileSystems.newFileSystem(URI.create("jrt:/"), env);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   358
                this.root = fileSystem.getPath("/modules");
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   359
                this.systemModules = walk(root);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   360
            }
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   361
        }
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   362
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   363
        private Map<String, ModuleReference> walk(Path root) {
38529
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38525
diff changeset
   364
            try (Stream<Path> stream = Files.walk(root, 1)) {
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38525
diff changeset
   365
                return stream.filter(path -> !path.equals(root))
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38525
diff changeset
   366
                             .map(this::toModuleReference)
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38525
diff changeset
   367
                             .collect(toMap(mref -> mref.descriptor().name(),
58cf7e51c16d 8152502: tools/jdeps/modules/GenModuleInfo.java and TransitiveDeps fails on windows
mchung
parents: 38525
diff changeset
   368
                                            Function.identity()));
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   369
            } catch (IOException e) {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   370
                throw new UncheckedIOException(e);
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   371
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   372
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   373
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   374
        private ModuleReference toModuleReference(Path path) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   375
            Path minfo = path.resolve(MODULE_INFO);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   376
            try (InputStream in = Files.newInputStream(minfo);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   377
                 BufferedInputStream bin = new BufferedInputStream(in)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   378
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   379
                ModuleDescriptor descriptor = dropHashes(ModuleDescriptor.read(bin));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   380
                String mn = descriptor.name();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   381
                URI uri = URI.create("jrt:/" + path.getFileName().toString());
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   382
                Supplier<ModuleReader> readerSupplier = () -> new ModuleReader() {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   383
                    @Override
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   384
                    public Optional<URI> find(String name) throws IOException {
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   385
                        return name.equals(mn)
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   386
                            ? Optional.of(uri) : Optional.empty();
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   387
                    }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   388
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   389
                    @Override
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   390
                    public Stream<String> list() {
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   391
                        return Stream.empty();
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   392
                    }
41859
85710a227743 8168789: ModuleReader.list and ModuleFinder.of update
alanb
parents: 41164
diff changeset
   393
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   394
                    @Override
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42822
diff changeset
   395
                    public void close() {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   396
                    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   397
                };
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   398
42822
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42407
diff changeset
   399
                return new ModuleReference(descriptor, uri) {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42407
diff changeset
   400
                    @Override
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42407
diff changeset
   401
                    public ModuleReader open() {
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42407
diff changeset
   402
                        return readerSupplier.get();
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42407
diff changeset
   403
                    }
a84956e7ee4d 8170987: Module system implementation refresh (12/2016)
alanb
parents: 42407
diff changeset
   404
                };
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   405
            } catch (IOException e) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   406
                throw new UncheckedIOException(e);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   407
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   408
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   409
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   410
        private ModuleDescriptor dropHashes(ModuleDescriptor md) {
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43026
diff changeset
   411
            ModuleDescriptor.Builder builder = ModuleDescriptor.newModule(md.name());
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   412
            md.requires().forEach(builder::requires);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   413
            md.exports().forEach(builder::exports);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41998
diff changeset
   414
            md.opens().forEach(builder::opens);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41998
diff changeset
   415
            md.provides().stream().forEach(builder::provides);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   416
            md.uses().stream().forEach(builder::uses);
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43026
diff changeset
   417
            builder.packages(md.packages());
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   418
            return builder.build();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   419
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   420
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   421
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   422
        public Set<ModuleReference> findAll() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   423
            return systemModules.values().stream().collect(toSet());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   424
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   425
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   426
        @Override
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   427
        public Optional<ModuleReference> find(String mn) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   428
            return systemModules.containsKey(mn)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   429
                    ? Optional.of(systemModules.get(mn)) : Optional.empty();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   430
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   431
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   432
        public Stream<String> moduleNames() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   433
            return systemModules.values().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   434
                .map(mref -> mref.descriptor().name());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   435
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   436
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   437
        public ClassFileReader getClassReader(String modulename) throws IOException {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   438
            Path mp = root.resolve(modulename);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   439
            if (Files.exists(mp) && Files.isDirectory(mp)) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   440
                return ClassFileReader.newInstance(fileSystem, mp);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   441
            } else {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   442
                throw new FileNotFoundException(mp.toString());
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   443
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   444
        }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   445
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   446
        public Set<String> defaultSystemRoots() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   447
            Set<String> roots = new HashSet<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   448
            boolean hasJava = false;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   449
            if (systemModules.containsKey(JAVA_SE)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   450
                // java.se is a system module
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   451
                hasJava = true;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   452
                roots.add(JAVA_SE);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   453
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   454
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   455
            for (ModuleReference mref : systemModules.values()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   456
                String mn = mref.descriptor().name();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   457
                if (hasJava && mn.startsWith("java."))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   458
                    continue;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   459
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   460
                // add as root if observable and exports at least one package
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   461
                ModuleDescriptor descriptor = mref.descriptor();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   462
                for (ModuleDescriptor.Exports e : descriptor.exports()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   463
                    if (!e.isQualified()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   464
                        roots.add(mn);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   465
                        break;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   466
                    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   467
                }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   468
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   469
            return roots;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   470
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   471
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   472
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   473
    public static class Builder {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   474
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   475
        final SystemModuleFinder systemModulePath;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   476
        final Set<String> rootModules = new HashSet<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   477
        final List<Archive> initialArchives = new ArrayList<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   478
        final List<Path> paths = new ArrayList<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   479
        final List<Path> classPaths = new ArrayList<>();
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   480
        final Set<String> tokens = new HashSet<>();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   481
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   482
        ModuleFinder upgradeModulePath;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   483
        ModuleFinder appModulePath;
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   484
        Runtime.Version version;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   485
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   486
        public Builder() {
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   487
            this.systemModulePath = new SystemModuleFinder();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   488
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   489
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   490
        public Builder(String javaHome) throws IOException {
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   491
            this.systemModulePath = SystemModuleFinder.JAVA_HOME.equals(javaHome)
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   492
                ? new SystemModuleFinder()
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   493
                : new SystemModuleFinder(javaHome);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   494
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   495
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   496
        public Builder upgradeModulePath(String upgradeModulePath) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   497
            this.upgradeModulePath = createModulePathFinder(upgradeModulePath);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   498
            return this;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   499
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   500
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   501
        public Builder appModulePath(String modulePath) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   502
            this.appModulePath = createModulePathFinder(modulePath);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   503
            return this;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   504
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   505
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   506
        public Builder addmods(Set<String> addmods) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   507
            for (String mn : addmods) {
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   508
                if (isToken(mn)) {
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   509
                    tokens.add(mn);
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   510
                } else {
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   511
                    rootModules.add(mn);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   512
                }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   513
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   514
            return this;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   515
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   516
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   517
        public Builder multiRelease(Runtime.Version version) {
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   518
            this.version = version;
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   519
            return this;
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   520
        }
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   521
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   522
        public Builder addRoot(Path path) {
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   523
            Archive archive = Archive.getInstance(path, version);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   524
            if (archive.contains(MODULE_INFO)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   525
                paths.add(path);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   526
            } else {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   527
                initialArchives.add(archive);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   528
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   529
            return this;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   530
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   531
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   532
        public Builder addClassPath(String classPath) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   533
            this.classPaths.addAll(getClassPaths(classPath));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   534
            return this;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   535
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   536
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   537
        public JdepsConfiguration build() throws  IOException {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   538
            ModuleFinder finder = systemModulePath;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   539
            if (upgradeModulePath != null) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   540
                finder = ModuleFinder.compose(upgradeModulePath, systemModulePath);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   541
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   542
            if (appModulePath != null) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   543
                finder = ModuleFinder.compose(finder, appModulePath);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   544
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   545
            if (!paths.isEmpty()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   546
                ModuleFinder otherModulePath = ModuleFinder.of(paths.toArray(new Path[0]));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   547
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   548
                finder = ModuleFinder.compose(finder, otherModulePath);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   549
                // add modules specified on command-line (convenience) as root set
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   550
                otherModulePath.findAll().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   551
                        .map(mref -> mref.descriptor().name())
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   552
                        .forEach(rootModules::add);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   553
            }
43026
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   554
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   555
            // add all modules to the root set for unnamed module or set explicitly
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   556
            boolean unnamed = !initialArchives.isEmpty() || !classPaths.isEmpty();
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   557
            if ((unnamed || tokens.contains(ALL_MODULE_PATH)) && appModulePath != null) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   558
                appModulePath.findAll().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   559
                    .map(mref -> mref.descriptor().name())
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   560
                    .forEach(rootModules::add);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   561
            }
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   562
43026
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   563
            // no archive is specified for analysis
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   564
            // add all system modules as root if --add-modules ALL-SYSTEM is specified
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   565
            if (tokens.contains(ALL_SYSTEM) && rootModules.isEmpty() &&
43026
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   566
                    initialArchives.isEmpty() && classPaths.isEmpty()) {
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   567
                systemModulePath.findAll()
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   568
                    .stream()
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   569
                    .map(mref -> mref.descriptor().name())
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   570
                    .forEach(rootModules::add);
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   571
            }
8e8b50c7491d 8172212: jdeps --require and --check should detect the specified module in the image
mchung
parents: 42840
diff changeset
   572
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   573
            if (unnamed && !tokens.contains(ALL_DEFAULT)) {
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   574
                tokens.add(ALL_SYSTEM);
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   575
            }
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   576
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   577
            return new JdepsConfiguration(systemModulePath,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   578
                                          finder,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   579
                                          rootModules,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   580
                                          classPaths,
38525
a2169f8fa712 8156575: Add jdeps -addmods, -system, -inverse options
mchung
parents: 38524
diff changeset
   581
                                          initialArchives,
48253
82767203606e 8193192: jdeps --generate-module-info does not look at module path
mchung
parents: 47239
diff changeset
   582
                                          tokens,
41164
69167c89e68f 8153654: Update jdeps to be multi-release jar aware
sdrach
parents: 40762
diff changeset
   583
                                          version);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   584
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   585
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   586
        private static ModuleFinder createModulePathFinder(String mpaths) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   587
            if (mpaths == null) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   588
                return null;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   589
            } else {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   590
                String[] dirs = mpaths.split(File.pathSeparator);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   591
                Path[] paths = new Path[dirs.length];
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   592
                int i = 0;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   593
                for (String dir : dirs) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   594
                    paths[i++] = Paths.get(dir);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   595
                }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   596
                return ModuleFinder.of(paths);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   597
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   598
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   599
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   600
        /*
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   601
         * Returns the list of Archive specified in cpaths and not included
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   602
         * initialArchives
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   603
         */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   604
        private List<Path> getClassPaths(String cpaths) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   605
            if (cpaths.isEmpty()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   606
                return Collections.emptyList();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   607
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   608
            List<Path> paths = new ArrayList<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   609
            for (String p : cpaths.split(File.pathSeparator)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   610
                if (p.length() > 0) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   611
                    // wildcard to parse all JAR files e.g. -classpath dir/*
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   612
                    int i = p.lastIndexOf(".*");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   613
                    if (i > 0) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   614
                        Path dir = Paths.get(p.substring(0, i));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   615
                        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.jar")) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   616
                            for (Path entry : stream) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   617
                                paths.add(entry);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   618
                            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   619
                        } catch (IOException e) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   620
                            throw new UncheckedIOException(e);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   621
                        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   622
                    } else {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   623
                        paths.add(Paths.get(p));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   624
                    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   625
                }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   626
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   627
            return paths;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   628
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   629
    }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   630
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   631
}