Skip to content

Commit

Permalink
fix: inline anonymous class wrongly handles field as Classname.this (PR
Browse files Browse the repository at this point in the history
#2367)

* fix: inline anonymous class wrongly handle Classname.this

* add test case

---------

Co-authored-by: Skylot <[email protected]>
  • Loading branch information
ewt45 and skylot authored Dec 12, 2024
1 parent 0d105a5 commit 17695ba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,21 @@ private static void removeSyntheticFields(ClassNode cls) {
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)) {
found++;
}
}
if (found != 0) {
field.addAttr(new FieldReplaceAttr(fieldsCls.getClassInfo()));
if (isParentInst) {
field.addAttr(new FieldReplaceAttr(fieldsCls.getClassInfo()));
}
field.add(AFlag.DONT_GENERATE);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 17695ba

Please sign in to comment.