-
Notifications
You must be signed in to change notification settings - Fork 29
Recipe to remove .toString() called on parameterized logging statement arguments
#224
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 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
84b51c2
New `StripToStringFromArguments` SLF4j recipe
pdelagrave f2f7ec0
Merge branch 'main' into issue-223
timtebeek f887c8a
Remove useless null check
pdelagrave a55edd8
Replace bad string comparison, using TypeUtils now
pdelagrave b32934a
Don't use final for local variables
pdelagrave 23b295f
using a matcher to match toString invocations
pdelagrave 40fa083
curly braces
pdelagrave adb6356
Licenses
pdelagrave 2ed724e
new test, needs a fix
pdelagrave e79fc64
refactor tests, add test with no additional message format string par…
pdelagrave c4682a4
Using ListUtils.map instead of manually creating a list
pdelagrave 65a1049
Returns `super.visitMethodInvocation()` if the the method doesn't match
pdelagrave 6d10848
rename mi to m
pdelagrave 0201efa
Do strip toString() on Throwable.toString() when that argument type b…
pdelagrave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/java/org/openrewrite/java/logging/slf4j/StripToStringFromArguments.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package org.openrewrite.java.logging.slf4j; | ||
|
|
||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Preconditions; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaIsoVisitor; | ||
| import org.openrewrite.java.search.UsesMethod; | ||
| import org.openrewrite.java.tree.Expression; | ||
| import org.openrewrite.java.tree.J; | ||
| import org.openrewrite.java.tree.TypeUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class StripToStringFromArguments extends Recipe { | ||
| @Override | ||
| public String getDisplayName() { | ||
| return "Strip `toString()` from arguments"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Remove `.toString()` from logger call arguments; SLF4J will automatically call `toString()` on an argument when not a string, and do so only if the log level is enabled."; | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check(Preconditions.or( | ||
| new UsesMethod<>("org.slf4j.Logger trace(..)"), | ||
| new UsesMethod<>("org.slf4j.Logger debug(..)"), | ||
| new UsesMethod<>("org.slf4j.Logger info(..)"), | ||
| new UsesMethod<>("org.slf4j.Logger warn(..)"), | ||
| new UsesMethod<>("org.slf4j.Logger error(..)") | ||
| ), | ||
| new JavaIsoVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J.MethodInvocation visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) { | ||
pdelagrave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final int firstFormatArgIndex = mi.getArguments().get(0).getType().toString() == "org.slf4j.Marker" ? 2 : 1; | ||
pdelagrave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final List<Expression> newArguments = new ArrayList<>(mi.getArguments().subList(0, firstFormatArgIndex)); | ||
| for (int i = firstFormatArgIndex; i < mi.getArguments().size(); i++) { | ||
pdelagrave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| final Expression arg = mi.getArguments().get(i); | ||
pdelagrave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Expression toAdd = arg; | ||
| if (arg instanceof J.MethodInvocation) { | ||
pdelagrave marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| J.MethodInvocation toStringInvocation = (J.MethodInvocation) arg; | ||
| if (toStringInvocation.getSimpleName().equals("toString") && | ||
pdelagrave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| toStringInvocation.getSelect() != null && | ||
| toStringInvocation.getSelect().getType() != null && | ||
pdelagrave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| !TypeUtils.isAssignableTo("java.lang.Throwable", toStringInvocation.getSelect().getType())) { | ||
| toAdd = toStringInvocation.getSelect().withPrefix(toStringInvocation.getPrefix()); | ||
| } | ||
| } | ||
| newArguments.add(toAdd); | ||
| } | ||
| return mi.withArguments(newArguments); | ||
pdelagrave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/test/java/org/openrewrite/java/logging/slf4j/StripToStringFromArgumentsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package org.openrewrite.java.logging.slf4j; | ||
pdelagrave marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.Arguments; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import org.openrewrite.InMemoryExecutionContext; | ||
| import org.openrewrite.java.JavaParser; | ||
| import org.openrewrite.test.RecipeSpec; | ||
| import org.openrewrite.test.RewriteTest; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static org.openrewrite.java.Assertions.java; | ||
|
|
||
| public class StripToStringFromArgumentsTest implements RewriteTest { | ||
pdelagrave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Override | ||
| public void defaults(RecipeSpec spec) { | ||
| spec.recipe(new StripToStringFromArguments()).parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "slf4j-api-2.1.+")); | ||
pdelagrave marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private static List<Arguments> stripToStringFromLogMethodArguments() { | ||
pdelagrave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| List<Arguments> arguments = new ArrayList<>(); | ||
| for (String marker : List.of("", "marker, ")) { | ||
| for (String method : List.of("trace", "debug", "info", "warn", "error")) { | ||
| arguments.addAll(List.of( | ||
| Arguments.of(method, marker, | ||
| List.of("o1.toString()"), | ||
| List.of("o1")), | ||
| Arguments.of(method, marker, | ||
| List.of("o1.toString()", "o2.toString()"), | ||
| List.of("o1", "o2")), | ||
| Arguments.of(method, marker, | ||
| List.of("o1.toString()", "o2.toString()", "o3.toString()"), | ||
| List.of("o1", "o2", "o3")), | ||
| Arguments.of(method, marker, | ||
| List.of("o1.toString()", "o2", "o3.toString()", "o1", "o3", "o1.toString()", "o2"), | ||
| List.of("o1", "o2", "o3", "o1", "o3", "o1", "o2")), | ||
| Arguments.of(method, marker, | ||
| List.of("exception.toString()"), | ||
| List.of("exception.toString()")) | ||
| )); | ||
| } | ||
| } | ||
| return arguments; | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @MethodSource | ||
| void stripToStringFromLogMethodArguments(String method, String marker, List<String> arguments, List<String> expectedArguments) { | ||
| //language=java | ||
| String testTemplate = """ | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.Marker; | ||
|
|
||
| class A { | ||
| void method(Logger log, Object o1, Object o2, Object o3, Exception exception, Marker marker) { | ||
| log.%s(%s"Hello", %s); | ||
| } | ||
| } | ||
| """; | ||
| String before = String.format(testTemplate, method, marker, String.join(", ", arguments)); | ||
| String after = String.format(testTemplate, method, marker, String.join(", ", expectedArguments)); | ||
|
|
||
| // Ideally we'd only call `rewriteRun(java(before, after));` but the only way to expect a no-change is to call `java(before)` | ||
| if (before.equals(after)) | ||
| rewriteRun(java(before)); | ||
timtebeek marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else | ||
| rewriteRun(java(before, after)); | ||
pdelagrave marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.