-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check variable usage before convert indexed loop to for-each var…
…iant (#483)
- Loading branch information
Showing
2 changed files
with
76 additions
and
2 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
71 changes: 71 additions & 0 deletions
71
jadx-core/src/test/java/jadx/tests/integration/loops/TestIndexedLoop.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,71 @@ | ||
package jadx.tests.integration.loops; | ||
|
||
import java.io.File; | ||
|
||
import org.junit.Test; | ||
|
||
import jadx.core.dex.nodes.ClassNode; | ||
import jadx.tests.api.IntegrationTest; | ||
|
||
import static jadx.tests.api.utils.JadxMatchers.containsOne; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.hamcrest.Matchers.nullValue; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class TestIndexedLoop extends IntegrationTest { | ||
|
||
public static class TestCls { | ||
|
||
public File test(File[] files) { | ||
File file = null; | ||
if (files != null) { | ||
int length = files.length; | ||
if (length == 0) { | ||
file = null; | ||
} else { | ||
for (int i = 0; i < length; i++) { | ||
file = files[i]; | ||
if (file.getName().equals("f")) { | ||
break; | ||
} | ||
} | ||
} | ||
} else { | ||
file = null; | ||
} | ||
if (file != null) { | ||
file.deleteOnExit(); | ||
} | ||
return file; | ||
} | ||
|
||
public void check() { | ||
assertThat(test(null), nullValue()); | ||
assertThat(test(new File[]{}), nullValue()); | ||
|
||
File file = new File("f"); | ||
assertThat(test(new File[]{new File("a"), file}), is(file)); | ||
} | ||
} | ||
|
||
@Test | ||
public void test() { | ||
ClassNode cls = getClassNode(TestCls.class); | ||
String code = cls.getCode().toString(); | ||
|
||
assertThat(code, not(containsString("for (File file :"))); | ||
assertThat(code, containsOne("for (int i = 0; i < length; i++) {")); | ||
} | ||
|
||
@Test | ||
public void testNoDebug() { | ||
noDebugInfo(); | ||
ClassNode cls = getClassNode(TestCls.class); | ||
String code = cls.getCode().toString(); | ||
|
||
assertThat(code, not(containsString("for (File file :"))); | ||
assertThat(code, containsOne("for (int i = 0; i < length; i++) {")); | ||
} | ||
} |