Commit c9521192 authored by Skylot's avatar Skylot

core: omit 'this' for methods and fields

parent 95e9da36
......@@ -125,7 +125,21 @@ public class InsnGen {
}
private String ifield(FieldInfo field, InsnArg arg) throws CodegenException {
return arg(arg) + "." + field.getName();
String name = field.getName();
if (arg.isThis()) {
boolean useShort = true;
List<RegisterArg> args = mth.getArguments(false);
for (RegisterArg param : args) {
String paramName = param.getTypedVar().getName();
if (paramName != null && paramName.equals(name)) {
useShort = false;
}
}
if (useShort) {
return name; // FIXME: check variable names in scope
}
}
return arg(arg) + "." + name;
}
private String sfield(FieldInfo field) {
......@@ -531,7 +545,10 @@ public class InsnGen {
case DIRECT:
case VIRTUAL:
case INTERFACE:
code.add(arg(insn.getArg(0))).add('.');
InsnArg arg = insn.getArg(0);
if (!arg.isThis()) { // FIXME: add 'this' for equals methods in scope
code.add(arg(arg)).add('.');
}
k++;
break;
......
package jadx.tests.internal;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
public class TestRedundantThis extends InternalJadxTest {
public static class TestCls {
public int field1 = 1;
public int field2 = 2;
public boolean f1() {
return false;
}
public int method() {
f1();
return field1;
}
public void method2(int field2) {
this.field2 = field2;
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, not(containsString("this.f1();")));
assertThat(code, not(containsString("return this.field1;")));
assertThat(code, containsString("this.field2 = field2;"));
}
}
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