Skip to content
Merged
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;
Comment thread
timtebeek marked this conversation as resolved.
Outdated
Comment thread
timtebeek marked this conversation as resolved.
Outdated
Comment thread
timtebeek marked this conversation as resolved.
Outdated
import org.openrewrite.test.RecipeSpec;
Comment thread
timtebeek marked this conversation as resolved.
Outdated
Comment thread
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests are passing without the extra config

Suggested change
public void defaults(RecipeSpec spec) {
spec.recipe(new ArgumentArrayToVarargs())
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "slf4j-api-2.1.+"));
spec.recipe(new ArgumentArrayToVarargs());

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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");
}
}
"""
)
);
}
}
Loading