Skip to content
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

AddTimeUnitArgument recipe #403

Merged
merged 11 commits into from
Aug 2, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
*/
package org.openrewrite.java.apache.httpclient5;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


Expand Down Expand Up @@ -68,21 +69,25 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
final MethodMatcher matcher = new MethodMatcher(methodPattern);
final JavaTemplate template = JavaTemplate.builder("#{any(long)}, TimeUnit.#{}")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpath("httpclient5", "httpcore5"))
.imports("java.util.concurrent.TimeUnit")
.build();

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation m = super.visitMethodInvocation(method, executionContext);
if (matcher.matches(method) && method.getArguments().size() == 1) {
if (matcher.matches(m)) {
JavaTemplate template = JavaTemplate
.builder(StringUtils.repeat("#{any()}, ", m.getArguments().size()) + "TimeUnit.#{}")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpath("httpclient5", "httpcore5"))
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
.imports("java.util.concurrent.TimeUnit")
.build();

List<Object> arguments = new ArrayList<>(m.getArguments());
arguments.add(timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS);

m = template.apply(
updateCursor(m),
m.getCoordinates().replaceArguments(),
m.getArguments().get(0),
timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS
arguments.toArray(new Object[0])
);
maybeAddImport("java.util.concurrent.TimeUnit");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void test() {
}

@Test
void doesNotModifyMethodsWithMoreThanOneArgument() {
void doesModifyMethodsWithMoreThanOneArgument() {
rewriteRun(
spec -> spec.recipe(new AddTimeUnitArgument("A method(int, float)", null)),
//language=java
Expand All @@ -126,16 +126,19 @@ void doesNotModifyMethodsWithMoreThanOneArgument() {

class A {
private long value;
private float foo;
private TimeUnit timeunit;

A method(int value, float foo) {
this.value = value;
this.foo = foo;
this.timeunit = TimeUnit.MILLISECONDS;
return this;
}

A method(long value, float foo, TimeUnit timeunit) {
this.value = value;
this.foo = foo;
this.timeunit = timeunit;
return this;
}
Expand All @@ -149,6 +152,15 @@ void test() {
a.method(100, 1.0f);
}
}
""", """
import java.util.concurrent.TimeUnit;

class B {
void test() {
A a = new A();
a.method(100, 1.0f, TimeUnit.MILLISECONDS);
}
}
""")
);
}
Expand Down
Loading