Path: blob/master/src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ModuleGraphBuilder.java
41171 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tools.jdeps;2627import java.io.PrintWriter;28import java.lang.module.ModuleDescriptor;29import java.util.Deque;30import java.util.HashSet;31import java.util.LinkedList;32import java.util.Map;33import java.util.Optional;34import java.util.Set;35import java.util.stream.Stream;3637import static java.lang.module.ModuleDescriptor.Requires.Modifier.*;38import static com.sun.tools.jdeps.Module.*;3940/**41* A builder to create a Graph<Module>42*/43public class ModuleGraphBuilder extends Graph.Builder<Module> {44final JdepsConfiguration config;4546ModuleGraphBuilder(JdepsConfiguration config) {47this.config = config;48}4950/**51* Adds a module to the graph.52*/53ModuleGraphBuilder addModule(Module module) {54addNode(module);55return this;56}5758/**59* Apply transitive reduction on the resulting graph60*/61public Graph<Module> reduced() {62Graph<Module> graph = build();63// transitive reduction64Graph<Module> newGraph = buildGraph(graph.edges()).reduce();6566if (DEBUG) {67PrintWriter log = new PrintWriter(System.err);68System.err.println("before transitive reduction: ");69graph.printGraph(log);70System.err.println("after transitive reduction: ");71newGraph.printGraph(log);72}7374return newGraph;75}7677public Graph<Module> buildGraph() {78Graph<Module> graph = build();79return buildGraph(graph.edges());80}8182/**83* Build a graph of module from the given dependences.84*85* It transitively includes all implied read edges.86*/87private Graph<Module> buildGraph(Map<Module, Set<Module>> edges) {88Graph.Builder<Module> builder = new Graph.Builder<>();89Set<Module> visited = new HashSet<>();90Deque<Module> deque = new LinkedList<>();91edges.entrySet().stream().forEach(e -> {92Module m = e.getKey();93deque.add(m);94e.getValue().stream().forEach(v -> {95deque.add(v);96builder.addEdge(m, v);97});98});99100// read requires transitive from ModuleDescriptor101Module source;102while ((source = deque.poll()) != null) {103if (visited.contains(source))104continue;105106visited.add(source);107builder.addNode(source);108Module from = source;109requiresTransitive(from).forEach(m -> {110deque.add(m);111builder.addEdge(from, m);112});113}114return builder.build();115}116117/*118* Returns a stream of modules upon which the given module `requires transitive`119*/120public Stream<Module> requiresTransitive(Module m) {121// find requires transitive122return m.descriptor()123.requires().stream()124.filter(req -> req.modifiers().contains(TRANSITIVE))125.map(ModuleDescriptor.Requires::name)126.map(config::findModule)127.flatMap(Optional::stream);128}129}130131132