langtools/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ModuleInfoBuilder.java
author alanb
Fri, 10 Feb 2017 09:06:10 +0000
changeset 43767 9cff98a149cb
parent 42407 f3702cff2933
child 45684 2c5f2779c3d1
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: mcimadamore, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
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
package com.sun.tools.jdeps;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    26
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    27
import static com.sun.tools.jdeps.JdepsTask.*;
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
    28
import static com.sun.tools.jdeps.Analyzer.*;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    29
import static com.sun.tools.jdeps.JdepsFilter.DEFAULT_FILTER;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.IOException;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.io.PrintWriter;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    33
import java.io.UncheckedIOException;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    34
import java.lang.module.ModuleDescriptor;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    35
import java.lang.module.ModuleDescriptor.Exports;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    36
import java.lang.module.ModuleDescriptor.Provides;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    37
import java.lang.module.ModuleDescriptor.Requires;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    38
import java.lang.module.ModuleFinder;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.nio.file.Files;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.nio.file.Path;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    41
import java.nio.file.Paths;
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
    42
import java.util.Collections;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    43
import java.util.Comparator;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.HashMap;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    45
import java.util.List;
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
    46
import java.util.Locale;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    47
import java.util.Map;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.Optional;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.Set;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.function.Function;
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
    51
import java.util.stream.Collectors;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
import java.util.stream.Stream;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
    53
