-
Notifications
You must be signed in to change notification settings - Fork 76
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
Apache Http Client 5.x Use Timeout and TimeValue classes #399
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6deda77
first approach of HttpClient5 migration
9fbc2a0
added header
0156149
removed redundant parameter
9b22f47
added a basic test
2e196b0
Add test for dependency & fix compilation on Java before/after
timtebeek 13cf4f7
Rename test class to reflect widened scope
timtebeek 7ff921b
removing deprecated changes until further research
b916ebb
Added UseTimeout and UseTimeValue
257b4c7
added classpath jars
d294d8e
add license headers
b87fdad
Merge branch 'main' into httpclient/use-timeout-timevalue
c5328bd
autoformat
77504ac
performance
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of curiosity: Do these methods actually exist? Or only when we're halfway through a migration with change package having partially updated the classes such that we use the new package here, but the methods taking an
int
as an argument do not actually exist?I'm wondering if the 4.x deprecated methods already had replacements taking a
Timeout
, which we could fully migrate in the 4.x line instead, before we even start a migration to 5.x. That could make the recipes more broadly applicable, and applied in isolation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, in 4.x all timeouts and tiemvalues are expressed with integers in milliseconds, and on 5.x there are two options, using Timeout and TimeValue classes, or long + TimeUnit... So, those methods we are matching here does not actually exists in the classes in the namespace, but are "half-way" partially updated mapped classes to 5.x