Commit ce7101be authored by Skylot's avatar Skylot

core: always inline 'this' (issue #10)

parent 0a241e3a
......@@ -192,7 +192,11 @@ public class CodeShrinker extends AbstractVisitor {
// continue;
// }
SSAVar sVar = arg.getSVar();
if (sVar.getAssign() == null || sVar.getVariableUseCount() != 1) {
if (sVar.getAssign() == null) {
continue;
}
// allow inline only one use arg or 'this'
if (sVar.getVariableUseCount() != 1 && !arg.isThis()) {
continue;
}
InsnNode assignInsn = sVar.getAssign().getParentInsn();
......
package jadx.tests.internal.usethis;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static jadx.tests.utils.JadxMatchers.containsOne;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
public class TestInlineThis extends InternalJadxTest {
public static class TestCls {
public int field;
private void test() {
TestCls something = this;
something.method();
something.field = 123;
}
private void method() {
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
System.out.println(code);
assertThat(code, not(containsString("something")));
assertThat(code, not(containsString("something.method()")));
assertThat(code, not(containsString("something.field")));
assertThat(code, not(containsString("= this")));
assertThat(code, containsOne("this.field = 123;"));
assertThat(code, containsOne("method();"));
}
}
package jadx.tests.internal;
package jadx.tests.internal.usethis;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
......@@ -30,7 +30,6 @@ public class TestRedundantThis extends InternalJadxTest {
// @Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, not(containsString("this.f1();")));
......
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