Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public J visitLambda(J.Lambda lambda, ExecutionContext ctx) {
J.Lambda l = (J.Lambda) super.visitLambda(lambda, ctx);
updateCursor(l);

if (insideAnonymousInnerClass()) {
return l;
}

J body = l.getBody();
if (body instanceof J.Block && ((J.Block) body).getStatements().size() == 1) {
Statement statement = ((J.Block) body).getStatements().get(0);
Expand Down Expand Up @@ -198,6 +202,7 @@ public J visitLambda(J.Lambda lambda, ExecutionContext ctx) {
if (select != null) {
return newInstanceMethodReference(select, methodType, lambda.getType()).withPrefix(lambda.getPrefix());
}

Cursor owner = getCursor().dropParentUntil(is -> is instanceof J.ClassDeclaration ||
(is instanceof J.NewClass && ((J.NewClass) is).getBody() != null) ||
is instanceof J.Lambda);
Expand All @@ -212,6 +217,25 @@ public J visitLambda(J.Lambda lambda, ExecutionContext ctx) {
return l;
}

private boolean insideAnonymousInnerClass() {
// Check if we're inside an anonymous inner class
Cursor current = getCursor();
while (current != null) {
if (current.getValue() instanceof J.ClassDeclaration) {
// We've reached a regular class declaration, stop looking
return false;
}
if (current.getValue() instanceof J.NewClass &&
current.<J.NewClass>getValue().getBody() != null) {
// Don't replace lambdas inside anonymous inner classes that call unqualified methods
// as the "this" reference semantics might change
return true;
}
current = current.getParent();
}
return false;
}

private boolean hasSelectWithPotentialSideEffects(MethodCall method) {
return method instanceof J.MethodInvocation &&
((J.MethodInvocation) method).getSelect() instanceof MethodCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1399,4 +1399,92 @@ void onChange(Foo a, Foo b, boolean c) {
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/722")
@Test
void doNotReplaceInAnonymousInnerClass() {
rewriteRun(
//language=java
java(
"""
public class A {
public void foo() {
B b = new B() {
@Override
public void run() {
new Thread(() -> run()).start();
}
};
}

interface B {
void run();
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/722")
@Test
void doNotReplaceInAnonymousInnerClassWithOtherMethods() {
rewriteRun(
//language=java
java(
"""
public class A {
public void foo() {
B b = new B() {
@Override
public void run() {
execute(() -> doSomething());
}

private void doSomething() {
System.out.println("doing something");
}

private void execute(Runnable r) {
r.run();
}
};
}

interface B {
void run();
}
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/722")
@Test
void stillReplaceInRegularInnerClass() {
rewriteRun(
//language=java
java(
"""
public class A {
class Inner {
public void run() {
new Thread(() -> run()).start();
}
}
}
""",
"""
public class A {
class Inner {
public void run() {
new Thread(this::run).start();
}
}
}
"""
)
);
}
}