Skip to content

Commit 7f1c38e

Browse files
joaodinissfclaude
andcommitted
refactor: convert remaining StringBuilder patterns to text blocks and simple concatenation
Replace StringBuilder.append() chains with Java 21 text blocks, String.format(), and simple concatenation across 11 files where the pattern was a mechanical artifact of the Xtend-to-Java migration. Remaining StringBuilder instances (74 across 20 files) are intentionally kept where loops, interleaved conditionals, or dispatch group consistency make them the better choice. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c643402 commit 7f1c38e

11 files changed

Lines changed: 229 additions & 320 deletions

File tree

com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/IssueCodeToLabelMapGenerationTest.java

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,14 @@ public class IssueCodeToLabelMapGenerationTest extends AbstractCheckGenerationTe
4545
@Test
4646
public void testMapGenerationWithNoChecks() {
4747
// ARRANGE
48-
StringBuilder builder = new StringBuilder(512);
49-
builder.append("package ");
50-
builder.append(PACKAGE_NAME);
51-
builder.append('\n');
52-
builder.append('\n');
53-
builder.append("catalog ");
54-
builder.append(CATALOG_NAME);
55-
builder.append('\n');
56-
builder.append("for grammar com.avaloq.tools.ddk.check.Check {\n");
57-
builder.append('\n');
58-
builder.append("}\n");
59-
final String source = builder.toString();
48+
final String source = String.format("""
49+
package %s
50+
51+
catalog %s
52+
for grammar com.avaloq.tools.ddk.check.Check {
53+
54+
}
55+
""", PACKAGE_NAME, CATALOG_NAME);
6056

6157
// check for the construction of an empty map
6258
final List<String> expectedCatalog = List.of("ImmutableMap.<String,String>builderWithExpectedSize(0).build()");
@@ -71,45 +67,37 @@ public void testMapGenerationWithNoChecks() {
7167
@Test
7268
public void testMapGeneration() {
7369
// ARRANGE
74-
// CHECKSTYLE:CHECK-OFF VariableDeclarationUsageDistance
75-
// @Format-Off
76-
StringBuilder builder = new StringBuilder(2048);
77-
builder.append("package ");
78-
builder.append(PACKAGE_NAME);
79-
builder.append('\n');
80-
builder.append('\n');
81-
builder.append("import com.avaloq.tools.ddk.check.check.Check\n");
82-
builder.append("import com.avaloq.tools.ddk.check.check.Context\n");
83-
builder.append("import com.avaloq.tools.ddk.check.check.Documented\n");
84-
builder.append('\n');
85-
builder.append("catalog ");
86-
builder.append(CATALOG_NAME);
87-
builder.append('\n');
88-
builder.append("for grammar com.avaloq.tools.ddk.check.Check {\n");
89-
builder.append('\n');
90-
builder.append(" live error ID1 \"Label 1\"\n");
91-
builder.append(" message \"Message 1\" {\n");
92-
builder.append(" for Documented elem {\n");
93-
builder.append(" switch elem {\n");
94-
builder.append(" Context : issue on elem\n");
95-
builder.append(" Check : issue on elem\n");
96-
builder.append(" }\n");
97-
builder.append(" }\n");
98-
builder.append(" }\n");
99-
builder.append('\n');
100-
builder.append(" live error ID2 \"Label 2\"\n");
101-
builder.append(" message \"Message 2\" {\n");
102-
builder.append(" for Documented elem {\n");
103-
builder.append(" switch elem {\n");
104-
builder.append(" Context : issue on elem\n");
105-
builder.append(" Check : issue on elem\n");
106-
builder.append(" }\n");
107-
builder.append(" }\n");
108-
builder.append(" }\n");
109-
builder.append("}\n");
110-
final String source = builder.toString();
111-
// CHECKSTYLE:CHECK-ON VariableDeclarationUsageDistance
112-
// @Format-On
70+
final String source = String.format("""
71+
package %s
72+
73+
import com.avaloq.tools.ddk.check.check.Check
74+
import com.avaloq.tools.ddk.check.check.Context
75+
import com.avaloq.tools.ddk.check.check.Documented
76+
77+
catalog %s
78+
for grammar com.avaloq.tools.ddk.check.Check {
79+
80+
live error ID1 "Label 1"
81+
message "Message 1" {
82+
for Documented elem {
83+
switch elem {
84+
Context : issue on elem
85+
Check : issue on elem
86+
}
87+
}
88+
}
89+
90+
live error ID2 "Label 2"
91+
message "Message 2" {
92+
for Documented elem {
93+
switch elem {
94+
Context : issue on elem
95+
Check : issue on elem
96+
}
97+
}
98+
}
99+
}
100+
""", PACKAGE_NAME, CATALOG_NAME);
113101

114102
final List<String> expectedCatalog = List.of("put(MyCatalogIssueCodes.ID_1,\"Label1\")", "put(MyCatalogIssueCodes.ID_2,\"Label2\")");
115103

com.avaloq.tools.ddk.check.core.test/src/com/avaloq/tools/ddk/check/core/test/util/CheckModelUtil.java

Lines changed: 26 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ public class CheckModelUtil {
2626
* @return the model stub string
2727
*/
2828
public String modelWithGrammar() {
29-
StringBuilder builder = new StringBuilder(512);
30-
builder.append("package com.test");
31-
builder.append('\n');
32-
builder.append("catalog c for grammar g {");
33-
return builder.toString();
29+
return "package com.test\n" + "catalog c for grammar g {";
3430
}
3531

3632
/**
@@ -39,10 +35,7 @@ public String modelWithGrammar() {
3935
* @return the model stub string
4036
*/
4137
public String modelWithCategory() {
42-
String modelWithGrammar = this.modelWithGrammar();
43-
StringBuilder builder = new StringBuilder(512);
44-
builder.append("category \"Default Category\" {");
45-
return modelWithGrammar + builder.toString();
38+
return this.modelWithGrammar() + "category \"Default Category\" {";
4639
}
4740

4841
/**
@@ -55,14 +48,7 @@ public String modelWithCategory() {
5548
* @return the category string
5649
*/
5750
public String emptyCategory(final String id, final String label) {
58-
StringBuilder builder = new StringBuilder(512);
59-
builder.append("category ");
60-
builder.append(id);
61-
builder.append(" \"");
62-
builder.append(label);
63-
builder.append("\" {\n");
64-
builder.append('}');
65-
return builder.toString();
51+
return String.format("category %s \"%s\" {\n}", id, label);
6652
}
6753

6854
/**
@@ -76,21 +62,8 @@ public String emptyCategory(final String id, final String label) {
7662
* the default severity
7763
* @return the model stub string
7864
*/
79-
// CHECKSTYLE:CHECK-OFF VariableDeclarationUsageDistance
8065
public String modelWithSeverityRange(final String min, final String max, final String severity) {
81-
String modelWithCategory = this.modelWithCategory();
82-
StringBuilder builder = new StringBuilder(512);
83-
builder.append("@SeverityRange(");
84-
builder.append(min);
85-
builder.append(" .. ");
86-
builder.append(max);
87-
builder.append(")\n");
88-
builder.append(" ");
89-
builder.append(severity);
90-
builder.append(" ID \"My Check\" ()\n");
91-
builder.append(" ");
92-
builder.append("message \"My Message\"");
93-
return modelWithCategory + builder.toString();
66+
return this.modelWithCategory() + String.format("@SeverityRange(%s .. %s)\n %s ID \"My Check\" ()\n message \"My Message\"", min, max, severity);
9467
}
9568

9669
/**
@@ -103,14 +76,7 @@ public String modelWithSeverityRange(final String min, final String max, final S
10376
* @return the model stub string
10477
*/
10578
public String modelWithSeverityRange(final String min, final String max) {
106-
String modelWithCategory = this.modelWithCategory();
107-
StringBuilder builder = new StringBuilder(512);
108-
builder.append("@SeverityRange(");
109-
builder.append(min);
110-
builder.append(" .. ");
111-
builder.append(max);
112-
builder.append(")\n");
113-
return modelWithCategory + builder.toString() + modelWithCheck();
79+
return this.modelWithCategory() + String.format("@SeverityRange(%s .. %s)\n", min, max) + modelWithCheck();
11480
}
11581

11682
/**
@@ -121,15 +87,8 @@ public String modelWithSeverityRange(final String min, final String max) {
12187
* @return the model stub string
12288
*/
12389
public String modelWithCheck(final String id) {
124-
String modelWithCategory = this.modelWithCategory();
125-
StringBuilder builder = new StringBuilder(512);
126-
builder.append("error ");
127-
builder.append(id);
128-
builder.append(" \"Some Error\" ()\n");
129-
builder.append("message \"My Message\" {");
130-
return modelWithCategory + builder.toString();
90+
return this.modelWithCategory() + String.format("error %s \"Some Error\" ()\nmessage \"My Message\" {", id);
13191
}
132-
// CHECKSTYLE:CHECK-ON VariableDeclarationUsageDistance
13392

13493
/**
13594
* Returns a base model stub with a check (SomeError) with severity 'error'
@@ -149,13 +108,7 @@ public String modelWithCheck() {
149108
* @return the check string
150109
*/
151110
public String emptyCheck(final String id) {
152-
StringBuilder builder = new StringBuilder(512);
153-
builder.append("error ");
154-
builder.append(id);
155-
builder.append(" \"Some Error\" ()\n");
156-
builder.append("message \"My message\" {\n");
157-
builder.append('}');
158-
return builder.toString();
111+
return String.format("error %s \"Some Error\" ()\nmessage \"My message\" {\n}", id);
159112
}
160113

161114
/**
@@ -165,10 +118,7 @@ public String emptyCheck(final String id) {
165118
* @return the model stub string
166119
*/
167120
public String modelWithContext() {
168-
String modelWithCheck = this.modelWithCheck();
169-
StringBuilder builder = new StringBuilder(512);
170-
builder.append("for ContextType ctx {");
171-
return modelWithCheck + builder.toString();
121+
return this.modelWithCheck() + "for ContextType ctx {";
172122
}
173123

174124
/**
@@ -195,25 +145,24 @@ public String modelWithContexts(final List<String> contexts) {
195145
* @return the model stub string
196146
*/
197147
public String modelWithComments() {
198-
StringBuilder builder = new StringBuilder(512);
199-
builder.append("package com.test // SL1\n");
200-
builder.append("/* ML1 */\n");
201-
builder.append("catalog c /* ML2 */ for grammar g {\n");
202-
builder.append(" // SL2\n");
203-
builder.append(" category \"My cat\" {\n");
204-
builder.append(" /* ML3 */\n");
205-
builder.append(" // SL3\n");
206-
builder.append(" error MYerr \"My Err\" (int Abc = 23) message \"A\" {\n");
207-
builder.append(" for Atype thisName {\n");
208-
builder.append(" val x = 3 // SL4\n");
209-
builder.append(" // SL5\n");
210-
builder.append(" /* ML5 */ issue /* ML4 */\n");
211-
builder.append(" // SL6\n");
212-
builder.append(" }\n");
213-
builder.append(" }\n");
214-
builder.append(" } // SL7\n");
215-
builder.append('}');
216-
return builder.toString();
148+
return """
149+
package com.test // SL1
150+
/* ML1 */
151+
catalog c /* ML2 */ for grammar g {
152+
// SL2
153+
category "My cat" {
154+
/* ML3 */
155+
// SL3
156+
error MYerr "My Err" (int Abc = 23) message "A" {
157+
for Atype thisName {
158+
val x = 3 // SL4
159+
// SL5
160+
/* ML5 */ issue /* ML4 */
161+
// SL6
162+
}
163+
}
164+
} // SL7
165+
}""";
217166
}
218167
}
219168
// CHECKSTYLE:CONSTANTS-ON

com.avaloq.tools.ddk.check.core/src/com/avaloq/tools/ddk/check/generator/CheckGenerator.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,25 @@ public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
8585
/* Documentation compiler, generates HTML output. */
8686
public CharSequence compileDoc(final CheckCatalog catalog) {
8787
final CharSequence body = bodyDoc(catalog);
88-
final StringBuilder sb = new StringBuilder(512);
89-
sb.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n");
90-
sb.append("<html>\n");
91-
sb.append("<head>\n");
92-
sb.append(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
93-
sb.append(" <link rel=\"stylesheet\" href=\"PLUGINS_ROOT/com.avaloq.tools.ddk.check.runtime.ui/css/check.css\" type=\"text/css\">\n");
94-
sb.append(" <title>").append(catalog.getName()).append("</title>\n");
95-
sb.append("</head>\n");
96-
sb.append('\n');
97-
sb.append("<body>\n");
98-
sb.append(" <h1>Check Catalog ").append(catalog.getName()).append("</h1>\n");
9988
final String formattedDescription = generatorExtensions.formatDescription(catalog.getDescription());
100-
if (formattedDescription != null) {
101-
sb.append(" <p>").append(formattedDescription).append("</p>\n");
102-
}
103-
sb.append(" ").append(body).append('\n');
104-
sb.append("</body>\n");
105-
sb.append('\n');
106-
sb.append("</html>\n");
107-
return sb;
89+
final String descriptionHtml = formattedDescription != null ? " <p>" + formattedDescription + "</p>\n" : "";
90+
final String name = catalog.getName();
91+
return String.format("""
92+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
93+
<html>
94+
<head>
95+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
96+
<link rel="stylesheet" href="PLUGINS_ROOT/com.avaloq.tools.ddk.check.runtime.ui/css/check.css" type="text/css">
97+
<title>%1$s</title>
98+
</head>
99+
100+
<body>
101+
<h1>Check Catalog %1$s</h1>
102+
%2$s %3$s
103+
</body>
104+
105+
</html>
106+
""", name, descriptionHtml, body);
108107
}
109108

110109
public CharSequence bodyDoc(final CheckCatalog catalog) {

0 commit comments

Comments
 (0)