langtools/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ModuleAnalyzer.java
author mchung
Fri, 11 Nov 2016 17:32:21 -0800
changeset 41997 79da2a8f4274
parent 41860 906670ff49c7
child 42407 f3702cff2933
permissions -rw-r--r--
8168386: Fix jdeps verbose options Reviewed-by: dfuchs, lancea
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) 2016, 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.Graph.*;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    28
import static com.sun.tools.jdeps.JdepsFilter.DEFAULT_FILTER;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    29
import static com.sun.tools.jdeps.Module.*;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import static java.lang.module.ModuleDescriptor.Requires.Modifier.*;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    31
import static java.util.stream.Collectors.*;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    32
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    33
import com.sun.tools.classfile.Dependency;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    34
import com.sun.tools.jdeps.JdepsTask.BadArgs;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    35
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    36
import java.io.IOException;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    37
import java.io.OutputStream;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    38
import java.io.PrintWriter;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.lang.module.ModuleDescriptor;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.nio.file.Files;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.nio.file.Path;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    42
import java.util.Collections;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import java.util.Comparator;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    44
import java.util.HashMap;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    45
import java.util.HashSet;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    46
import java.util.Map;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    47
import java.util.Optional;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    48
import java.util.Set;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    49
import java.util.function.Function;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    50
import java.util.stream.Collectors;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    51
import java.util.stream.Stream;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    52
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    53
/**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    54
 * Analyze module dependences and compare with module descriptor.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    55
 * Also identify any qualified exports not used by the target module.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    56
 */
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    57
public class ModuleAnalyzer {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    58
    private static final String JAVA_BASE = "java.base";
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    59
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    60
    private final JdepsConfiguration configuration;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    61
    private final PrintWriter log;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    62
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    63
    private final DependencyFinder dependencyFinder;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    64
    private final Map<Module, ModuleDeps> modules;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    65
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    66
    public ModuleAnalyzer(JdepsConfiguration config,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    67
                          PrintWriter log) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    68
        this(config, log, Collections.emptySet());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    69
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    70
    public ModuleAnalyzer(JdepsConfiguration config,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    71
                          PrintWriter log,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    72
                          Set<String> names) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    73
        this.configuration = config;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    74
        this.log = log;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    75
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    76
        this.dependencyFinder = new DependencyFinder(config, DEFAULT_FILTER);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    77
        if (names.isEmpty()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    78
            this.modules = configuration.rootModules().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    79
                .collect(toMap(Function.identity(), ModuleDeps::new));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    80
        } else {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    81
            this.modules = names.stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    82
                .map(configuration::findModule)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    83
                .flatMap(Optional::stream)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    84
                .collect(toMap(Function.identity(), ModuleDeps::new));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    85
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    86
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    87
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    88
    public boolean run() throws IOException {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    89
        try {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    90
            // compute "requires public" dependences
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    91
            modules.values().forEach(ModuleDeps::computeRequiresPublic);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
    92
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    93
            modules.values().forEach(md -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    94
                // compute "requires" dependences
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    95
                md.computeRequires();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    96
                // apply transitive reduction and reports recommended requires.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    97
                md.analyzeDeps();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    98
            });
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
    99
        } finally {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   100
            dependencyFinder.shutdown();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        return true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   104
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   105
    class ModuleDeps {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   106
        final Module root;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   107
        Set<Module> requiresPublic;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   108
        Set<Module> requires;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   109
        Map<String, Set<String>> unusedQualifiedExports;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   110
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   111
        ModuleDeps(Module root) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   112
            this.root = root;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   113
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   114
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   115
        /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   116
         * Compute 'requires public' dependences by analyzing API dependencies
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   117
         */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   118
        private void computeRequiresPublic() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   119
            // record requires public
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   120
            this.requiresPublic = computeRequires(true)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   121
                .filter(m -> !m.name().equals(JAVA_BASE))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   122
                .collect(toSet());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   123
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   124
            trace("requires public: %s%n", requiresPublic);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   125
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   126
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   127
        private void computeRequires() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   128
            this.requires = computeRequires(false).collect(toSet());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   129
            trace("requires: %s%n", requires);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   130
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   131
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   132
        private Stream<Module> computeRequires(boolean apionly) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   133
            // analyze all classes
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   134
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   135
            if (apionly) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   136
                dependencyFinder.parseExportedAPIs(Stream.of(root));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   137
            } else {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   138
                dependencyFinder.parse(Stream.of(root));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   139
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   140
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   141
            // find the modules of all the dependencies found
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   142
            return dependencyFinder.getDependences(root)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   143
                        .map(Archive::getModule);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   144
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   145
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   146
        ModuleDescriptor descriptor() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   147
            return descriptor(requiresPublic, requires);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   148
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   149
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   150
        private ModuleDescriptor descriptor(Set<Module> requiresPublic,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   151
                                            Set<Module> requires) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   152
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   153
            ModuleDescriptor.Builder builder = new ModuleDescriptor.Builder(root.name());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   154
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   155
            if (!root.name().equals(JAVA_BASE))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   156
                builder.requires(MANDATED, JAVA_BASE);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   157
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   158
            requiresPublic.stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   159
                .filter(m -> !m.name().equals(JAVA_BASE))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   160
                .map(Module::name)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   161
                .forEach(mn -> builder.requires(PUBLIC, mn));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   162
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   163
            requires.stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   164
                .filter(m -> !requiresPublic.contains(m))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   165
                .filter(m -> !m.name().equals(JAVA_BASE))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   166
                .map(Module::name)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   167
                .forEach(mn -> builder.requires(mn));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   168
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   169
            return builder.build();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   170
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   171
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   172
        private Graph<Module> buildReducedGraph() {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   173
            ModuleGraphBuilder rpBuilder = new ModuleGraphBuilder(configuration);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   174
            rpBuilder.addModule(root);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   175
            requiresPublic.stream()
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   176
                          .forEach(m -> rpBuilder.addEdge(root, m));
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   177
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   178
            // requires public graph
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   179
            Graph<Module> rbg = rpBuilder.build().reduce();
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   180
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   181
            ModuleGraphBuilder gb = new ModuleGraphBuilder(configuration);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   182
            gb.addModule(root);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   183
            requires.stream()
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   184
                    .forEach(m -> gb.addEdge(root, m));
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   185
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   186
            // transitive reduction
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   187
            Graph<Module> newGraph = gb.buildGraph().reduce(rbg);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   188
            if (DEBUG) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   189
                System.err.println("after transitive reduction: ");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   190
                newGraph.printGraph(log);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   191
            }
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   192
            return newGraph;
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   193
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   194
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   195
        /**
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   196
         * Apply the transitive reduction on the module graph
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   197
         * and returns the corresponding ModuleDescriptor
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   198
         */
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   199
        ModuleDescriptor reduced() {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   200
            Graph<Module> g = buildReducedGraph();
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   201
            return descriptor(requiresPublic, g.adjacentNodes(root));
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents: 40308
diff changeset
   202
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
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
         * Apply transitive reduction on the resulting graph and reports
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   206
         * recommended requires.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   207
         */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   208
        private void analyzeDeps() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   209
            printModuleDescriptor(log, root);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   210
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   211
            ModuleDescriptor analyzedDescriptor = descriptor();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   212
            if (!matches(root.descriptor(), analyzedDescriptor)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   213
                log.format("  [Suggested module descriptor for %s]%n", root.name());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   214
                analyzedDescriptor.requires()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   215
                    .stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   216
                    .sorted(Comparator.comparing(ModuleDescriptor.Requires::name))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   217
                    .forEach(req -> log.format("    requires %s;%n", req));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   218
            }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   219
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   220
            ModuleDescriptor reduced = reduced();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   221
            if (!matches(root.descriptor(), reduced)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   222
                log.format("  [Transitive reduced graph for %s]%n", root.name());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   223
                reduced.requires()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   224
                    .stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   225
                    .sorted(Comparator.comparing(ModuleDescriptor.Requires::name))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   226
                    .forEach(req -> log.format("    requires %s;%n", req));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   227
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   228
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   229
            checkQualifiedExports();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   230
            log.println();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   231
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   232
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   233
        private void checkQualifiedExports() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   234
            // detect any qualified exports not used by the target module
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   235
            unusedQualifiedExports = unusedQualifiedExports();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   236
            if (!unusedQualifiedExports.isEmpty())
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   237
                log.format("  [Unused qualified exports in %s]%n", root.name());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   238
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   239
            unusedQualifiedExports.keySet().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   240
                .sorted()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   241
                .forEach(pn -> log.format("    exports %s to %s%n", pn,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   242
                    unusedQualifiedExports.get(pn).stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   243
                        .sorted()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   244
                        .collect(joining(","))));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   245
        }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   246
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   247
        private void printModuleDescriptor(PrintWriter out, Module module) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   248
            ModuleDescriptor descriptor = module.descriptor();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   249
            out.format("%s (%s)%n", descriptor.name(), module.location());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   250
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   251
            if (descriptor.name().equals(JAVA_BASE))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   252
                return;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   253
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   254
            out.println("  [Module descriptor]");
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   255
            descriptor.requires()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   256
                .stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   257
                .sorted(Comparator.comparing(ModuleDescriptor.Requires::name))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   258
                .forEach(req -> out.format("    requires %s;%n", req));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   259
        }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   260
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   261
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   262
        /**
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   263
         * Detects any qualified exports not used by the target module.
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   264
         */
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   265
        private Map<String, Set<String>> unusedQualifiedExports() {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   266
            Map<String, Set<String>> unused = new HashMap<>();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   267
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   268
            // build the qualified exports map
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   269
            Map<String, Set<String>> qualifiedExports =
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   270
                root.exports().entrySet().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   271
                    .filter(e -> !e.getValue().isEmpty())
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   272
                    .map(Map.Entry::getKey)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   273
                    .collect(toMap(Function.identity(), _k -> new HashSet<>()));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   274
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   275
            Set<Module> mods = new HashSet<>();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   276
            root.exports().values()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   277
                .stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   278
                .flatMap(Set::stream)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   279
                .forEach(target -> configuration.findModule(target)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   280
                    .ifPresentOrElse(mods::add,
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   281
                        () -> log.format("Warning: %s not found%n", target))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   282
                );
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   283
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   284
            // parse all target modules
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   285
            dependencyFinder.parse(mods.stream());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   286
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   287
            // adds to the qualified exports map if a module references it
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   288
            mods.stream().forEach(m ->
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   289
                m.getDependencies()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   290
                    .map(Dependency.Location::getPackageName)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   291
                    .filter(qualifiedExports::containsKey)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   292
                    .forEach(pn -> qualifiedExports.get(pn).add(m.name())));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   293
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   294
            // compare with the exports from ModuleDescriptor
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   295
            Set<String> staleQualifiedExports =
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   296
                qualifiedExports.keySet().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   297
                    .filter(pn -> !qualifiedExports.get(pn).equals(root.exports().get(pn)))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   298
                    .collect(toSet());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   299
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   300
            if (!staleQualifiedExports.isEmpty()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   301
                for (String pn : staleQualifiedExports) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   302
                    Set<String> targets = new HashSet<>(root.exports().get(pn));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   303
                    targets.removeAll(qualifiedExports.get(pn));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   304
                    unused.put(pn, targets);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   305
                }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   306
            }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   307
            return unused;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   308
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   309
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   310
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   311
    private boolean matches(ModuleDescriptor md, ModuleDescriptor other) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   312
        // build requires public from ModuleDescriptor
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   313
        Set<ModuleDescriptor.Requires> reqPublic = md.requires().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   314
            .filter(req -> req.modifiers().contains(PUBLIC))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   315
            .collect(toSet());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   316
        Set<ModuleDescriptor.Requires> otherReqPublic = other.requires().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   317
            .filter(req -> req.modifiers().contains(PUBLIC))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   318
            .collect(toSet());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   319
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   320
        if (!reqPublic.equals(otherReqPublic)) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   321
            trace("mismatch requires public: %s%n", reqPublic);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   322
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   323
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   324
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   325
        Set<ModuleDescriptor.Requires> unused = md.requires().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   326
            .filter(req -> !other.requires().contains(req))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   327
            .collect(Collectors.toSet());
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   328
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   329
        if (!unused.isEmpty()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   330
            trace("mismatch requires: %s%n", unused);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   331
            return false;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   332
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   333
        return true;
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   334
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   335
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   336
    /**
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   337
     * Generate dotfile from module descriptor
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   338
     *
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   339
     * @param dir output directory
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   340
     */
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   341
    public boolean genDotFiles(Path dir) throws IOException {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   342
        Files.createDirectories(dir);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   343
        for (Module m : modules.keySet()) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   344
            genDotFile(dir, m.name());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   345
        }
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   346
        return true;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   347
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   348
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   349
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   350
    private void genDotFile(Path dir, String name) throws IOException {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   351
        try (OutputStream os = Files.newOutputStream(dir.resolve(name + ".dot"));
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   352
             PrintWriter out = new PrintWriter(os)) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   353
            Set<Module> modules = configuration.resolve(Set.of(name))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   354
                .collect(Collectors.toSet());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   355
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   356
            // transitive reduction
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   357
            Graph<String> graph = gengraph(modules);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   358
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   359
            out.format("digraph \"%s\" {%n", name);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   360
            DotGraph.printAttributes(out);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   361
            DotGraph.printNodes(out, graph);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   362
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   363
            modules.stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   364
                .map(Module::descriptor)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   365
                .sorted(Comparator.comparing(ModuleDescriptor::name))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   366
                .forEach(md -> {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   367
                    String mn = md.name();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   368
                    Set<String> requiresPublic = md.requires().stream()
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   369
                        .filter(d -> d.modifiers().contains(PUBLIC))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   370
                        .map(d -> d.name())
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   371
                        .collect(toSet());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   372
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   373
                    DotGraph.printEdges(out, graph, mn, requiresPublic);
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   374
                });
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   375
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   376
            out.println("}");
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   377
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   378
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   379
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   380
    /**
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   381
     * Returns a Graph of the given Configuration after transitive reduction.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   382
     *
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   383
     * Transitive reduction of requires public edge and requires edge have
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   384
     * to be applied separately to prevent the requires public edges
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   385
     * (e.g. U -> V) from being reduced by a path (U -> X -> Y -> V)
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   386
     * in which  V would not be re-exported from U.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   387
     */
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   388
    private Graph<String> gengraph(Set<Module> modules) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   389
        // build a Graph containing only requires public edges
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   390
        // with transitive reduction.
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   391
        Graph.Builder<String> rpgbuilder = new Graph.Builder<>();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   392
        for (Module module : modules) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   393
            ModuleDescriptor md = module.descriptor();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   394
            String mn = md.name();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   395
            md.requires().stream()
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   396
                    .filter(d -> d.modifiers().contains(PUBLIC))
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   397
                    .map(d -> d.name())
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   398
                    .forEach(d -> rpgbuilder.addEdge(mn, d));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   399
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   400
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   401
        Graph<String> rpg = rpgbuilder.build().reduce();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   402
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   403
        // build the readability graph
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   404
        Graph.Builder<String> builder = new Graph.Builder<>();
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   405
        for (Module module : modules) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   406
            ModuleDescriptor md = module.descriptor();
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   407
            String mn = md.name();
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   408
            builder.addNode(mn);
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   409
            configuration.reads(module)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   410
                    .map(Module::name)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   411
                    .forEach(d -> builder.addEdge(mn, d));
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   412
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   413
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   414
        // transitive reduction of requires edges
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   415
        return builder.build().reduce(rpg);
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   416
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   417
38524
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   418
    // ---- for testing purpose
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   419
    public ModuleDescriptor[] descriptors(String name) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   420
        ModuleDeps moduleDeps = modules.keySet().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   421
            .filter(m -> m.name().equals(name))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   422
            .map(modules::get)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   423
            .findFirst().get();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   424
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   425
        ModuleDescriptor[] descriptors = new ModuleDescriptor[3];
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   426
        descriptors[0] = moduleDeps.root.descriptor();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   427
        descriptors[1] = moduleDeps.descriptor();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   428
        descriptors[2] = moduleDeps.reduced();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   429
        return descriptors;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   430
    }
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   431
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   432
    public Map<String, Set<String>> unusedQualifiedExports(String name) {
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   433
        ModuleDeps moduleDeps = modules.keySet().stream()
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   434
            .filter(m -> m.name().equals(name))
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   435
            .map(modules::get)
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   436
            .findFirst().get();
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   437
        return moduleDeps.unusedQualifiedExports;
badd925c1d2f 8156680: jdeps implementation refresh
mchung
parents: 36526
diff changeset
   438
    }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents:
diff changeset
   439
}