fix: don't eliminate StringBuilder if no String arg present
This commit is contained in:
@@ -332,6 +332,20 @@ public class SimplifyVisitor extends AbstractVisitor {
|
||||
}
|
||||
args.add(arg);
|
||||
}
|
||||
|
||||
boolean stringArgFound = false;
|
||||
for (InsnArg arg : args) {
|
||||
if (arg.getType().equals(ArgType.STRING)) {
|
||||
stringArgFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!stringArgFound) {
|
||||
// TODO: convert one arg to string using `String.valueOf()`
|
||||
return null;
|
||||
}
|
||||
|
||||
// all check passed
|
||||
removeStringBuilderInsns(mth, toStrInsn, chain);
|
||||
|
||||
InsnNode concatInsn = new InsnNode(InsnType.STR_CONCAT, args);
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
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;
|
||||
|
||||
public class TestStringBuilderElimination4Neg extends IntegrationTest {
|
||||
|
||||
public static class TestCls<K, V> {
|
||||
private K k;
|
||||
private V v;
|
||||
|
||||
public String test() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(k);
|
||||
sb.append('=');
|
||||
sb.append(v);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
ClassNode cls = getClassNode(TestCls.class);
|
||||
String code = cls.getCode().toString();
|
||||
|
||||
assertThat(code, containsString("sb.append('=');"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user