Skip to content

Commit 0834dac

Browse files
Fix SpotBugs SBSC_USE_STRINGBUFFER_CONCATENATION warnings
Refactored code to use StringBuilder in loops to prevent string concatenation performance issues. Updated .github/scripts/generate-quality-report.py to treat SBSC_USE_STRINGBUFFER_CONCATENATION as a build failure.
1 parent 9744b06 commit 0834dac

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

CodenameOne/src/com/codename1/ui/html/HTMLComponent.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,17 +2223,17 @@ private Label addString(String str, int align) {
22232223
int spacesToAdd = (width - lbl.getPreferredW()) / spaceW;
22242224
int spacesPerWord = spacesToAdd / (words.size() - 1);
22252225
int addtlSpaces = spacesToAdd % (words.size() - 1);
2226-
String newStr = (String) words.elementAt(0);
2226+
StringBuilder newStr = new StringBuilder((String) words.elementAt(0));
22272227
for (int i = 1; i < words.size(); i++) {
22282228
for (int j = 0; j < spacesPerWord; j++) {
2229-
newStr += ' ';
2229+
newStr.append(' ');
22302230
}
22312231
if (i - 1 < addtlSpaces) {
2232-
newStr += ' ';
2232+
newStr.append(' ');
22332233
}
2234-
newStr += ' ' + (String) words.elementAt(i);
2234+
newStr.append(' ').append((String) words.elementAt(i));
22352235
}
2236-
lbl.setText(newStr);
2236+
lbl.setText(newStr.toString());
22372237
}
22382238
} else {
22392239
lbl.setPreferredW(width);

CodenameOne/src/com/codename1/ui/html/HTMLElement.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -971,11 +971,11 @@ static int getColor(String colorStr, int defaultColor) {
971971
}
972972

973973
if (colorStr.length() == 3) { // shortened format rgb - translated to rrggbb
974-
String newColStr = "";
974+
StringBuilder newColStr = new StringBuilder();
975975
for (int i = 0; i < 3; i++) {
976-
newColStr += colorStr.charAt(i) + "" + colorStr.charAt(i);
976+
newColStr.append(colorStr.charAt(i)).append("").append(colorStr.charAt(i));
977977
}
978-
colorStr = newColStr;
978+
colorStr = newColStr.toString();
979979
}
980980

981981
try {
@@ -1159,23 +1159,23 @@ public String getSupportedAttributesList() {
11591159
if ((id < 0) || (id >= TAG_ATTRIBUTES.length)) {
11601160
return "Unknown";
11611161
}
1162-
String list = "";
1162+
StringBuilder list = new StringBuilder();
11631163
for (int a = 0; a < TAG_ATTRIBUTES[id].length; a++) {
1164-
list += ATTRIBUTE_NAMES[TAG_ATTRIBUTES[id][a]] + ",";
1164+
list.append(ATTRIBUTE_NAMES[TAG_ATTRIBUTES[id][a]]).append(",");
11651165
}
11661166
if (supportsCoreAttributes()) {
11671167
for (int a = 0; a < COMMON_ATTRIBUTES.length; a++) {
1168-
list += ATTRIBUTE_NAMES[COMMON_ATTRIBUTES[a]] + ",";
1168+
list.append(ATTRIBUTE_NAMES[COMMON_ATTRIBUTES[a]]).append(",");
11691169
}
11701170
}
11711171

1172-
if (list.endsWith(",")) {
1173-
list = list.substring(0, list.length() - 1);
1172+
if (list.length() > 0 && list.charAt(list.length() - 1) == ',') {
1173+
list.setLength(list.length() - 1);
11741174
}
1175-
if (list.equals("")) {
1176-
list = "None";
1175+
if (list.length() == 0) {
1176+
return "None";
11771177
}
1178-
return list;
1178+
return list.toString();
11791179
}
11801180

11811181
/**

0 commit comments

Comments
 (0)