fix: inline anonymous class wrongly handles field as Classname.this (PR #2367)

* fix: inline anonymous class wrongly handle Classname.this

* add test case

---------

Co-authored-by: Skylot <118523+skylot@users.noreply.github.com>
This commit is contained in:
ewt45
2024-12-13 02:54:05 +08:00
committed by GitHub
parent 0d105a5095
commit 17695babf4
2 changed files with 48 additions and 3 deletions
@@ -86,8 +86,11 @@ public class ClassModifier extends AbstractVisitor {
ClassInfo clsInfo = ClassInfo.fromType(cls.root(), fldType);
ClassNode fieldsCls = cls.root().resolveClass(clsInfo);
ClassInfo parentClass = cls.getClassInfo().getParentClass();
if (fieldsCls != null
&& (inline || Objects.equals(parentClass, fieldsCls.getClassInfo()))) {
if (fieldsCls == null) {
continue;
}
boolean isParentInst = Objects.equals(parentClass, fieldsCls.getClassInfo());
if (inline || isParentInst) {
int found = 0;
for (MethodNode mth : cls.getMethods()) {
if (removeFieldUsageFromConstructor(mth, field, fieldsCls)) {
@@ -95,7 +98,9 @@ public class ClassModifier extends AbstractVisitor {
}
}
if (found != 0) {
field.addAttr(new FieldReplaceAttr(fieldsCls.getClassInfo()));
if (isParentInst) {
field.addAttr(new FieldReplaceAttr(fieldsCls.getClassInfo()));
}
field.add(AFlag.DONT_GENERATE);
}
}
@@ -0,0 +1,40 @@
package jadx.tests.integration.inner;
import org.junit.jupiter.api.Test;
import jadx.tests.api.IntegrationTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestAnonymousClass22 extends IntegrationTest {
public static class TestCls {
public static class Parent {
public static Parent test(Class<?> cls) {
final AnotherClass another = new AnotherClass();
return new Parent() {
@Override
public String func() {
return another.toString();
}
};
}
public String func() {
return "";
}
}
public static class AnotherClass {
}
}
@Test
public void test() {
assertThat(getClassNode(TestCls.class))
.code()
.containsOne("return another.toString();")
.doesNotContain("AnotherClass.this");
}
}