Skip to content

Commit

Permalink
Fix lambdas in anonymous classes referencing parameters from the root…
Browse files Browse the repository at this point in the history
… method (#423)

* Fix lambdas in anonymous classes referencing parameters from the root
method

* Update test
  • Loading branch information
coehlrich authored Aug 24, 2024
1 parent d75fd5f commit 842371b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,7 @@ private void iterateClashingExprent(Statement stat, StructMethod mt, Map<Stateme
for (String mthKey : node.getWrapper().getMethods().getLstKeys()) {
MethodWrapper mw = node.getWrapper().getMethods().getWithKey(mthKey);
StructMethod mt2 = node.getWrapper().getClassStruct().getMethod(mthKey);
if (mt2 != null && mw != null) {
if (mt2 != null && mw != null && !mt2.hasModifier(CodeConstants.ACC_SYNTHETIC)) {
// Propagate current data through to method
VarDefinitionHelper vardef = new VarDefinitionHelper(mw.root, mt2, mw.varproc, false);

Expand Down
1 change: 1 addition & 0 deletions test/org/jetbrains/java/decompiler/SingleClassesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ private void registerDefault() {
register(JAVA_21, "TestSwitchPatternMatchingJ21");
register(JAVA_21, "TestCastIntersectionJ21");
register(JAVA_16, "TestRecordLocal");
register(JAVA_8, "TestAnonymousClassToLambda");
}

private void registerEntireClassPath() {
Expand Down
38 changes: 38 additions & 0 deletions testData/results/pkg/TestAnonymousClassToLambda.dec
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package pkg;

public class TestAnonymousClassToLambda {
public Object test1(final Object o) {
return new Object() {// 5
public Runnable getRunnable() {
return () -> System.out.println(o);// 7
}
};
}
}

class 'pkg/TestAnonymousClassToLambda' {
method 'test1 (Ljava/lang/Object;)Ljava/lang/Object;' {
9 4
}
}

class 'pkg/TestAnonymousClassToLambda$1' {
method 'getRunnable ()Ljava/lang/Runnable;' {
9 6
}

method 'lambda$getRunnable$0 (Ljava/lang/Object;)V' {
0 6
1 6
2 6
3 6
4 6
5 6
6 6
7 6
}
}

Lines mapping:
5 <-> 5
7 <-> 7
11 changes: 11 additions & 0 deletions testData/src/java8/pkg/TestAnonymousClassToLambda.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pkg;

public class TestAnonymousClassToLambda {
public Object test1(Object o) {
return new Object() {
public Runnable getRunnable() {
return () -> System.out.println(o);
}
};
}
}

0 comments on commit 842371b

Please sign in to comment.