Skip to content

Commit

Permalink
Verify EqualsAvoidsNullVisitor works well for chained method calls (#…
Browse files Browse the repository at this point in the history
…412)

* inline

forma

nested

* Add missing test sources to make test pass

* Rename test method to reflect the case we're testing

---------

Co-authored-by: Vincent Potucek <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored Dec 23, 2024
1 parent 26125aa commit b407586
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@
@EqualsAndHashCode(callSuper = false)
public class EqualsAvoidsNullVisitor<P> extends JavaVisitor<P> {

private static final MethodMatcher EQUALS = new MethodMatcher("java.lang.String " + "equals(java.lang.Object)");
private static final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher("java.lang.String " + "equalsIgnoreCase(java.lang.String)");
private static final MethodMatcher COMPARE_TO = new MethodMatcher("java.lang.String " + "compareTo(java.lang.String)");
private static final MethodMatcher COMPARE_TO_IGNORE_CASE = new MethodMatcher("java.lang.String " + "compareToIgnoreCase(java.lang.String)");
private static final MethodMatcher CONTENT_EQUALS = new MethodMatcher("java.lang.String " + "contentEquals(java.lang.CharSequence)");
private static final String JAVA_LANG_STRING = "java.lang.String ";
private static final MethodMatcher EQUALS = new MethodMatcher(JAVA_LANG_STRING + "equals(java.lang.Object)");
private static final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + "equalsIgnoreCase(java.lang.String)");
private static final MethodMatcher COMPARE_TO = new MethodMatcher(JAVA_LANG_STRING + "compareTo(java.lang.String)");
private static final MethodMatcher COMPARE_TO_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + "compareToIgnoreCase(java.lang.String)");
private static final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING + "contentEquals(java.lang.CharSequence)");

EqualsAvoidsNullStyle style;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpec;

import static org.openrewrite.java.Assertions.java;

Expand Down Expand Up @@ -149,6 +150,53 @@ private boolean isFoo(String foo) {
);
}

@Test
void chainedMethodCalls() {
// language=java
rewriteRun(
java(
"""
package c;
public class Constants {
public static final String FOO = "FOO";
}
""",
SourceSpec::skip
),
java(
"""
class Foo {
String getFooType() {
return "FOO";
}
Foo getFOO() {
return this;
}
}
""",
SourceSpec::skip
),
java(
"""
import static c.Constants.FOO;
class A {
boolean filterFoo(final Foo foo) {
return foo.getFOO().getFooType().contentEquals(FOO);
}
}
""",
"""
import static c.Constants.FOO;
class A {
boolean filterFoo(final Foo foo) {
return FOO.contentEquals(foo.getFOO().getFooType());
}
}
"""
)
);
}

@Test
void staticImport() {
rewriteRun(
Expand Down

0 comments on commit b407586

Please sign in to comment.