import static java.util.stream.Collectors.*;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    55
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
public class ModuleInfoBuilder {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    57
    final JdepsConfiguration configuration;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    58
    final Path outputdir;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
    59
    final boolean open;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    60
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    final DependencyFinder dependencyFinder;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    final Analyzer analyzer;
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
    63
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
    64
    // an input JAR file (loaded as an automatic module for analysis)
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
    65
    // maps to a normal module to generate module-info.java
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
    66
    final Map<Module, Module> automaticToNormalModule;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    67
    public ModuleInfoBuilder(JdepsConfiguration configuration,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    68
                             List<String> args,
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
    69
                             Path outputdir,
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
    70
                             boolean open) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    71
        this.configuration = configuration;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    72
        this.outputdir = outputdir;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
    73
        this.open = open;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    74
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    75
        this.dependencyFinder = new DependencyFinder(configuration, DEFAULT_FILTER);
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
    76
        this.analyzer = new Analyzer(configuration, Type.CLASS, DEFAULT_FILTER);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    77
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    78
        // add targets to modulepath if it has module-info.class
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    79
        List<Path> paths = args.stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    80
            .map(fn -> Paths.get(fn))
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
    81
            .collect(toList());
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    82
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
    83
        // automatic module to convert to normal module
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
    84
        this.automaticToNormalModule = ModuleFinder.of(paths.toArray(new Path[0]))
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    85
                .findAll().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    86
                .map(configuration::toModule)
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
    87
                .collect(toMap(Function.identity(), Function.identity()));
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    88
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
    89
        Optional<Module> om = automaticToNormalModule.keySet().stream()
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    90
                                    .filter(m -> !m.descriptor().isAutomatic())
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    91
                                    .findAny();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    92
        if (om.isPresent()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    93
            throw new UncheckedBadArgs(new BadArgs("err.genmoduleinfo.not.jarfile",
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    94
                                                   om.get().getPathName()));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    95
        }
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
    96
        if (automaticToNormalModule.isEmpty()) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    97
            throw new UncheckedBadArgs(new BadArgs("err.invalid.path", args));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    98
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   100
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   101
    public boolean run() throws IOException {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   102
        try {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   103
            // pass 1: find API dependencies
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   104
            Map<Archive, Set<Archive>> requiresTransitive = computeRequiresTransitive();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   105
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   106
            // pass 2: analyze all class dependences
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   107
            dependencyFinder.parse(automaticModules().stream());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   108
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   109
            analyzer.run(automaticModules(), dependencyFinder.locationToArchive());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   110
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   111
            boolean missingDeps = false;
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   112
            for (Module m : automaticModules()) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   113
                Set<Archive> apiDeps = requiresTransitive.containsKey(m)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   114
                                            ? requiresTransitive.get(m)
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   115
                                            : Collections.emptySet();
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   116
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   117
                Path file = outputdir.resolve(m.name()).resolve("module-info.java");
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   118
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   119
                // computes requires and requires transitive
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   120
                Module normalModule = toNormalModule(m, apiDeps);
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   121
                if (normalModule != null) {
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   122
                    automaticToNormalModule.put(m, normalModule);
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   123
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   124
                    // generate module-info.java
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   125
                    System.out.format("writing to %s%n", file);
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   126
                    writeModuleInfo(file,  normalModule.descriptor());
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   127
                } else {
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   128
                    // find missing dependences
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   129
                    System.out.format("Missing dependence: %s not generated%n", file);
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   130
                    missingDeps = true;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   131
                }
38530
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38524
diff changeset
   132
            }
8e89d567748c 8157391: jdeps left JarFile open
mchung
parents: 38524
diff changeset
   133
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   134
            return !missingDeps;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   135
        } finally {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   136
            dependencyFinder.shutdown();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   137
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   138
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   139
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   140
    boolean notFound(Archive m) {
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   141
        return m == NOT_FOUND || m == REMOVED_JDK_INTERNALS;
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   142
    }
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   143
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   144
    private Module toNormalModule(Module module, Set<Archive> requiresTransitive)
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   145
        throws IOException
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   146
    {
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   147
        // done analysis
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   148
        module.close();
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   149
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   150
        if (analyzer.requires(module).anyMatch(this::notFound)) {
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   151
            // missing dependencies
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   152
            return null;
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   153
        }
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   154
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   155
        Map<String, Boolean> requires = new HashMap<>();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   156
        requiresTransitive.stream()
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   157
            .map(Archive::getModule)
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   158
            .forEach(m -> requires.put(m.name(), Boolean.TRUE));
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   159
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   160
        analyzer.requires(module)
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   161
            .map(Archive::getModule)
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   162
            .forEach(d -> requires.putIfAbsent(d.name(), Boolean.FALSE));
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   163
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   164
        return module.toNormalModule(requires);
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   165
    }
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   166
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   167
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   168
     * Returns the stream of resulting modules
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   169
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   170
    Stream<Module> modules() {
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   171
        return automaticToNormalModule.values().stream();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   172
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   173
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   174
    /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   175
     * Returns the stream of resulting ModuleDescriptors
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   176
     */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   177
    public Stream<ModuleDescriptor> descriptors() {
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   178
        return automaticToNormalModule.entrySet().stream()
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   179
                    .map(Map.Entry::getValue)
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   180
                    .map(Module::descriptor);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   181
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   182
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   183
    void visitMissingDeps(Analyzer.Visitor visitor) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   184
        automaticModules().stream()
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   185
            .filter(m -> analyzer.requires(m).anyMatch(this::notFound))
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   186
            .forEach(m -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   187
                analyzer.visitDependences(m, visitor, Analyzer.Type.VERBOSE);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   188
            });
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   189
    }
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   190
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   191
    void writeModuleInfo(Path file, ModuleDescriptor md) {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   192
        try {
38532
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   193
            Files.createDirectories(file.getParent());
24f77d64bb1f 8153042: jdeps should continue to report JDK internal APIs that are removed/renamed in JDK
mchung
parents: 38530
diff changeset
   194
            try (PrintWriter pw = new PrintWriter(Files.newOutputStream(file))) {
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   195
                printModuleInfo(pw, md);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   196
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   197
        } catch (IOException e) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   198
            throw new UncheckedIOException(e);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   199
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   200
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   201
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   202
    private void printModuleInfo(PrintWriter writer, ModuleDescriptor md) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   203
        writer.format("%smodule %s {%n", open ? "open " : "", md.name());
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   204
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   205
        Map<String, Module> modules = configuration.getModules();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   206
        // first print the JDK modules
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   207
        md.requires().stream()
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   208
          .filter(req -> !req.name().equals("java.base"))   // implicit requires
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   209
          .sorted(Comparator.comparing(Requires::name))
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   210
          .forEach(req -> writer.format("    requires %s;%n",
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   211
                                        toString(req.modifiers(), req.name())));
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   212
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   213
        if (!open) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   214
            md.exports().stream()
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   215
              .peek(exp -> {
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   216
                  if (exp.isQualified())
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   217
                      throw new InternalError(md.name() + " qualified exports: " + exp);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   218
              })
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   219
              .sorted(Comparator.comparing(Exports::source))
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   220
              .forEach(exp -> writer.format("    exports %s;%n", exp.source()));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   221
        }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   222
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   223
        md.provides().stream()
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   224
          .sorted(Comparator.comparing(Provides::service))
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   225
          .map(p -> p.providers().stream()
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   226
                     .map(impl -> "        " + impl.replace('$', '.'))
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   227
                     .collect(joining(",\n",
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   228
                                      String.format("    provides %s with%n",
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   229
                                                    p.service().replace('$', '.')),
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   230
                                      ";")))
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   231
          .forEach(writer::println);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   232
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   233
        writer.println("}");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   234
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   235
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   236
    private Set<Module> automaticModules() {
43767
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   237
        return automaticToNormalModule.keySet();
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   238
    }
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   239
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   240
    /**
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   241
     * Returns a string containing the given set of modifiers and label.
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   242
     */
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   243
    private static <M> String toString(Set<M> mods, String what) {
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   244
        return (Stream.concat(mods.stream().map(e -> e.toString().toLowerCase(Locale.US)),
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   245
                              Stream.of(what)))
9cff98a149cb 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42407
diff changeset
   246
                      .collect(Collectors.joining(" "));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   247
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   248
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   249
    /**
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   250
     * Compute 'requires transitive' dependences by analyzing API dependencies
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   251
     */
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   252
    private Map<Archive, Set<Archive>> computeRequiresTransitive()
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   253
        throws IOException
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 38532
diff changeset
   254
    {
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   255
        // parse the input modules
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   256
        dependencyFinder.parseExportedAPIs(automaticModules().stream());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   257
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   258
        return dependencyFinder.dependences();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   259
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   260
}