Skip to content

Commit

Permalink
Prevent NPE in Enum equals with empty select
Browse files Browse the repository at this point in the history
Fixes #143
  • Loading branch information
timtebeek committed Jul 24, 2023
1 parent be14ab4 commit 664e9dd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, executionContext);
if (enumEquals.matches(m)) {
if (enumEquals.matches(m) && m.getSelect() != null) {
Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Unary || is instanceof J.Block);
boolean isNot = parent.getValue() instanceof J.Unary && ((J.Unary) parent.getValue()).getOperator() == J.Unary.Type.Not;
if (isNot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void defaults(RecipeSpec spec) {
spec.recipe(new CompareEnumsWithEqualityOperator());
}

//language=java
SourceSpecs enumA = java(
"""
package a;
Expand Down Expand Up @@ -198,4 +199,21 @@ void m(Object parameterValue, Type partType) {
"""
));
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/143")
void noSelect() {
rewriteRun(
//language=java
java("""
package a;
public enum A {
FOO, BAR, BUZ;
boolean isFoo() {
return equals(FOO);
}
}
""")
);
}
}

0 comments on commit 664e9dd

Please sign in to comment.