Commit ecaa87e7 authored by Ahmed Ashour's avatar Ahmed Ashour Committed by skylot

fix: remove redundant array type when initialized with declaration (PR #566)

parent 0a08d8b6
...@@ -532,8 +532,10 @@ public class InsnGen { ...@@ -532,8 +532,10 @@ public class InsnGen {
} }
private void filledNewArray(FilledNewArrayNode insn, CodeWriter code) throws CodegenException { private void filledNewArray(FilledNewArrayNode insn, CodeWriter code) throws CodegenException {
if (!insn.contains(AFlag.DECLARE_VAR)) {
code.add("new "); code.add("new ");
useType(code, insn.getArrayType()); useType(code, insn.getArrayType());
}
code.add('{'); code.add('{');
int c = insn.getArgsCount(); int c = insn.getArgsCount();
for (int i = 0; i < c; i++) { for (int i = 0; i < c; i++) {
......
package jadx.tests.integration.arrays;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
public class TestArrayInit extends IntegrationTest {
public static class TestCls {
byte[] bytes;
@SuppressWarnings("unused")
public void test() {
byte[] arr = new byte[]{10, 20, 30};
}
public void test2() {
bytes = new byte[]{10, 20, 30};
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsString("= {10, 20, 30};"));
assertThat(code, containsString("this.bytes = new byte[]{10, 20, 30};"));
}
}
...@@ -29,7 +29,7 @@ public class TestInline2 extends IntegrationTest { ...@@ -29,7 +29,7 @@ public class TestInline2 extends IntegrationTest {
ClassNode cls = getClassNode(TestCls.class); ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString(); String code = cls.getCode().toString();
assertThat(code, containsOne("int[] a = new int[]{1, 2, 4, 6, 8};")); assertThat(code, containsOne("int[] a = {1, 2, 4, 6, 8};"));
assertThat(code, containsOne("for (int i = 0; i < a.length; i += 2) {")); assertThat(code, containsOne("for (int i = 0; i < a.length; i += 2) {"));
assertThat(code, containsOne("for (long i2 = (long) b; i2 > 0; i2--) {")); assertThat(code, containsOne("for (long i2 = (long) b; i2 > 0; i2--) {"));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment