Skip to content

Commit

Permalink
Minor fix in FindRecipes to prevent unexpected reported errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima committed Oct 10, 2023
1 parent 8c8c9ad commit 9a5b79d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,23 @@ public String getDescription() {
)
);
}

@Test
void returnInLambda() {
rewriteRun(
spec -> spec.recipe(new FindRecipes()),
java(
"""
import java.util.function.UnaryOperator;
class SomeTest {
private final UnaryOperator<String> notEmpty = actual -> {
//noinspection CodeBlock2Expr
return actual + "\\n";
};
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,19 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m

@Override
public J.Return visitReturn(J.Return aReturn, ExecutionContext ctx) {
J.MethodDeclaration method = getCursor().firstEnclosingOrThrow(J.MethodDeclaration.class);
if (getDisplayName.matches(method.getMethodType()) && aReturn.getExpression() instanceof J.Literal) {
getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, "displayName",
requireNonNull(((J.Literal) aReturn.getExpression()).getValue()));
}
if (getDescription.matches(method.getMethodType()) && aReturn.getExpression() instanceof J.Literal) {
getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, "description",
requireNonNull(((J.Literal) aReturn.getExpression()).getValue()));
J j = getCursor().dropParentUntil(it -> it instanceof J.MethodDeclaration || it instanceof J.ClassDeclaration).getValue();
if (j instanceof J.MethodDeclaration) {
J.MethodDeclaration method = (J.MethodDeclaration) j;
if (getDisplayName.matches(method.getMethodType()) && aReturn.getExpression() instanceof J.Literal) {
getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, "displayName",
requireNonNull(((J.Literal) aReturn.getExpression()).getValue()));
}
if (getDescription.matches(method.getMethodType()) && aReturn.getExpression() instanceof J.Literal) {
getCursor().putMessageOnFirstEnclosing(J.ClassDeclaration.class, "description",
requireNonNull(((J.Literal) aReturn.getExpression()).getValue()));
}
}

return super.visitReturn(aReturn, ctx);
}

Expand Down

0 comments on commit 9a5b79d

Please sign in to comment.