Skip to content

Commit

Permalink
AddTimeUnitArgument recipe (#403)
Browse files Browse the repository at this point in the history
* AddTimeUnitArgument recipe

* accept any number of arguments

* Update src/main/resources/META-INF/rewrite/apache-httpclient-5.yml

Co-authored-by: Raquel Pau <[email protected]>

* Update src/main/resources/META-INF/rewrite/apache-httpclient-5.yml

Co-authored-by: Raquel Pau <[email protected]>

* Update src/main/java/org/openrewrite/java/apache/httpclient5/AddTimeUnitArgument.java

Co-authored-by: Raquel Pau <[email protected]>

* Update src/main/java/org/openrewrite/java/apache/httpclient5/AddTimeUnitArgument.java

Co-authored-by: Raquel Pau <[email protected]>

* Update src/main/java/org/openrewrite/java/apache/httpclient5/AddTimeUnitArgument.java

Co-authored-by: Raquel Pau <[email protected]>

* Update src/main/java/org/openrewrite/java/apache/httpclient5/AddTimeUnitArgument.java

Co-authored-by: Raquel Pau <[email protected]>

* Update src/main/java/org/openrewrite/java/apache/httpclient5/AddTimeUnitArgument.java

Co-authored-by: Raquel Pau <[email protected]>

* fixed test

* format

---------

Co-authored-by: Raquel Pau <[email protected]>
  • Loading branch information
Joan Viladrosa and rpau authored Aug 2, 2023
1 parent a277a21 commit 3c42c95
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 203 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
* <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.apache.httpclient5;

import lombok.EqualsAndHashCode;
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;


@Value
@EqualsAndHashCode(callSuper = true)
public class AddTimeUnitArgument extends Recipe {

@Option(displayName = "Method pattern",
description = "A method pattern that is used to find matching method invocations.",
example = "org.apache.http.client.config.RequestConfig.Builder setConnectionRequestTimeout(int)")
String methodPattern;

@Option(displayName = "Time Unit",
description = "The TimeUnit enum value we want to add to the method invocation. Defaults to `MILLISECONDS`.",
example = "MILLISECONDS",
required = false)
@Nullable
TimeUnit timeUnit;

@Override
public String getDisplayName() {
return "Adds a TimeUnit argument to the matched method invocations";
}

@Override
public String getDescription() {
return "In Apache Http Client 5.x migration, an extra TimeUnit argument is required in the timeout and duration methods. " +
"Previously in 4.x, all these methods were implicitly having the timeout or duration expressed in milliseconds, " +
"but in 5.x the unit of the timeout or duration is required. So, by default this recipe adds " +
"`TimeUnit.MILLISECONDS`, it is possible to specify this as a parameter. Since all affected methods of " +
"the Apache Http Client 5.x migration only have one integer/long argument, the recipe applies with matched method " +
"invocations of exactly one parameter.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
final MethodMatcher matcher = new MethodMatcher(methodPattern);

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
J.MethodInvocation m = super.visitMethodInvocation(method, executionContext);
if (matcher.matches(m)) {
JavaTemplate template = JavaTemplate
.builder(StringUtils.repeat("#{any()}, ", m.getArguments().size()) + "TimeUnit.#{}")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpath("httpclient5", "httpcore5"))
.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(),
arguments.toArray(new Object[0])
);
maybeAddImport("java.util.concurrent.TimeUnit");
}
return m;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {

final MethodMatcher matcher = new MethodMatcher("org.apache.hc.core5.http.HttpResponse getStatusLine()");
final JavaTemplate template = JavaTemplate.builder("new StatusLine(#{any(org.apache.hc.core5.http.HttpResponse)})")
.javaParser(JavaParser.fromJavaVersion().classpath("httpcore5"))
Expand Down

This file was deleted.

This file was deleted.

19 changes: 17 additions & 2 deletions src/main/resources/META-INF/rewrite/apache-httpclient-5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ recipeList:
newVersion: 5.1.x
- org.openrewrite.java.apache.httpclient5.UpgradeApacheHttpClient_5_ClassMapping
- org.openrewrite.java.apache.httpclient5.UpgradeApacheHttpClient_5_DeprecatedMethods
- org.openrewrite.java.apache.httpclient5.UseTimeout
- org.openrewrite.java.apache.httpclient5.UseTimeValue
- org.openrewrite.java.apache.httpclient5.UpgradeApacheHttpClient_5_TimeUnit
- org.openrewrite.java.apache.httpclient5.StatusLine
---
type: specs.openrewrite.org/v1beta/recipe
Expand Down Expand Up @@ -431,6 +430,22 @@ recipeList:
newMethodName: setResponseTimeout
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.apache.httpclient5.UpgradeApacheHttpClient_5_TimeUnit
displayName: Adds `TimeUnit` to timeouts and duration methods
description: Apache HttpClient 5.x Timeout and duration methods need an extra the TimeUnit argument. This recipe uses milliseconds as a default unit.
recipeList:
- org.openrewrite.java.apache.httpclient5.AddTimeUnitArgument:
methodPattern: org.apache.hc.client5.http.config.RequestConfig.Builder setConnectionRequestTimeout(int)
- org.openrewrite.java.apache.httpclient5.AddTimeUnitArgument:
methodPattern: org.apache.hc.client5.http.config.RequestConfig.Builder setConnectTimeout(int)
- org.openrewrite.java.apache.httpclient5.AddTimeUnitArgument:
methodPattern: org.apache.hc.client5.http.config.RequestConfig.Builder setResponseTimeout(int)
- org.openrewrite.java.apache.httpclient5.AddTimeUnitArgument:
methodPattern: org.apache.hc.core5.http.io.SocketConfig.Builder setSoLinger(int)
- org.openrewrite.java.apache.httpclient5.AddTimeUnitArgument:
methodPattern: org.apache.hc.core5.http.io.SocketConfig.Builder setSoTimeout(int)
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.apache.httpclient5.StatusLine
displayName: Migrate to ApacheHttpClient 5.x deprecated methods from 4.x
description: Migrates deprecated methods to their equivalent ones in 5.x
Expand Down
Loading

0 comments on commit 3c42c95

Please sign in to comment.