-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apache Http Client 5.x Use Timeout and TimeValue classes (#399)
- Loading branch information
Joan Viladrosa
authored
Jul 31, 2023
1 parent
0c20814
commit e95a680
Showing
8 changed files
with
269 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -7,3 +7,5 @@ bin/ | |
.project | ||
.classpath | ||
.settings/ | ||
.DS_Store | ||
.moderne/ |
80 changes: 80 additions & 0 deletions
80
src/main/java/org/openrewrite/java/apache/httpclient5/UseTimeValue.java
This file contains 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,80 @@ | ||
/* | ||
* 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 org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class UseTimeValue extends Recipe { | ||
@Override | ||
public String getDisplayName() { | ||
return "Use `TimeValue` class to define time values (duration)"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Use `TimeValue` class to define time values (duration)."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check(new UsesType<>("org.apache.hc..*", true), new JavaIsoVisitor<ExecutionContext>() { | ||
|
||
List<MethodMatcher> methodMatchers = Arrays.asList( | ||
new MethodMatcher("org.apache.hc.core5.http.io.SocketConfig.Builder setSoLinger(int)") | ||
); | ||
|
||
JavaTemplate template = JavaTemplate.builder("TimeValue.ofMilliseconds(#{})") | ||
.contextSensitive() | ||
.javaParser(JavaParser.fromJavaVersion().classpath( | ||
"httpclient5", "httpcore5" | ||
)) | ||
.imports("org.apache.hc.core5.util.TimeValue") | ||
.build(); | ||
|
||
@Override | ||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
J.MethodInvocation m = super.visitMethodInvocation(method, ctx); | ||
|
||
if (methodMatches(m)) { | ||
m = template.apply(updateCursor(m), m.getCoordinates().replaceArguments(), m.getArguments().get(0)); | ||
maybeAddImport("org.apache.hc.core5.util.TimeValue"); | ||
} | ||
return m; | ||
} | ||
|
||
private boolean methodMatches(J.MethodInvocation method) { | ||
for (MethodMatcher matcher : methodMatchers) { | ||
if (matcher.matches(method)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/main/java/org/openrewrite/java/apache/httpclient5/UseTimeout.java
This file contains 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,84 @@ | ||
/* | ||
* 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 org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.MethodMatcher; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
|
||
public class UseTimeout extends Recipe { | ||
@Override | ||
public String getDisplayName() { | ||
return "Use `Timeout` class to define timeouts"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Use Timeout class to define timeouts."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check(new UsesType<>("org.apache.hc..*", true), new JavaIsoVisitor<ExecutionContext>() { | ||
|
||
final List<MethodMatcher> methodMatchers = Arrays.asList( | ||
new MethodMatcher("org.apache.hc.client5.http.config.RequestConfig.Builder setConnectionRequestTimeout(int)"), | ||
new MethodMatcher("org.apache.hc.client5.http.config.RequestConfig.Builder setConnectTimeout(int)"), | ||
new MethodMatcher("org.apache.hc.client5.http.config.RequestConfig.Builder setResponseTimeout(int)"), | ||
new MethodMatcher("org.apache.hc.core5.http.io.SocketConfig.Builder setSoTimeout(int)") | ||
); | ||
|
||
final JavaTemplate template = JavaTemplate.builder("Timeout.ofMilliseconds(#{})") | ||
.contextSensitive() | ||
.javaParser(JavaParser.fromJavaVersion().classpath( | ||
"httpclient5", "httpcore5" | ||
)) | ||
.imports("org.apache.hc.core5.util.Timeout") | ||
.build(); | ||
|
||
@Override | ||
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
J.MethodInvocation m = super.visitMethodInvocation(method, ctx); | ||
|
||
if (methodMatches(m)) { | ||
m = template.apply(updateCursor(m), m.getCoordinates().replaceArguments(), m.getArguments().get(0)); | ||
maybeAddImport("org.apache.hc.core5.util.Timeout"); | ||
} | ||
return m; | ||
} | ||
|
||
private boolean methodMatches(J.MethodInvocation method) { | ||
for (MethodMatcher matcher : methodMatchers) { | ||
if (matcher.matches(method)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/openrewrite/java/apache/httpclient5/package-info.java
This file contains 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,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
@NonNullApi @NonNullFields | ||
package org.openrewrite.java.apache.httpclient5; | ||
|
||
import org.openrewrite.internal.lang.NonNullApi; | ||
import org.openrewrite.internal.lang.NonNullFields; |
This file contains 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
Binary file not shown.
Binary file not shown.
This file contains 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