Skip to content

Fix NullPointerException in VisitorUtils #509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 2 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 @@ -21,6 +21,7 @@
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.J.MethodDeclaration;
import org.openrewrite.java.tree.J.MethodInvocation;
Expand Down Expand Up @@ -151,7 +152,7 @@ public static class AdjustTypesFromExpressionMarkers extends Recipe {

@Override
public String getDisplayName() {
return "Adjustt type based on marked expressions";
return "Adjust type based on marked expressions";
}

@Override
Expand All @@ -161,12 +162,15 @@ protected TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public Return visitReturn(Return _return, ExecutionContext p) {
Return r = super.visitReturn(_return, p);
MarkReturnType marker = r.getExpression().getMarkers().findFirst(MarkReturnType.class).orElse(null);
if (marker != null) {
removeMarker(r.getExpression(), marker);
MethodDeclaration method = getCursor().firstEnclosing(MethodDeclaration.class);
if (method != null) {
doAfterVisit(new ChangeMethodReturnTypeRecipe(m -> m.getId().equals(method.getId()), marker.getExpression(), marker.getImports()));
Expression expression = r.getExpression();
if (expression != null) {
MarkReturnType marker = expression.getMarkers().findFirst(MarkReturnType.class).orElse(null);
if (marker != null) {
removeMarker(expression, marker);
MethodDeclaration method = getCursor().firstEnclosing(MethodDeclaration.class);
if (method != null) {
doAfterVisit(new ChangeMethodReturnTypeRecipe(m -> m.getId().equals(method.getId()), marker.getExpression(), marker.getImports()));
}
}
}
return r;
Expand Down