Skip to content

Commit daaa47e

Browse files
author
Mandy Chung
committed
8274311: Make build.tools.jigsaw.GenGraphs more configurable
Reviewed-by: alanb, iris
1 parent 2cffe4c commit daaa47e

File tree

3 files changed

+134
-67
lines changed

3 files changed

+134
-67
lines changed

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

Lines changed: 77 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.nio.file.Path;
3737
import java.nio.file.Paths;
3838
import java.util.ArrayList;
39+
import java.util.Arrays;
3940
import java.util.HashMap;
4041
import java.util.HashSet;
4142
import java.util.List;
@@ -115,102 +116,112 @@ public static void main(String[] args) throws Exception {
115116
/**
116117
* Custom dot file attributes.
117118
*/
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",
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.transaction.xa"));
146-
ranks.add(Set.of("java.compiler", "java.instrument"));
147-
ranks.add(Set.of("java.desktop", "java.management"));
148-
149-
this.attrs = attrs;
150-
}
119+
static class ModuleGraphAttributes extends ModuleDotGraph.DotGraphAttributes {
120+
final Properties attrs;
121+
final Map<String, Integer> weights;
151122

152123
ModuleGraphAttributes() {
153-
this(DEFAULT_ATTRIBUTES);
154-
}
124+
this(new Properties());
125+
};
155126
ModuleGraphAttributes(Properties props) {
156-
this(toAttributes(props));
127+
this.attrs = props;
128+
this.weights = initWeights(props);
129+
}
130+
131+
@Override
132+
public double nodeSep() {
133+
String v = attrs.getProperty("nodesep");
134+
return v != null ? Double.valueOf(v) : super.nodeSep();
157135
}
158136

159137
@Override
160138
public double rankSep() {
161-
return Double.valueOf(attrs.get("ranksep"));
139+
String v = attrs.getProperty("ranksep");
140+
return v != null ? Double.valueOf(v) : super.rankSep();
162141
}
163142

164143
@Override
165144
public int fontSize() {
166-
return Integer.valueOf(attrs.get("fontsize"));
145+
String v = attrs.getProperty("fontsize");
146+
return v != null ? Integer.valueOf(v) : super.fontSize();
167147
}
168148

169149
@Override
170150
public String fontName() {
171-
return attrs.get("fontname");
151+
String v = attrs.getProperty("fontname");
152+
return v != null ? v : super.fontName();
172153
}
173154

174155
@Override
175156
public String fontColor() {
176-
return attrs.get("fontcolor");
157+
String v = attrs.getProperty("fontcolor");
158+
return v != null ? v : super.fontColor();
177159
}
178160

179161
@Override
180162
public int arrowSize() {
181-
return Integer.valueOf(attrs.get("arrowsize"));
163+
String v = attrs.getProperty("arrowsize");
164+
return v != null ? Integer.valueOf(v) : super.arrowSize();
182165
}
183166

184167
@Override
185168
public int arrowWidth() {
186-
return Integer.valueOf(attrs.get("arrowwidth"));
169+
String v = attrs.getProperty("arrowwidth");
170+
return v != null ? Integer.valueOf(v) : super.arrowWidth();
187171
}
188172

189173
@Override
190174
public String arrowColor() {
191-
return attrs.get("arrowcolor");
175+
String v = attrs.getProperty("arrowcolor");
176+
return v != null ? v : super.arrowColor();
192177
}
193178

194179
@Override
195180
public List<Set<String>> ranks() {
196-
return ranks;
181+
return attrs.stringPropertyNames().stream()
182+
.filter(k -> k.startsWith("ranks."))
183+
.sorted()
184+
.map(k -> Arrays.stream(attrs.getProperty(k).split(","))
185+
.collect(Collectors.toSet()))
186+
.toList();
197187
}
198188

