-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix detector StringConcatenation to detect bug SBSC_USE_STRINGBUFFER_…
…CONCATENATION also in Java 11 and above (#2186) * Test for Issue #2182 * Fix detector `StringConcatenation` to detect bug `SBSC_USE_STRINGBUFFER_CONCATENATION` also in Java 11 and above Instead of using `StringBuffer` or `StringBuilder` internally, Java 11 and above uses a dynamic call to `makeConcatWithConstants()` to append strings. However, in case of loops, this is still less efficient than creating a `StringBuffer` or `StringBuilder` outside the loop and using it inside to build the string. Previously, the `StringConcatenation` detector missed all the cases in Java 11 and higher, but this PR fixes this. See issue [#2182](#2182). * Updated according to the comments of @ThrawnCA Co-authored-by: Kengo TODA <skypencil@gmail.com>
- Loading branch information
1 parent
c4c26c1
commit b4560e0
Showing
4 changed files
with
91 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
spotbugs-tests/src/test/java/edu/umd/cs/findbugs/detect/Issue2182Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package edu.umd.cs.findbugs.detect; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import edu.umd.cs.findbugs.AbstractIntegrationTest; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher; | ||
import edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder; | ||
|
||
import static org.junit.Assume.assumeFalse; | ||
import static org.junit.Assume.assumeThat; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasItem; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.number.OrderingComparison.greaterThanOrEqualTo; | ||
|
||
public class Issue2182Test extends AbstractIntegrationTest { | ||
@Before | ||
public void verifyJavaVersion() { | ||
assumeFalse(System.getProperty("java.specification.version").startsWith("1.")); | ||
int javaVersion = Integer.parseInt(System.getProperty("java.specification.version")); | ||
assumeThat(javaVersion, is(greaterThanOrEqualTo(11))); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
performAnalysis("../java11/ghIssues/Issue2182.class"); | ||
BugInstanceMatcher matcher = new BugInstanceMatcherBuilder() | ||
.bugType("SBSC_USE_STRINGBUFFER_CONCATENATION") | ||
.inClass("Issue2182") | ||
.inMethod("test") | ||
.atLine(22) | ||
.build(); | ||
assertThat(getBugCollection(), hasItem(matcher)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ghIssues; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
public class Issue2182 { | ||
|
||
public void test() { | ||
List<String> A = new ArrayList<String>(); | ||
A.add("Alex"); | ||
A.add("john"); | ||
A.add("lily"); | ||
A.add("tracy"); | ||
Iterator <String>it = A.iterator(); | ||
|
||
it = A.iterator(); | ||
String add = ""; | ||
while (it.hasNext()) { | ||
String retrieve = it.next(); | ||
if (retrieve != null && !"".equals(retrieve)) { | ||
add += retrieve; | ||
} | ||
System.out.println(add); | ||
} | ||
} | ||
|
||
} |