Skip to content

Commit dabe805

Browse files
Merge pull request #225 from refactorfirst/optimize-class-dot
Optimize generated class DOT
2 parents 4ca6a4f + 69a923d commit dabe805

6 files changed

Lines changed: 755 additions & 732 deletions

File tree

‎.refactorfirst/refactor-first.json‎

Lines changed: 720 additions & 720 deletions
Large diffs are not rendered by default.

‎report/src/main/java/org/hjug/refactorfirst/report/HtmlReport.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ String buildClassGraphDot(
594594
return toJavaScriptTemplateLiteral(buildRawClassGraphDot(classGraph, repoUrl, codebaseGraphDTO));
595595
}
596596

597+
/**
598+
* Appends class DOT nodes, omitting sink-only anonymous or synthetic vertices and labels that
599+
* match their node IDs. Includes source-link attributes supplied by the report and highlights
600+
* classes selected for removal.
601+
*/
597602
private void renderClassVertices(
598603
Graph<String, DefaultWeightedEdge> classGraph,
599604
String repoUrl,
@@ -609,19 +614,23 @@ private void renderClassVertices(
609614
continue;
610615
}
611616

612-
dot.append(renderSafeNodeId(vertex, classGraph, codebaseGraphDTO));
617+
String safeNodeId = renderSafeNodeId(vertex, classGraph, codebaseGraphDTO);
618+
dot.append(safeNodeId);
613619

614620
dot.append(" [");
615621
dot.append(hyperlinkClassForDot(vertex, repoUrl, codebaseGraphDTO));
616622

623+
String label;
617624
if (isAnonymousFqn(vertex)) {
618625
// Kotlin "<anonymous>" renders under the enclosing source file's base name as the
619626
// owner with $ as the enclosing-class separator (escaped for DOT).
620-
dot.append(" label=\"")
621-
.append(escapeDotQuoted(anonymousOwnerLabel(vertex, codebaseGraphDTO)))
622-
.append("\"");
627+
label = escapeDotQuoted(anonymousOwnerLabel(vertex, codebaseGraphDTO));
623628
} else {
624-
dot.append(" label=\"").append(escapeDotQuoted(className)).append("\"");
629+
label = escapeDotQuoted(className);
630+
}
631+
632+
if (!label.equals(safeNodeId)) {
633+
dot.append(" label=\"").append(label).append("\"");
625634
}
626635

627636
if (classesToRemove.contains(vertex)) {

‎report/src/main/java/org/hjug/refactorfirst/report/JsonGenerator.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ public void execute(
8080
// copyViewerResources(dotRefactorFirstDir);
8181
}
8282

83+
/**
84+
* Intentionally excluding hyperlinks in DOT to reduce JSON file size
85+
*
86+
* @param fqClassName The fully qualified class name
87+
* @param repoUrl The repo URL
88+
* @param codebaseGraphDTO The DTO containing the codebase graph
89+
*/
90+
String hyperlinkClassForDot(String fqClassName, String repoUrl, CodebaseGraphDTO codebaseGraphDTO) {
91+
return "";
92+
}
93+
8394
/** Copies the Mustache template and browser viewer into the report directory. */
8495
private void copyViewerResources(Path targetDir) {
8596
try {

‎report/src/test/java/org/hjug/refactorfirst/report/HtmlReportKotlinTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
*/
5454
class HtmlReportKotlinTest {
5555

56+
/** Verifies cycle DOT preserves Kotlin source links and edge weights while omitting redundant labels. */
5657
@DisplayName("HtmlReport.buildClassCycleDot emits .kt URLs from Kotlin source-path mapping")
5758
@Test
5859
void buildClassCycleDot_kotlinSourcePaths() {
@@ -95,9 +96,9 @@ void buildClassCycleDot_kotlinSourcePaths() {
9596
KotlinCycleA -\\u003E KotlinCycleB [ label = "2" weight = "2" ];
9697
KotlinCycleB -\\u003E KotlinCycleC [ label = "1" weight = "1" ];
9798
KotlinCycleC -\\u003E KotlinCycleA [ label = "1" weight = "1" ];
98-
KotlinCycleA [URL="https://github.com/refactorfirst/RefactorFirst/blob/src/main/kotlin/com/kotlin/cycles/KotlinCycleA.kt" target="_blank" label="KotlinCycleA"];
99-
KotlinCycleB [URL="https://github.com/refactorfirst/RefactorFirst/blob/src/main/kotlin/com/kotlin/cycles/KotlinCycleB.kt" target="_blank" label="KotlinCycleB"];
100-
KotlinCycleC [URL="https://github.com/refactorfirst/RefactorFirst/blob/src/main/kotlin/com/kotlin/cycles/KotlinCycleC.kt" target="_blank" label="KotlinCycleC"];
99+
KotlinCycleA [URL="https://github.com/refactorfirst/RefactorFirst/blob/src/main/kotlin/com/kotlin/cycles/KotlinCycleA.kt" target="_blank"];
100+
KotlinCycleB [URL="https://github.com/refactorfirst/RefactorFirst/blob/src/main/kotlin/com/kotlin/cycles/KotlinCycleB.kt" target="_blank"];
101+
KotlinCycleC [URL="https://github.com/refactorfirst/RefactorFirst/blob/src/main/kotlin/com/kotlin/cycles/KotlinCycleC.kt" target="_blank"];
101102
}`;\
102103
""";
103104

‎report/src/test/java/org/hjug/refactorfirst/report/HtmlReportTest.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ void getDescription() {
3838
mavenReport.getDescription(Locale.getDefault()));
3939
}
4040

41+
/** Verifies cycle DOT preserves Java source links and edge weights while omitting redundant labels. */
4142
@Test
4243
void buildClassCycleDot() {
4344
Graph<String, DefaultWeightedEdge> classGraph = new DefaultDirectedWeightedGraph<>(DefaultWeightedEdge.class);
@@ -66,9 +67,9 @@ void buildClassCycleDot() {
6667
+ "A -\\u003E B [ label = \"2\" weight = \"2\" ];\n"
6768
+ "B -\\u003E C [ label = \"1\" weight = \"1\" ];\n"
6869
+ "C -\\u003E A [ label = \"1\" weight = \"1\" ];\n"
69-
+ "A [URL=\"https://github.com/refactorfirst/RefactorFirst/blob/src/main/java/org/hjug/refactorfirst/A.java\" target=\"_blank\" label=\"A\"];\n"
70-
+ "B [URL=\"https://github.com/refactorfirst/RefactorFirst/blob/src/main/java/org/hjug/refactorfirst/B.java\" target=\"_blank\" label=\"B\"];\n"
71-
+ "C [URL=\"https://github.com/refactorfirst/RefactorFirst/blob/src/main/java/org/hjug/refactorfirst/C.java\" target=\"_blank\" label=\"C\"];\n"
70+
+ "A [URL=\"https://github.com/refactorfirst/RefactorFirst/blob/src/main/java/org/hjug/refactorfirst/A.java\" target=\"_blank\"];\n"
71+
+ "B [URL=\"https://github.com/refactorfirst/RefactorFirst/blob/src/main/java/org/hjug/refactorfirst/B.java\" target=\"_blank\"];\n"
72+
+ "C [URL=\"https://github.com/refactorfirst/RefactorFirst/blob/src/main/java/org/hjug/refactorfirst/C.java\" target=\"_blank\"];\n"
7273
+ "}`;";
7374

7475
assertEquals(expectedDot, dot);

‎report/src/test/java/org/hjug/refactorfirst/report/JsonGeneratorTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ void given_repoWithOrigin_when_reportGenerated_then_bubbleUrlsPointToSourceFiles
166166
complexMethod
167167
.append(" if (a > ")
168168
.append(i)
169-
.append(") { for (int j = 0; j < a; j++) { if (j % 2 == 0 && b) { c += j; } else { c -= j; } } }\n");
169+
.append(
170+
") { for (int j = 0; j < a; j++) { if (j % 2 == 0 && b) { c += j; } else { c -= j; } } }\n");
170171
}
171172
Files.writeString(
172173
new File(srcDir, "ComplexService.java").toPath(),

0 commit comments

Comments
 (0)