-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve StringBuilder elimination (#704)
- Loading branch information
Showing
7 changed files
with
209 additions
and
90 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
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
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
2 changes: 1 addition & 1 deletion
2
...gration/TestStringBuilderElimination.java → .../others/TestStringBuilderElimination.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package jadx.tests.integration; | ||
package jadx.tests.integration.others; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...ration/TestStringBuilderElimination2.java → ...others/TestStringBuilderElimination2.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package jadx.tests.integration; | ||
package jadx.tests.integration.others; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
|
63 changes: 63 additions & 0 deletions
63
jadx-core/src/test/java/jadx/tests/integration/others/TestStringBuilderElimination3.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,63 @@ | ||
package jadx.tests.integration.others; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import jadx.core.dex.nodes.ClassNode; | ||
import jadx.tests.api.IntegrationTest; | ||
|
||
import static org.hamcrest.CoreMatchers.containsString; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
|
||
public class TestStringBuilderElimination3 extends IntegrationTest { | ||
|
||
public static class TestCls { | ||
public static String test(String a) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("result = "); | ||
sb.append(a); | ||
return sb.toString(); | ||
} | ||
} | ||
|
||
@Test | ||
public void test() { | ||
ClassNode cls = getClassNode(TestCls.class); | ||
String code = cls.getCode().toString(); | ||
|
||
assertThat(code, containsString("return \"result = \" + a;")); | ||
assertThat(code, not(containsString("new StringBuilder()"))); | ||
} | ||
|
||
public static class TestClsNegative { | ||
private String f = "first"; | ||
|
||
public String test() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("before = "); | ||
sb.append(this.f); | ||
updateF(); | ||
sb.append(", after = "); | ||
sb.append(this.f); | ||
return sb.toString(); | ||
} | ||
|
||
private void updateF() { | ||
this.f = "second"; | ||
} | ||
|
||
public void check() { | ||
assertThat(test(), is("before = first, after = second")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNegative() { | ||
ClassNode cls = getClassNode(TestClsNegative.class); | ||
String code = cls.getCode().toString(); | ||
|
||
assertThat(code, containsString("return sb.toString();")); | ||
assertThat(code, containsString("new StringBuilder()")); | ||
} | ||
} |