199189
@Override
200190
public String requiresMandatedColor() {
201-
return attrs.get("requiresMandatedColor");
191+
String v = attrs.getProperty("requiresMandatedColor");
192+
return v != null ? v : super.requiresMandatedColor();
202193
}
203194

204195
@Override
205196
public String javaSubgraphColor() {
206-
return attrs.get("javaSubgraphColor");
197+
String v = attrs.getProperty("javaSubgraphColor");
198+
return v != null ? v : super.javaSubgraphColor();
207199
}
208200

209201
@Override
210202
public String jdkSubgraphColor() {
211-
return attrs.get("jdkSubgraphColor");
203+
String v = attrs.getProperty("jdkSubgraphColor");
204+
return v != null ? v : super.jdkSubgraphColor();
205+
}
206+
207+
@Override
208+
public String nodeMargin() {
209+
String v = attrs.getProperty("node-margin");
210+
return v != null ? v : super.nodeMargin();
212211
}
213212

213+
@Override
214+
public String requiresStyle() {
215+
String v = attrs.getProperty("requiresStyle");
216+
return v != null ? v : super.requiresStyle();
217+
};
218+
219+
@Override
220+
public String requiresTransitiveStyle() {
221+
String v = attrs.getProperty("requiresTransitiveStyle");
222+
return v != null ? v : super.requiresTransitiveStyle();
223+
};
224+
214225
@Override
215226
public int weightOf(String s, String t) {
216227
int w = weights.getOrDefault(s + ":" + t, 1);
@@ -221,14 +232,25 @@ public int weightOf(String s, String t) {
221232
return 1;
222233
}
223234

224-
public void weight(String s, String t, int w) {
225-
weights.put(s + ":" + t, w);
226-
}
227-
228-
static Map<String, String> toAttributes(Properties props) {
229-
return DEFAULT_ATTRIBUTES.keySet().stream()
230-
.collect(Collectors.toMap(Function.identity(),
231-
k -> props.getProperty(k, DEFAULT_ATTRIBUTES.get(k))));
235+
/*
236+
* Create a map of <mn>:<dep> with a weight trying to line up
237+
* the modules in the weights property in the specified order.
238+
*/
239+
public static Map<String, Integer> initWeights(Properties props) {
240+
String[] modules = props.getProperty("weights", "").split(",");
241+
int len = modules.length;
242+
if (len == 0) return Map.of();
243+
244+
Map<String, Integer> weights = new HashMap<>();
245+
String mn = modules[0];
246+
int w = 10000;
247+
for (int i = 1; i < len; i++) {
248+
String dep = modules[i];
249+
weights.put(mn + ":" + dep, w);
250+
mn = dep;
251+
}
252+
weights.put(mn + ":java.base", w);
253+
return weights;
232254
}
233255
}
234256

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,35 @@
1+
# Configuration file for build.tools.jigsaw.GenGraphs
2+
3+
nodesep=.5
4+
node-margin=.2,.2
5+
ranksep=0.6
6+
fontsize=12
7+
fontcolor=#000000
8+
fontname=DejaVuSans
9+
arrowsize=1
10+
arrowwidth=2
11+
12+
# requires edge: gray
113
arrowcolor=#999999
2-
requiresMandatedColor=#999999
14+
15+
# requires mandated java.base edge: light gray
16+
requiresMandatedColor=#dddddd
17+
18+
requiresTransitiveStyle=
19+
requiresStyle=dashed
20+
21+
# java.* modules: orange
22+
javaSubgraphColor=#e76f00
23+
24+
# jdk.* modules: blue
25+
jdkSubgraphColor=#437291
26+
27+
# configure the group of modules in the same rank
28+
ranks.1=java.logging,java.scripting,java.xml
29+
ranks.2=java.sql
30+
ranks.4=java.compiler,java.instrument
31+
ranks.5=java.desktop,java.management
32+
33+
# configure the edges A -> B -> C .... with the same weight
34+
# that should get these modules lined in a straight line
35+
weights=java.se,java.sql.rowset,java.sql,java.xml

src/jdk.jdeps/share/classes/com/sun/tools/jdeps/ModuleDotGraph.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public interface Attributes {
197197
static final String ORANGE = "#e76f00";
198198
static final String BLUE = "#437291";
199199
static final String BLACK = "#000000";
200-
static final String DARK_GRAY = "#999999";
200+
static final String DARK_GRAY = "#a9a9a9";
201201
static final String LIGHT_GRAY = "#dddddd";
202202

203203
int fontSize();
@@ -208,8 +208,12 @@ public interface Attributes {
208208
int arrowWidth();
209209
String arrowColor();
210210

211+
default double nodeSep() {
212+
return 0.5;
213+
}
214+
211215
default double rankSep() {
212-
return 1;
216+
return 0.6;
213217
}
214218

215219
default List<Set<String>> ranks() {
@@ -231,9 +235,15 @@ default String javaSubgraphColor() {
231235
default String jdkSubgraphColor() {
232236
return BLUE;
233237
}
238+
239+
default String nodeMargin() { return ".2, .2"; }
240+
241+
default String requiresStyle() { return "dashed"; };
242+
243+
default String requiresTransitiveStyle() { return ""; };
234244
}
235245

236-
static class DotGraphAttributes implements Attributes {
246+
public static class DotGraphAttributes implements Attributes {
237247
static final DotGraphAttributes DEFAULT = new DotGraphAttributes();
238248

239249
static final String FONT_NAME = "DejaVuSans";
@@ -273,9 +283,6 @@ public String arrowColor() {
273283
}
274284

275285
private static class DotGraphBuilder {
276-
static final String REEXPORTS = "";
277-
static final String REQUIRES = "style=\"dashed\"";
278-
279286
static final Set<String> JAVA_SE_SUBGRAPH = javaSE();
280287
static final Set<String> JDK_SUBGRAPH = jdk();
281288

@@ -347,14 +354,15 @@ public void build(Path filename) throws IOException {
347354
PrintWriter out = new PrintWriter(writer)) {
348355

349356
out.format("digraph \"%s\" {%n", name);
350-
out.format(" nodesep=.5;%n");
357+
out.format(" nodesep=%f;%n", attributes.nodeSep());
351358
out.format((Locale)null, " ranksep=%f;%n", attributes.rankSep());
352359
out.format(" pencolor=transparent;%n");
353360
out.format(" node [shape=plaintext, fontcolor=\"%s\", fontname=\"%s\","
354-
+ " fontsize=%d, margin=\".2,.2\"];%n",
361+
+ " fontsize=%d, margin=\"%s\"];%n",
355362
attributes.fontColor(),
356363
attributes.fontName(),
357-
attributes.fontSize());
364+
attributes.fontSize(),
365+
attributes.nodeMargin());
358366
out.format(" edge [penwidth=%d, color=\"%s\", arrowhead=open, arrowsize=%d];%n",
359367
attributes.arrowWidth(),
360368
attributes.arrowColor(),
@@ -407,11 +415,15 @@ public void printNode(PrintWriter out, ModuleDescriptor md, Set<String> edges) {
407415

408416
String mn = md.name();
409417
edges.forEach(dn -> {
410-
String attr;
418+
String attr = "";
411419
if (dn.equals("java.base")) {
412420
attr = "color=\"" + attributes.requiresMandatedColor() + "\"";
413421
} else {
414-
attr = (requiresTransitive.contains(dn) ? REEXPORTS : REQUIRES);
422+
String style = requiresTransitive.contains(dn) ? attributes.requiresTransitiveStyle()
423+
: attributes.requiresStyle();
424+
if (!style.isEmpty()) {
425+
attr = "style=\"" + style + "\"";
426+
}
415427
}
416428

417429
int w = attributes.weightOf(mn, dn);

0 commit comments

Comments
 (0)