Skip to content
Merged
Changes from 1 commit
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 @@ -19,11 +19,13 @@
import lombok.Value;
import org.openrewrite.Cursor;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;

Expand Down Expand Up @@ -64,7 +66,16 @@ public Duration getEstimatedEffortPerOccurrence() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
return Preconditions.check(
Preconditions.or(
new UsesMethod<>(STRING_FORMAT),
new UsesMethod<>(STRING_FORMATTED),
new UsesMethod<>(PRINT_STREAM_PRINTF),
new UsesMethod<>(PRINT_WRITER_PRINTF),
new UsesMethod<>(FORMATTER_FORMAT),
new UsesMethod<>(CONSOLE_PRINTF)
),
new JavaIsoVisitor<ExecutionContext>() {

@Override
public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
Expand All @@ -82,7 +93,7 @@ public J.Literal visitLiteral(J.Literal literal, ExecutionContext ctx) {
// Direct use in method invocation
if (value instanceof J.MethodInvocation) {
J.MethodInvocation mi = (J.MethodInvocation) value;
if (isFormatMethod(mi) && mi.getArguments().size() > 0 && mi.getArguments().get(0) == literal) {
if (isFormatMethod(mi) && !mi.getArguments().isEmpty() && mi.getArguments().get(0) == literal) {
return replaceNewlineInLiteral(l);
}
}
Expand Down Expand Up @@ -176,6 +187,6 @@ private J.MethodInvocation replaceNewlineInFormatString(J.MethodInvocation mi) {

return mi;
}
};
});
}
}