< prev index next >

jdk/make/src/classes/build/tools/jigsaw/GenGraphs.java

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package build.tools.jigsaw;
  27 
  28 import com.sun.tools.jdeps.ModuleDotGraph;
  29 import com.sun.tools.jdeps.ModuleDotGraph.DotGraphBuilder;
  30 
  31 import java.io.IOException;
  32 import java.lang.module.Configuration;
  33 import java.lang.module.ModuleDescriptor;
  34 import java.lang.module.ModuleFinder;
  35 import java.lang.module.ModuleReference;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;

  39 import java.util.HashMap;
  40 import java.util.HashSet;

  41 import java.util.Map;

  42 import java.util.Set;


  43 
  44 /**
  45  * Generate the DOT file for a module graph for each module in the JDK
  46  * after transitive reduction.
  47  */
  48 public class GenGraphs {
  49 
  50     public static void main(String[] args) throws Exception {
  51         Path dir = null;
  52         boolean spec = false;

  53         for (int i=0; i < args.length; i++) {
  54             String arg = args[i];
  55             if (arg.equals("--spec")) {
  56                 spec = true;






  57             } else if (arg.equals("--output")) {
  58                 i++;
  59                 dir = i < args.length ? Paths.get(args[i]) : null;
  60             } else if (arg.startsWith("-")) {
  61                 throw new IllegalArgumentException("Invalid option: " + arg);
  62             }
  63         }
  64 
  65         if (dir == null) {
  66             System.err.println("ERROR: must specify --output argument");
  67             System.exit(1);
  68         }
  69 
  70         // setup and configure the dot graph attributes
  71         initDotGraphAttributes();
  72         Files.createDirectories(dir);
  73 
  74         GenGraphs genGraphs = new GenGraphs(dir, spec);





  75 
  76         // print dot file for each module
  77         Map<String, Configuration> configurations = new HashMap<>();
  78         Set<String> modules = new HashSet<>();
  79         ModuleFinder finder = ModuleFinder.ofSystem();
  80         for (ModuleReference mref : finder.findAll()) {
  81             String name = (mref.descriptor().name());
  82             modules.add(name);
  83             if (genGraphs.accept(name, mref.descriptor())) {
  84                 configurations.put(name, Configuration.empty()
  85                                                       .resolve(finder,
  86                                                                ModuleFinder.of(),
  87                                                                Set.of(name)));
  88             }
  89         }
  90 
  91         if (genGraphs.accept("jdk", null)) {
  92             // print a graph of all JDK modules
  93             configurations.put("jdk", Configuration.empty()
  94                                                    .resolve(finder,
  95                                                             ModuleFinder.of(),
  96                                                             modules));
  97         }
  98 
  99         genGraphs.genDotFiles(configurations);
 100     }
 101 
 102     static void initDotGraphAttributes() {





















 103         int h = 1000;
 104         DotGraphBuilder.weight("java.se", "java.sql.rowset", h * 10);
 105         DotGraphBuilder.weight("java.sql.rowset", "java.sql", h * 10);
 106         DotGraphBuilder.weight("java.sql", "java.xml", h * 10);
 107         DotGraphBuilder.weight("java.xml", "java.base", h * 10);
 108 
 109         DotGraphBuilder.sameRankNodes(Set.of("java.logging", "java.scripting", "java.xml"));
 110         DotGraphBuilder.sameRankNodes(Set.of("java.sql"));
 111         DotGraphBuilder.sameRankNodes(Set.of("java.compiler", "java.instrument"));
 112         DotGraphBuilder.sameRankNodes(Set.of("java.desktop", "java.management"));
 113         DotGraphBuilder.sameRankNodes(Set.of("java.corba", "java.xml.ws"));
 114         DotGraphBuilder.sameRankNodes(Set.of("java.xml.bind", "java.xml.ws.annotation"));
 115         DotGraphBuilder.setRankSep(0.7);
 116         DotGraphBuilder.setFontSize(12);
 117         DotGraphBuilder.setArrowSize(1);
 118         DotGraphBuilder.setArrowWidth(2);

















































































 119     }
 120 
 121     private final Path dir;
 122     private final boolean spec;
 123     GenGraphs(Path dir, boolean spec) {

 124         this.dir = dir;
 125         this.spec = spec;

 126     }
 127 
 128     void genDotFiles(Map<String, Configuration> configurations) throws IOException {
 129         ModuleDotGraph dotGraph = new ModuleDotGraph(configurations, spec);
 130         dotGraph.genDotFiles(dir);
 131     }
 132 




 133     boolean accept(String name, ModuleDescriptor descriptor) {
 134         if (!spec) return true;
 135 
 136         if (name.equals("jdk"))
 137             return false;
 138 
 139         if (name.equals("java.se") || name.equals("java.se.ee"))
 140             return true;
 141 
 142         // only the module that has exported API
 143         return descriptor.exports().stream()
 144                          .filter(e -> !e.isQualified())
 145                          .findAny().isPresent();
 146     }
 147 }


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package build.tools.jigsaw;
  27 
  28 import com.sun.tools.jdeps.ModuleDotGraph;

  29 
  30 import java.io.IOException;
  31 import java.lang.module.Configuration;
  32 import java.lang.module.ModuleDescriptor;
  33 import java.lang.module.ModuleFinder;
  34 import java.lang.module.ModuleReference;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.util.ArrayList;
  39 import java.util.HashMap;
  40 import java.util.HashSet;
  41 import java.util.List;
  42 import java.util.Map;
  43 import java.util.Properties;
  44 import java.util.Set;
  45 import java.util.function.Function;
  46 import java.util.stream.Collectors;
  47 
  48 /**
  49  * Generate the DOT file for a module graph for each module in the JDK
  50  * after transitive reduction.
  51  */
  52 public class GenGraphs {
  53 
  54     public static void main(String[] args) throws Exception {
  55         Path dir = null;
  56         boolean spec = false;
  57         Properties props = null;
  58         for (int i=0; i < args.length; i++) {
  59             String arg = args[i];
  60             if (arg.equals("--spec")) {
  61                 spec = true;
  62             } else if (arg.equals("--dot-attributes")) {
  63                 if (i++ == args.length) {
  64                     throw new IllegalArgumentException("Missing argument: --dot-attributes option");
  65                 }
  66                 props = new Properties();
  67                 props.load(Files.newInputStream(Paths.get(args[i])));
  68             } else if (arg.equals("--output")) {
  69                 dir = ++i < args.length ? Paths.get(args[i]) : null;

  70             } else if (arg.startsWith("-")) {
  71                 throw new IllegalArgumentException("Invalid option: " + arg);
  72             }
  73         }
  74 
  75         if (dir == null) {
  76             System.err.println("ERROR: must specify --output argument");
  77             System.exit(1);
  78         }
  79 


  80         Files.createDirectories(dir);
  81         ModuleGraphAttributes attributes;
  82         if (props != null) {
  83             attributes = new ModuleGraphAttributes(props);
  84         } else {
  85             attributes = new ModuleGraphAttributes();
  86         }
  87         GenGraphs genGraphs = new GenGraphs(dir, spec, attributes);
  88 
  89         // print dot file for each module
  90         Map<String, Configuration> configurations = new HashMap<>();
  91         Set<String> modules = new HashSet<>();
  92         ModuleFinder finder = ModuleFinder.ofSystem();
  93         for (ModuleReference mref : finder.findAll()) {
  94             String name = (mref.descriptor().name());
  95             modules.add(name);
  96             if (genGraphs.accept(name, mref.descriptor())) {
  97                 configurations.put(name, Configuration.empty()
  98                                                       .resolve(finder,
  99                                                                ModuleFinder.of(),
 100                                                                Set.of(name)));
 101             }
 102         }
 103 
 104         if (genGraphs.accept("jdk", null)) {
 105             // print a graph of all JDK modules
 106             configurations.put("jdk", Configuration.empty()
 107                                                    .resolve(finder,
 108                                                             ModuleFinder.of(),
 109                                                             modules));
 110         }
 111 
 112         genGraphs.genDotFiles(configurations);
 113     }
 114 
 115     /**
 116      * Custom dot file attributes.
 117      */
 118     static class ModuleGraphAttributes implements ModuleDotGraph.Attributes {
 119         static Map<String, String> DEFAULT_ATTRIBUTES = Map.of(
 120             "ranksep", "0.6",
 121             "fontsize", "12",
 122             "fontcolor", BLACK,
 123             "fontname", "DejaVuSans bold",
 124             "arrowsize", "1",
 125             "arrowwidth", "2",
 126             "arrowcolor", DARK_GRAY,
 127             // custom
 128             "requiresMandatedColor", LIGHT_GRAY,
 129             "javaSubgraphColor", ORANGE,
 130             "jdkSubgraphColor", BLUE
 131         );
 132 
 133         final Map<String, Integer> weights = new HashMap<>();
 134         final List<Set<String>> ranks = new ArrayList<>();
 135         final Map<String, String> attrs;
 136         ModuleGraphAttributes(Map<String, String> attrs) {
 137             int h = 1000;
 138             weight("java.se", "java.sql.rowset", h * 10);
 139             weight("java.sql.rowset", "java.sql", h * 10);
 140             weight("java.sql", "java.xml", h * 10);
 141             weight("java.xml", "java.base", h * 10);
 142 
 143             ranks.add(Set.of("java.logging", "java.scripting", "java.xml"));
 144             ranks.add(Set.of("java.sql"));
 145             ranks.add(Set.of("java.compiler", "java.instrument"));
 146             ranks.add(Set.of("java.desktop", "java.management"));
 147             ranks.add(Set.of("java.corba", "java.xml.ws"));
 148             ranks.add(Set.of("java.xml.bind", "java.xml.ws.annotation"));
 149 
 150             this.attrs = attrs;
 151         }
 152 
 153         ModuleGraphAttributes() {
 154             this(DEFAULT_ATTRIBUTES);
 155         }
 156         ModuleGraphAttributes(Properties props) {
 157             this(toAttributes(props));
 158         }
 159 
 160         @Override
 161         public double rankSep() {
 162             return Double.valueOf(attrs.get("ranksep"));
 163         }
 164 
 165         @Override
 166         public int fontSize() {
 167             return Integer.valueOf(attrs.get("fontsize"));
 168         }
 169 
 170         @Override
 171         public String fontName() {
 172             return attrs.get("fontname");
 173         }
 174 
 175         @Override
 176         public String fontColor() {
 177             return attrs.get("fontcolor");
 178         }
 179 
 180         @Override
 181         public int arrowSize() {
 182             return Integer.valueOf(attrs.get("arrowsize"));
 183         }
 184 
 185         @Override
 186         public int arrowWidth() {
 187             return Integer.valueOf(attrs.get("arrowwidth"));
 188         }
 189 
 190         @Override
 191         public String arrowColor() {
 192             return attrs.get("arrowcolor");
 193         }
 194 
 195         @Override
 196         public List<Set<String>> ranks() {
 197             return ranks;
 198         }
 199 
 200         @Override
 201         public String requiresMandatedColor() {
 202             return attrs.get("requiresMandatedColor");
 203         }
 204 
 205         @Override
 206         public String javaSubgraphColor() {
 207             return attrs.get("javaSubgraphColor");
 208         }
 209 
 210         @Override
 211         public String jdkSubgraphColor() {
 212             return attrs.get("jdkSubgraphColor");
 213         }
 214 
 215         @Override
 216         public int weightOf(String s, String t) {
 217             int w = weights.getOrDefault(s + ":" + t, 1);
 218             if (w != 1)
 219                 return w;
 220             if (s.startsWith("java.") && t.startsWith("java."))
 221                 return 10;
 222             return 1;
 223         }
 224 
 225         public void weight(String s, String t, int w) {
 226             weights.put(s + ":" + t, w);
 227         }
 228 
 229         static Map<String, String> toAttributes(Properties props) {
 230             return DEFAULT_ATTRIBUTES.keySet().stream()
 231                 .collect(Collectors.toMap(Function.identity(),
 232                     k -> props.getProperty(k, DEFAULT_ATTRIBUTES.get(k))));
 233         }
 234     }
 235 
 236     private final Path dir;
 237     private final boolean spec;
 238     private final ModuleGraphAttributes attributes;
 239     GenGraphs(Path dir, boolean spec, ModuleGraphAttributes attributes) {
 240         this.dir = dir;
 241         this.spec = spec;
 242         this.attributes = attributes;
 243     }
 244 
 245     void genDotFiles(Map<String, Configuration> configurations) throws IOException {
 246         ModuleDotGraph dotGraph = new ModuleDotGraph(configurations, spec);
 247         dotGraph.genDotFiles(dir, attributes);
 248     }
 249 
 250     /**
 251      * Returns true for any name if generating graph for non-spec;
 252      * otherwise, returns true except "jdk" and name with "jdk.internal." prefix
 253      */
 254     boolean accept(String name, ModuleDescriptor descriptor) {
 255         if (!spec)





 256             return true;
 257 
 258         return !name.equals("jdk") && !name.startsWith("jdk.internal.");



 259     }
 260 }
< prev index next >