jdk/make/src/classes/build/tools/jigsaw/GenGraphs.java
author mchung
Wed, 15 Feb 2017 16:18:22 -0800
changeset 43808 c0a93657773d
parent 43712 5dfd0950317c
child 44417 a431edba1629
permissions -rw-r--r--
8173374: Update GenGraphs tool to generate dot graph with requires transitive edges Reviewed-by: dfuchs, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
43539
b7c4265f40e7 8173604: Rename module 8173604 java.annotations.common to java.xml.ws.annoations
lancea
parents: 42708
diff changeset
     2
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package build.tools.jigsaw;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    28
import com.sun.tools.jdeps.ModuleDotGraph;
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    29
import com.sun.tools.jdeps.ModuleDotGraph.DotGraphBuilder;
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    30
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.io.IOException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.Configuration;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.module.ModuleDescriptor;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.module.ModuleReference;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.nio.file.Files;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
import java.util.HashMap;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
import java.util.HashSet;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import java.util.Map;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import java.util.Set;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
 * Generate the DOT file for a module graph for each module in the JDK
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
 * after transitive reduction.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
public class GenGraphs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
    public static void main(String[] args) throws Exception {
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    51
        Path dir = null;
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    52
        boolean spec = false;
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    53
        for (int i=0; i < args.length; i++) {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    54
            String arg = args[i];
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    55
            if (arg.equals("--spec")) {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    56
                spec = true;
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    57
            } else if (arg.equals("--output")) {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    58
                i++;
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    59
                dir = i < args.length ? Paths.get(args[i]) : null;
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    60
            } else if (arg.startsWith("-")) {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    61
                throw new IllegalArgumentException("Invalid option: " + arg);
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    62
            }
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    63
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    65
        if (dir == null) {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    66
            System.err.println("ERROR: must specify --output argument");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            System.exit(1);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
        }
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    69
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    70
        // setup and configure the dot graph attributes
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    71
        initDotGraphAttributes();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
        Files.createDirectories(dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    74
        GenGraphs genGraphs = new GenGraphs(dir, spec);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    76
        // print dot file for each module
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    77
        Map<String, Configuration> configurations = new HashMap<>();
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    78
        Set<String> modules = new HashSet<>();
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    79
        ModuleFinder finder = ModuleFinder.ofSystem();
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    80
        for (ModuleReference mref : finder.findAll()) {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    81
            String name = (mref.descriptor().name());
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    82
            modules.add(name);
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    83
            if (genGraphs.accept(name, mref.descriptor())) {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    84
                configurations.put(name, Configuration.empty()
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    85
                                                      .resolve(finder,
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    86
                                                               ModuleFinder.of(),
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    87
                                                               Set.of(name)));
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    88
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    91
        if (genGraphs.accept("jdk", null)) {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    92
            // print a graph of all JDK modules
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    93
            configurations.put("jdk", Configuration.empty()
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    94
                                                   .resolve(finder,
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    95
                                                            ModuleFinder.of(),
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    96
                                                            modules));
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    97
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
    99
        genGraphs.genDotFiles(configurations);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   102
    static void initDotGraphAttributes() {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   103
        int h = 1000;
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   104
        DotGraphBuilder.weight("java.se", "java.sql.rowset", h * 10);
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   105
        DotGraphBuilder.weight("java.sql.rowset", "java.sql", h * 10);
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   106
        DotGraphBuilder.weight("java.sql", "java.xml", h * 10);
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   107
        DotGraphBuilder.weight("java.xml", "java.base", h * 10);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   109
        DotGraphBuilder.sameRankNodes(Set.of("java.logging", "java.scripting", "java.xml"));
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   110
        DotGraphBuilder.sameRankNodes(Set.of("java.sql"));
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   111
        DotGraphBuilder.sameRankNodes(Set.of("java.compiler", "java.instrument"));
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   112
        DotGraphBuilder.sameRankNodes(Set.of("java.desktop", "java.management"));
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   113
        DotGraphBuilder.sameRankNodes(Set.of("java.corba", "java.xml.ws"));
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   114
        DotGraphBuilder.sameRankNodes(Set.of("java.xml.bind", "java.xml.ws.annotation"));
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   115
        DotGraphBuilder.setRankSep(0.7);
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   116
        DotGraphBuilder.setFontSize(12);
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   117
        DotGraphBuilder.setArrowSize(1);
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   118
        DotGraphBuilder.setArrowWidth(2);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
42701
274434f1297c 8171323: generate dot file for java.se and java.se.ee with only API dependences
mchung
parents: 42693
diff changeset
   121
    private final Path dir;
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   122
    private final boolean spec;
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   123
    GenGraphs(Path dir, boolean spec) {
42701
274434f1297c 8171323: generate dot file for java.se and java.se.ee with only API dependences
mchung
parents: 42693
diff changeset
   124
        this.dir = dir;
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   125
        this.spec = spec;
42701
274434f1297c 8171323: generate dot file for java.se and java.se.ee with only API dependences
mchung
parents: 42693
diff changeset
   126
    }
274434f1297c 8171323: generate dot file for java.se and java.se.ee with only API dependences
mchung
parents: 42693
diff changeset
   127
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   128
    void genDotFiles(Map<String, Configuration> configurations) throws IOException {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   129
        ModuleDotGraph dotGraph = new ModuleDotGraph(configurations, spec);
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   130
        dotGraph.genDotFiles(dir);
42701
274434f1297c 8171323: generate dot file for java.se and java.se.ee with only API dependences
mchung
parents: 42693
diff changeset
   131
    }
274434f1297c 8171323: generate dot file for java.se and java.se.ee with only API dependences
mchung
parents: 42693
diff changeset
   132
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   133
    boolean accept(String name, ModuleDescriptor descriptor) {
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   134
        if (!spec) return true;
42693
6645de32a866 8171201: Drop java.compact$N aggregator modules
mchung
parents: 42338
diff changeset
   135
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   136
        if (name.equals("jdk"))
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   137
            return false;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   139
        if (name.equals("java.se") || name.equals("java.se.ee"))
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   140
            return true;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   142
        // only the module that has exported API
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   143
        return descriptor.exports().stream()
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   144
                         .filter(e -> !e.isQualified())
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   145
                         .findAny().isPresent();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
    }
43808
c0a93657773d 8173374: Update GenGraphs tool to generate dot graph with requires transitive edges
mchung
parents: 43712
diff changeset
   147
}