Skip to content

Commit 42acf97

Browse files
Fix string concatenation loops (SpotBugs SBSC) (#4370)
* 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. * 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. * 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. * 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. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent db3cea5 commit 42acf97

20 files changed

+201
-182
lines changed

.github/scripts/generate-quality-report.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,8 @@ def main() -> None:
773773
"SA_FIELD_SELF_ASSIGNMENT",
774774
"UC_USELESS_CONDITION",
775775
"EC_UNRELATED_TYPES",
776-
"EQ_ALWAYS_FALSE"
776+
"EQ_ALWAYS_FALSE",
777+
"SBSC_USE_STRINGBUFFER_CONCATENATION"
777778
}
778779
violations = [
779780
f for f in spotbugs.findings

CodenameOne/src/com/codename1/ads/InnerActive.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ public void initService(Ads ads) {
144144
String[] keywords = ads.getKeywords();
145145
if (keywords != null && keywords.length > 0) {
146146
int klen = keywords.length;
147-
String k = "";
147+
StringBuilder k = new StringBuilder();
148148
for (int i = 0; i < klen; i++) {
149-
k += "," + keywords[i];
149+
k.append(",").append(keywords[i]);
150150
}
151-
addParam(this, "k", k.substring(1));
151+
addParam(this, "k", k.toString().substring(1));
152152
}
153153
if (testAds) {
154154
addParam(this, "test", "1");

CodenameOne/src/com/codename1/facebook/FaceBookAccess.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,12 @@ public Oauth2 createOAuth() {
218218
String scope = "";
219219
if (permissions != null && permissions.length > 0) {
220220
int plen = permissions.length;
221+
StringBuilder scopeBuilder = new StringBuilder();
221222
for (int i = 0; i < plen; i++) {
222223
String permission = permissions[i];
223-
scope += permission + ",";
224+
scopeBuilder.append(permission).append(",");
224225
}
226+
scope = scopeBuilder.toString();
225227
scope = scope.substring(0, scope.length() - 1);
226228
}
227229
Hashtable additionalParams = new Hashtable();
@@ -1221,21 +1223,21 @@ public void getUsersDetails(String[] usersIds, String[] fields, final ActionList
12211223
checkAuthentication();
12221224

12231225
final FacebookRESTService con = new FacebookRESTService(token, "https://api.facebook.com/method/users.getInfo", false);
1224-
String ids = usersIds[0];
1226+
StringBuilder ids = new StringBuilder(usersIds[0]);
12251227
int ulen = usersIds.length;
12261228
for (int i = 1; i < ulen; i++) {
1227-
ids += "," + usersIds[i];
1229+
ids.append(",").append(usersIds[i]);
12281230

12291231
}
1230-
con.addArgumentNoEncoding("uids", ids);
1232+
con.addArgumentNoEncoding("uids", ids.toString());
12311233

1232-
String fieldsStr = fields[0];
1234+
StringBuilder fieldsStr = new StringBuilder(fields[0]);
12331235
int flen = fields.length;
12341236
for (int i = 1; i < flen; i++) {
1235-
fieldsStr += "," + fields[i];
1237+
fieldsStr.append(",").append(fields[i]);
12361238

12371239
}
1238-
con.addArgumentNoEncoding("fields", fieldsStr);
1240+
con.addArgumentNoEncoding("fields", fieldsStr.toString());
12391241
con.addArgument("format", "json");
12401242

12411243
con.addResponseListener(new ActionListener() {

CodenameOne/src/com/codename1/io/Log.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,12 @@ public static String getLogContent() {
335335
Reader r = Util.getReader(FileSystemStorage.getInstance().openInputStream(instance.getFileURL()));
336336
char[] buffer = new char[1024];
337337
int size = r.read(buffer);
338+
StringBuilder textBuilder = new StringBuilder();
338339
while (size > -1) {
339-
text += new String(buffer, 0, size);
340+
textBuilder.append(new String(buffer, 0, size));
340341
size = r.read(buffer);
341342
}
343+
text = textBuilder.toString();
342344
r.close();
343345
}
344346
return text;

CodenameOne/src/com/codename1/io/Oauth2.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,26 +385,26 @@ public void actionPerformed(ActionEvent ev) {
385385
}
386386

387387
private String buildURL() {
388-
String URL = oauth2URL + "?client_id=" + Util.encodeUrl(clientId)
389-
+ "&redirect_uri=" + Util.encodeUrl(redirectURI);
388+
StringBuilder URL = new StringBuilder(oauth2URL + "?client_id=" + Util.encodeUrl(clientId)
389+
+ "&redirect_uri=" + Util.encodeUrl(redirectURI));
390390
if (scope != null) {
391-
URL += "&scope=" + Util.encodeUrl(scope);
391+
URL.append("&scope=").append(Util.encodeUrl(scope));
392392
}
393393
if (clientSecret != null) {
394-
URL += "&response_type=code";
394+
URL.append("&response_type=code");
395395
} else {
396-
URL += "&response_type=token";
396+
URL.append("&response_type=token");
397397
}
398398

399399
if (additionalParams != null) {
400400
Enumeration e = additionalParams.keys();
401401
while (e.hasMoreElements()) {
402402
String key = (String) e.nextElement();
403403
String val = additionalParams.get(key).toString();
404-
URL += "&" + Util.encodeUrl(key) + "=" + Util.encodeUrl(val);
404+
URL.append("&").append(Util.encodeUrl(key)).append("=").append(Util.encodeUrl(val));
405405
}
406406
}
407-
return URL;
407+
return URL.toString();
408408
}
409409

410410
private Component createLoginComponent(final ActionListener al, final Form frm, final Form backToForm, final Dialog progress) {

CodenameOne/src/com/codename1/io/Util.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,11 +2492,12 @@ private static String append(String a, int in) {
24922492

24932493
private static String append(String a, long in, int length) {
24942494
int lim = (length << 2) - 4;
2495+
StringBuilder sb = new StringBuilder(a);
24952496
while (lim >= 0) {
2496-
a += (DIGITS[(byte) (in >> lim) & 0x0f]);
2497+
sb.append((DIGITS[(byte) (in >> lim) & 0x0f]));
24972498
lim -= 4;
24982499
}
2499-
return a;
2500+
return sb.toString();
25002501
}
25012502

25022503
private static long getUniqueDeviceID() {
@@ -2537,13 +2538,13 @@ private static long generateLongFromDeviceInfo() {
25372538
* @return
25382539
*/
25392540
private static String sanitizeString(String input) {
2540-
String result = "";
2541+
StringBuilder result = new StringBuilder();
25412542
for (char myChar : input.toCharArray()) {
25422543
if ((myChar >= '0' && myChar <= '9') || (myChar >= 'a' && myChar <= 'z') || (myChar >= 'A' && myChar <= 'Z')) {
2543-
result += myChar;
2544+
result.append(myChar);
25442545
}
25452546
}
2546-
return result.substring(0, Math.min(10, result.length())).toUpperCase();
2547+
return result.toString().substring(0, Math.min(10, result.length())).toUpperCase();
25472548
}
25482549

25492550
/**

CodenameOne/src/com/codename1/properties/PropertyXMLElement.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -372,22 +372,22 @@ public String toString() {
372372
@Override
373373
public String toString(String spacing) {
374374

375-
String str = spacing;
376-
str += "<" + getTagName();
375+
StringBuilder str = new StringBuilder(spacing);
376+
str.append("<").append(getTagName());
377377
Hashtable attributes = getAttributes();
378378
if (attributes != null) {
379379
for (Enumeration e = attributes.keys(); e.hasMoreElements(); ) {
380380
String attrStr = (String) e.nextElement();
381381
String val = (String) attributes.get(attrStr);
382-
str += " " + attrStr + "='" + val + "'";
382+
str.append(" ").append(attrStr).append("='").append(val).append("'");
383383
}
384384
}
385-
str += ">\n";
385+
str.append(">\n");
386386
Vector children = getChildren();
387387
for (int i = 0; i < children.size(); i++) {
388-
str += ((Element) children.get(i)).toString(spacing + ' ');
388+
str.append(((Element) children.get(i)).toString(spacing + ' '));
389389
}
390-
str += spacing + "</" + getTagName() + ">\n";
391-
return str;
390+
str.append(spacing).append("</").append(getTagName()).append(">\n");
391+
return str.toString();
392392
}
393393
}

CodenameOne/src/com/codename1/testing/TestUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ private static String toString(int[] p) {
262262
if (p.length == 0) {
263263
return "{}";
264264
}
265-
String s = "{" + p[0];
265+
StringBuilder s = new StringBuilder("{" + p[0]);
266266
for (int iter = 1; iter < p.length; iter++) {
267-
s += ", " + p[iter];
267+
s.append(", ").append(p[iter]);
268268

269269
}
270-
return s + "}";
270+
return s.append("}").toString();
271271
}
272272

273273
/**

CodenameOne/src/com/codename1/ui/ComponentSelector.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,12 +2229,13 @@ public ComponentSelector addTags(String... tags) {
22292229
existing = "";
22302230
}
22312231

2232+
StringBuilder existingBuilder = new StringBuilder(existing);
22322233
for (String tag : tags) {
2233-
if (existing.indexOf(" " + tag + " ") == -1) {
2234-
existing += " " + tag + " ";
2234+
if (existingBuilder.toString().indexOf(" " + tag + " ") == -1) {
2235+
existingBuilder.append(" ").append(tag).append(" ");
22352236
}
22362237
}
2237-
c.putClientProperty(PROPERTY_TAG, existing);
2238+
c.putClientProperty(PROPERTY_TAG, existingBuilder.toString());
22382239

22392240
}
22402241
return this;
@@ -2266,10 +2267,11 @@ public ComponentSelector removeTags(String... tags) {
22662267
c.putClientProperty(PROPERTY_TAG, null);
22672268
continue;
22682269
}
2270+
StringBuilder existingBuilder = new StringBuilder(existing);
22692271
for (String tag : existingSet) {
2270-
existing += " " + tag + " ";
2272+
existingBuilder.append(" ").append(tag).append(" ");
22712273
}
2272-
c.putClientProperty(PROPERTY_TAG, existing);
2274+
c.putClientProperty(PROPERTY_TAG, existingBuilder.toString());
22732275
}
22742276
return this;
22752277
}

CodenameOne/src/com/codename1/ui/Container.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3095,18 +3095,18 @@ protected String paramString() {
30953095
* @return the container components objects as list of Strings
30963096
*/
30973097
private String getComponentsNames() {
3098-
String ret = "[";
3098+
StringBuilder ret = new StringBuilder("[");
30993099
int componentCount = components.size();
31003100
for (int iter = 0; iter < componentCount; iter++) {
31013101
Component cmp = components.get(iter);
31023102
String className = cmp.getClass().getName();
3103-
ret += className.substring(className.lastIndexOf('.') + 1) + ", ";
3103+
ret.append(className.substring(className.lastIndexOf('.') + 1)).append(", ");
31043104
}
31053105
if (ret.length() > 1) {
3106-
ret = ret.substring(0, ret.length() - 2);
3106+
ret.setLength(ret.length() - 2);
31073107
}
3108-
ret = ret + "]";
3109-
return ret;
3108+
ret.append("]");
3109+
return ret.toString();
31103110
}
31113111

31123112
/**

0 commit comments

Comments
 (0)