Skip to content

Commit 933c3c1

Browse files
Fix SpotBugs SBSC_USE_STRINGBUFFER_CONCATENATION warnings
Replaced string concatenation in loops with StringBuilder in multiple files to improve performance and satisfy SpotBugs checks. Updated .github/scripts/generate-quality-report.py to fail on this violation. Fixed compilation errors in ComponentSelector and InnerActive by calling toString() before indexOf/substring on StringBuilder, as CN1's StringBuilder lacks these methods.
1 parent 65d3796 commit 933c3c1

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

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

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,13 +1856,14 @@ private void checkRedirect(HTMLElement head) {
18561856
if (seperator != -1) {
18571857
String tempUrl = content.substring(seperator + 1);
18581858

1859-
redirectURL = "";
1859+
StringBuilder redirectURLBuilder = new StringBuilder();
18601860
for (int i = 0; i < tempUrl.length(); i++) {
18611861
char ch = tempUrl.charAt(i);
18621862
if (!CSSParser.isWhiteSpace(ch)) {
1863-
redirectURL += ch;
1863+
redirectURLBuilder.append(ch);
18641864
}
18651865
}
1866+
redirectURL = redirectURLBuilder.toString();
18661867

18671868
if (redirectURL.startsWith("url=")) { // i.e. 10;url=http://....
18681869
redirectURL = redirectURL.substring(4);
@@ -1986,22 +1987,22 @@ Vector showPreTagText(String text, int align) {
19861987
return comps; //no text to show
19871988
}
19881989

1989-
String line = "";
1990+
StringBuilder line = new StringBuilder();
19901991
for (int c = 0; c < text.length(); c++) {
19911992
char ch = text.charAt(c);
19921993
if ((ch == 10) || (ch == 13)) {
1993-
if (!line.equals("")) {
1994-
comps.addElement(addString(line, align));
1994+
if (line.length() > 0) {
1995+
comps.addElement(addString(line.toString(), align));
19951996
newLine(align);
1996-
line = "";
1997+
line.setLength(0);
19971998
}
19981999
} else {
1999-
line += ch;
2000+
line.append(ch);
20002001
}
20012002

20022003
}
2003-
if (!line.equals("")) {
2004-
comps.addElement(addString(line, align));
2004+
if (line.length() > 0) {
2005+
comps.addElement(addString(line.toString(), align));
20052006
newLine(align);
20062007
}
20072008
return comps;
@@ -2086,32 +2087,33 @@ private Vector showTextFixedWidth(String text, int align) {
20862087

20872088
if (words.size() > 0) {
20882089
int w = 0;
2089-
String wordStr = "";
2090+
StringBuilder wordStr = new StringBuilder();
20902091
if ((CSSParser.isWhiteSpace(text.charAt(0))) && (curLine.getComponentCount() != 0)) { //leading space is trimmed if it is in the first component of the line
2091-
wordStr = " "; //leading space
2092+
wordStr.append(" "); //leading space
20922093
}
20932094

20942095
while (w < words.size()) {
20952096
String nextWord = (String) words.elementAt(w);
20962097
String space = "";
2097-
if ((!wordStr.equals("")) && (!wordStr.equals(" "))) {
2098+
if ((wordStr.length() > 0) && (!wordStr.toString().equals(" "))) {
20982099
space = " ";
20992100
}
2100-
if (font.stringWidth(wordStr + space + nextWord) > spaceW - 2) {
2101-
comps.addElement(addString(wordStr, align));
2101+
if (font.stringWidth(wordStr.toString() + space + nextWord) > spaceW - 2) {
2102+
comps.addElement(addString(wordStr.toString(), align));
21022103
newLineIfNotEmpty(align);
21032104
spaceW = width - x;
2104-
wordStr = nextWord;
2105+
wordStr.setLength(0);
2106+
wordStr.append(nextWord);
21052107
} else {
2106-
wordStr += space + nextWord;
2108+
wordStr.append(space).append(nextWord);
21072109
}
21082110
w++;
21092111
}
21102112
if (CSSParser.isWhiteSpace(text.charAt(text.length() - 1))) {
2111-
wordStr += " "; //trailing space
2113+
wordStr.append(" "); //trailing space
21122114
}
21132115

2114-
comps.addElement(addString(wordStr, align));
2116+
comps.addElement(addString(wordStr.toString(), align));
21152117
}
21162118

21172119
return comps;
@@ -2459,23 +2461,23 @@ private void handleImageMapArea(HTMLElement areaTag) {
24592461
supportedShape = true;
24602462
String coordsStr = areaTag.getAttributeById(HTMLElement.ATTR_COORDS);
24612463
if ((coordsStr != null) && (hrefStr != null)) {
2462-
String curValStr = "";
2464+
StringBuilder curValStr = new StringBuilder();
24632465
int[] coords = new int[4];
24642466
int curCoord = 0;
24652467
boolean error = true;
24662468
try {
24672469
for (int c = 0; c < coordsStr.length(); c++) {
24682470
char ch = coordsStr.charAt(c);
24692471
if (ch != ',') {
2470-
curValStr += ch;
2472+
curValStr.append(ch);
24712473
} else {
2472-
coords[curCoord] = Integer.parseInt(curValStr);
2474+
coords[curCoord] = Integer.parseInt(curValStr.toString());
24732475
curCoord++;
2474-
curValStr = "";
2476+
curValStr.setLength(0);
24752477
}
24762478
}
24772479
if (curValStr.length() > 0) {
2478-
coords[curCoord] = Integer.parseInt(curValStr);
2480+
coords[curCoord] = Integer.parseInt(curValStr.toString());
24792481
curCoord++;
24802482
}
24812483
if (shape.equalsIgnoreCase("rect")) {

0 commit comments

Comments
 (0)