-
Notifications
You must be signed in to change notification settings - Fork 60
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
WIP: Fix PMD rule LiteralsFirstInComparisons for compareTo* and contentEquals #448
base: main
Are you sure you want to change the base?
Changes from all commits
1f33b73
b38ff0a
3725b4a
52676bf
883f9cf
1d3c8de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
*/ | ||
package org.openrewrite.staticanalysis; | ||
|
||
import org.apache.commons.lang3.ObjectUtils; | ||
import org.jspecify.annotations.Nullable; | ||
import org.openrewrite.*; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
|
@@ -54,24 +55,23 @@ public Duration getEstimatedEffortPerOccurrence() { | |
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
JavaIsoVisitor<ExecutionContext> replacementVisitor = new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J visit(@Nullable Tree tree, ExecutionContext ctx) { | ||
if (tree instanceof JavaSourceFile) { | ||
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree); | ||
EqualsAvoidsNullStyle style = cu.getStyle(EqualsAvoidsNullStyle.class); | ||
if (style == null) { | ||
style = Checkstyle.equalsAvoidsNull(); | ||
return Preconditions.check( | ||
// new UsesMethod<>("java.lang.String *quals*(..)"), | ||
new UsesMethod<>("java.lang.String *(..)"), | ||
new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J visit(@Nullable Tree tree, ExecutionContext ctx) { | ||
if (tree instanceof JavaSourceFile) { | ||
JavaSourceFile cu = (JavaSourceFile) requireNonNull(tree); | ||
return new EqualsAvoidsNullVisitor<>( | ||
ObjectUtils.defaultIfNull(cu.getStyle(EqualsAvoidsNullStyle.class), | ||
Checkstyle.equalsAvoidsNull())) | ||
.visitNonNull(cu, ctx); | ||
} | ||
//noinspection DataFlowIssue | ||
return (J) tree; | ||
} | ||
return new EqualsAvoidsNullVisitor<>(style).visitNonNull(cu, ctx); | ||
} | ||
//noinspection DataFlowIssue | ||
return (J) tree; | ||
} | ||
}; | ||
return Preconditions.check( | ||
new UsesMethod<>("java.lang.String *quals*(..)"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed this by accident while wondering why the recipe wasn’t applied. I think it would be better to prioritize a functional approach—putting the most important things first. In a functional style, it’s easier to see and understand the flow right away. Thanks for considering this! |
||
replacementVisitor | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,58 @@ public class A { | |
); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void invertConditional_compareTo() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
public class A { | ||
{ | ||
String s = null; | ||
if(s.compareTo("test")) {} | ||
} | ||
} | ||
""", | ||
""" | ||
public class A { | ||
{ | ||
String s = null; | ||
if("test".compareTo(s)) {} | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void invertConditional_compareToIgnoreCase() { | ||
rewriteRun( | ||
//language=java | ||
java( | ||
""" | ||
public class A { | ||
{ | ||
String s = null; | ||
if(s.compareToIgnoreCase("test")) {} | ||
} | ||
} | ||
""", | ||
""" | ||
public class A { | ||
{ | ||
String s = null; | ||
if("test".compareToIgnoreCase(s)) {} | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void removeUnnecessaryNullCheck() { | ||
rewriteRun( | ||
|
@@ -447,6 +499,15 @@ public class A { | |
System.out.println(s.compareToIgnoreCase("test")); | ||
} | ||
} | ||
""", | ||
""" | ||
public class A { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m not sure why we don’t have a second comparison method here—you guys probably know better. Looking at the code, it seems like the goal is to avoid an NPE. This test was the only one failing, which caught my attention, so I decided to conclude it and ask the team for their thoughts. Thanks! |
||
{ | ||
String s = null; | ||
System.out.println("test".compareTo(s)); | ||
System.out.println("test".compareToIgnoreCase(s)); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @timtebeek,
I need help finding a better regex for equals and compareTo. The current one is just a temporary fix—it might work, but it's inefficient.