core: always inline 'this' (issue #10)
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
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();"));
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -1,4 +1,4 @@
|
||||
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();")));
|
||||
Reference in New Issue
Block a user