src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ModuleGraphBuilder.java
author serb
Sat, 09 Jun 2018 13:33:35 -0700
changeset 50647 a98ff7c2103d
parent 47216 71c04702a3d5
permissions -rw-r--r--
6608234: SwingWorker.get throws CancellationException Reviewed-by: psadhukhan, kaddepalli, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
     1
/*
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
     4
 *
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    10
 *
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    15
 * accompanied this code).
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    16
 *
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    20
 *
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    23
 * questions.
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    24
 */
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    25
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    26
package com.sun.tools.jdeps;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    27
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    28
import java.io.PrintWriter;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    29
import java.lang.module.ModuleDescriptor;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    30
import java.util.Deque;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    31
import java.util.HashSet;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    32
import java.util.LinkedList;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    33
import java.util.Map;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    34
import java.util.Optional;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    35
import java.util.Set;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    36
import java.util.stream.Stream;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    37
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    38
import static java.lang.module.ModuleDescriptor.Requires.Modifier.*;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    39
import static com.sun.tools.jdeps.Module.*;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    40
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    41
/**
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    42
 * A builder to create a Graph<Module>
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    43
 */
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    44
public class ModuleGraphBuilder extends Graph.Builder<Module> {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    45
    final JdepsConfiguration config;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    46
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    47
    ModuleGraphBuilder(JdepsConfiguration config) {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    48
        this.config = config;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    49
    }
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    50
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    51
    /**
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    52
     * Adds a module to the graph.
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    53
     */
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    54
    ModuleGraphBuilder addModule(Module module) {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    55
        addNode(module);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    56
        return this;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    57
    }
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    58
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    59
    /**
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    60
     * Apply transitive reduction on the resulting graph
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    61
     */
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    62
    public Graph<Module> reduced() {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    63
        Graph<Module> graph = build();
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    64
        // transitive reduction
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    65
        Graph<Module> newGraph = buildGraph(graph.edges()).reduce();
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    66
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    67
        if (DEBUG) {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    68
            PrintWriter log = new PrintWriter(System.err);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    69
            System.err.println("before transitive reduction: ");
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    70
            graph.printGraph(log);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    71
            System.err.println("after transitive reduction: ");
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    72
            newGraph.printGraph(log);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    73
        }
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    74
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    75
        return newGraph;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    76
    }
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    77
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    78
    public Graph<Module> buildGraph() {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    79
        Graph<Module> graph = build();
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    80
        return buildGraph(graph.edges());
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    81
    }
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    82
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    83
    /**
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    84
     * Build a graph of module from the given dependences.
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    85
     *
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    86
     * It transitively includes all implied read edges.
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    87
     */
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    88
    private Graph<Module> buildGraph(Map<Module, Set<Module>> edges) {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    89
        Graph.Builder<Module> builder = new Graph.Builder<>();
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    90
        Set<Module> visited = new HashSet<>();
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    91
        Deque<Module> deque = new LinkedList<>();
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    92
        edges.entrySet().stream().forEach(e -> {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    93
            Module m = e.getKey();
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    94
            deque.add(m);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    95
            e.getValue().stream().forEach(v -> {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    96
                deque.add(v);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    97
                builder.addEdge(m, v);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    98
            });
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
    99
        });
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   100
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41860
diff changeset
   101
        // read requires transitive from ModuleDescriptor
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   102
        Module source;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   103
        while ((source = deque.poll()) != null) {
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   104
            if (visited.contains(source))
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   105
                continue;
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   106
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   107
            visited.add(source);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   108
            builder.addNode(source);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   109
            Module from = source;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41860
diff changeset
   110
            requiresTransitive(from).forEach(m -> {
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   111
                deque.add(m);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   112
                builder.addEdge(from, m);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   113
            });
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   114
        }
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   115
        return builder.build();
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   116
    }
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   117
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   118
    /*
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41860
diff changeset
   119
     * Returns a stream of modules upon which the given module `requires transitive`
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   120
     */
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41860
diff changeset
   121
    public Stream<Module> requiresTransitive(Module m) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41860
diff changeset
   122
        // find requires transitive
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   123
        return m.descriptor()
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   124
                .requires().stream()
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41860
diff changeset
   125
                .filter(req -> req.modifiers().contains(TRANSITIVE))
41860
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   126
                .map(ModuleDescriptor.Requires::name)
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   127
                .map(config::findModule)
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   128
                .flatMap(Optional::stream);
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   129
    }
906670ff49c7 8167057: jdeps option to list modules and internal APIs for @modules for test dev
mchung
parents:
diff changeset
   130
}