-
Notifications
You must be signed in to change notification settings - Fork 34
Add ArgumentArrayToVarargs for logger methods that take a var args argument Object array
#246
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
Changes from 1 commit
204c632
e99a822
2325506
a40392f
1a748cc
5df1c9a
7a8b248
1157ca6
d095295
bd0f49d
fc4fa6f
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * Copyright 2025 the original author or authors. | ||
| * <p> | ||
| * Licensed under the Moderne Source Available License (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * <p> | ||
| * https://docs.moderne.io/licensing/moderne-source-available-license | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.openrewrite.java.logging; | ||
|
|
||
| import org.openrewrite.ExecutionContext; | ||
| import org.openrewrite.Preconditions; | ||
| import org.openrewrite.Recipe; | ||
| import org.openrewrite.TreeVisitor; | ||
| import org.openrewrite.java.JavaVisitor; | ||
| import org.openrewrite.java.MethodMatcher; | ||
| import org.openrewrite.java.search.UsesMethod; | ||
| import org.openrewrite.java.tree.J; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| // Inspired by `org.openrewrite.staticanalysis.SimplifyArraysAsList` | ||
| public class ArgumentArrayToVarargs extends Recipe { | ||
| private static final MethodMatcher LOGGER_METHOD = new MethodMatcher("*..Log* *(..)"); | ||
|
|
||
| @Override | ||
| public String getDisplayName() { | ||
| return "Convert argument array to varargs"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Converts method calls that use an array of arguments to use varargs instead."; | ||
| } | ||
|
|
||
| @Override | ||
| public Duration getEstimatedEffortPerOccurrence() { | ||
| return Duration.ofMinutes(2); | ||
| } | ||
|
|
||
| @Override | ||
| public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
| return Preconditions.check(new UsesMethod<>(LOGGER_METHOD), new JavaVisitor<ExecutionContext>() { | ||
| @Override | ||
| public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
| J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx); | ||
| if (LOGGER_METHOD.matches(mi)) { | ||
| } | ||
| return mi; | ||
| } | ||
| }); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,65 @@ | ||||||||||||
| /* | ||||||||||||
| * Copyright 2025 the original author or authors. | ||||||||||||
| * <p> | ||||||||||||
| * Licensed under the Moderne Source Available License (the "License"); | ||||||||||||
| * you may not use this file except in compliance with the License. | ||||||||||||
| * You may obtain a copy of the License at | ||||||||||||
| * <p> | ||||||||||||
| * https://docs.moderne.io/licensing/moderne-source-available-license | ||||||||||||
| * <p> | ||||||||||||
| * Unless required by applicable law or agreed to in writing, software | ||||||||||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||
| * See the License for the specific language governing permissions and | ||||||||||||
| * limitations under the License. | ||||||||||||
| */ | ||||||||||||
| package org.openrewrite.java.logging; | ||||||||||||
|
|
||||||||||||
| import org.junit.jupiter.api.Test; | ||||||||||||
| import org.openrewrite.DocumentExample; | ||||||||||||
| import org.openrewrite.InMemoryExecutionContext; | ||||||||||||
| import org.openrewrite.java.JavaParser; | ||||||||||||
| import org.openrewrite.staticanalysis.SimplifyArraysAsList; | ||||||||||||
|
timtebeek marked this conversation as resolved.
Outdated
timtebeek marked this conversation as resolved.
Outdated
|
||||||||||||
| import org.openrewrite.test.RecipeSpec; | ||||||||||||
|
timtebeek marked this conversation as resolved.
Outdated
timtebeek marked this conversation as resolved.
Outdated
|
||||||||||||
| import org.openrewrite.test.RewriteTest; | ||||||||||||
|
|
||||||||||||
| import static org.openrewrite.java.Assertions.java; | ||||||||||||
|
|
||||||||||||
| @SuppressWarnings({"RedundantArrayCreation", "ArraysAsListWithZeroOrOneArgument"}) | ||||||||||||
| class ArgumentArrayToVarargsTest implements RewriteTest { | ||||||||||||
|
|
||||||||||||
| @Override | ||||||||||||
| public void defaults(RecipeSpec spec) { | ||||||||||||
| spec.recipe(new ArgumentArrayToVarargs()) | ||||||||||||
| .parser(JavaParser.fromJavaVersion() | ||||||||||||
| .classpathFromResources(new InMemoryExecutionContext(), "slf4j-api-2.1.+")); | ||||||||||||
|
Comment on lines
+31
to
+34
Contributor
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. Tests are passing without the extra config
Suggested change
Member
Author
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 suspect that's a result of a leaky classloader, as we do really need the SLF4J classes on the classpath, which ought not to be present by default. Hence why I'd added them here as that might change in the future. |
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| @DocumentExample | ||||||||||||
| @Test | ||||||||||||
| void objectArrayToVarargs() { | ||||||||||||
| rewriteRun( | ||||||||||||
| //language=java | ||||||||||||
| java( | ||||||||||||
| """ | ||||||||||||
| import org.slf4j.Logger; | ||||||||||||
| class Test { | ||||||||||||
| Logger logger; | ||||||||||||
| void method() { | ||||||||||||
| logger.info("Message {} {} {}", new Object[]{"old", "style", "args"}); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| """, | ||||||||||||
| """ | ||||||||||||
| import org.slf4j.Logger; | ||||||||||||
| class Test { | ||||||||||||
| Logger logger; | ||||||||||||
| void method() { | ||||||||||||
| logger.info("Message {} {} {}", "old", "style", "args"); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
| """ | ||||||||||||
| ) | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
Uh oh!
There was an error while loading. Please reload this page.