Skip to content

Commit

Permalink
Adding new migrations for HTTP5 Client
Browse files Browse the repository at this point in the history
  • Loading branch information
ammachado committed Apr 13, 2024
1 parent c4da55f commit 6567fbd
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.List;
import java.util.concurrent.TimeUnit;


@Value
@EqualsAndHashCode(callSuper = false)
public class AddTimeUnitArgument extends Recipe {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,52 @@
*/
package org.openrewrite.apache.httpclient5;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

@Value
@EqualsAndHashCode(callSuper = false)
public class MigrateAuthScope extends Recipe {

public class NewStatusLine extends Recipe {
@Override
public String getDisplayName() {
return "Replaces deprecated `HttpResponse::getStatusLine()`";
return "Replaces `AuthScope.ANY`";
}

@Override
public String getDescription() {
return "`HttpResponse::getStatusLine()` was deprecated in 4.x, so we replace it for `new StatusLine(HttpResponse)`. " +
"Ideally we will try to simplify method chains for `getStatusCode`, `getProtocolVersion` and `getReasonPhrase`, " +
"but there are some scenarios where the `StatusLine` object is assigned or used directly, and we need to " +
"instantiate the object.";
return "Replace removed constant `org.apache.http.auth.AuthScope.AuthScope.ANY` with `new org.apache.hc.client5.http.auth.AuthScope(null, -1)`";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaVisitor<ExecutionContext>() {
final MethodMatcher matcher = new MethodMatcher("org.apache.hc.core5.http.HttpResponse getStatusLine()");

@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (matcher.matches(m)) {
maybeAddImport("org.apache.hc.core5.http.message.StatusLine");
return JavaTemplate.builder("new StatusLine(#{any(org.apache.hc.core5.http.HttpResponse)})")
.imports("org.apache.hc.core5.http.message.StatusLine")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "httpcore5"))
.build()
.apply(updateCursor(m), m.getCoordinates().replace(), m.getSelect());
}
return m;
}
};
return Preconditions.check(
new UsesType<>("org.apache.hc.client5.http.auth.AuthScope", false),
new JavaVisitor<ExecutionContext>() {
@Override
public J visitFieldAccess(J.FieldAccess fieldAccess, ExecutionContext ctx) {
J.FieldAccess f = (J.FieldAccess) super.visitFieldAccess(fieldAccess, ctx);
// Type was already relocated by previous recipe
if ("ANY".equals(f.getSimpleName()) && TypeUtils.isOfClassType(f.getTarget().getType(), "org.apache.hc.client5.http.auth.AuthScope")) {
maybeAddImport("org.apache.hc.client5.http.auth.AuthScope");
return JavaTemplate.builder("new AuthScope(null, -1)")
.imports("org.apache.hc.client5.http.auth.AuthScope")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "httpcore5"))
.build()
.apply(updateCursor(f), f.getCoordinates().replace());
}
return f;
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.openrewrite.apache.httpclient5;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
Expand All @@ -24,7 +26,10 @@
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.J;

@Value
@EqualsAndHashCode(callSuper = false)
public class NewStatusLine extends Recipe {

@Override
public String getDisplayName() {
return "Replaces deprecated `HttpResponse::getStatusLine()`";
Expand Down
16 changes: 16 additions & 0 deletions src/main/resources/META-INF/rewrite/apache-httpclient-5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ recipeList:
- org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_DeprecatedMethods
- org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_TimeUnit
- org.openrewrite.apache.httpclient5.StatusLine
- org.openrewrite.apache.httpclient5.MigrateAuthScope
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_ClassMapping
Expand Down Expand Up @@ -93,6 +94,9 @@ recipeList:
oldPackageName: org.apache.http.impl.client
newPackageName: org.apache.hc.client5.http.impl.classic
# Fixing specific mappings
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.http.impl.client.BasicAuthCache
newFullyQualifiedTypeName: org.apache.hc.client5.http.impl.auth.BasicAuthCache
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.hc.client5.http.impl.classic.BasicAuthCache
newFullyQualifiedTypeName: org.apache.hc.client5.http.impl.auth.BasicAuthCache
Expand Down Expand Up @@ -379,6 +383,12 @@ recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.hc.client5.http.impl.io.DefaultHttpResponseParser
newFullyQualifiedTypeName: org.apache.hc.core5.http.impl.io.DefaultHttpResponseParser
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.hc.client5.http.impl.io.PoolingClientConnectionManager
newFullyQualifiedTypeName: org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.hc.client5.http.impl.SchemeRegistryFactory
newFullyQualifiedTypeName: org.apache.hc.client5.http.impl.DefaultSchemePortResolver

- org.openrewrite.java.ChangePackage:
oldPackageName: org.apache.http.conn
Expand Down Expand Up @@ -425,6 +435,12 @@ recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.hc.core5.http.HttpConnectionFactory
newFullyQualifiedTypeName: org.apache.hc.core5.http.io.HttpConnectionFactory
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.http.HttpRequest
newFullyQualifiedTypeName: org.apache.hc.core5.http.ClassicHttpRequest
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.apache.http.HttpResponse
newFullyQualifiedTypeName: org.apache.hc.core5.http.ClassicHttpResponse
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5_DeprecatedMethods
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,52 @@
import static org.junit.jupiter.api.Assertions.*;
class MigrateAuthScopeTest {

package org.openrewrite.apache.httpclient5;

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.config.Environment;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.TypeValidation;

import static org.openrewrite.java.Assertions.java;

class MigrateAuthScopeTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpath("httpclient", "httpcore", "httpclient5", "httpcore5"))
.afterTypeValidationOptions(TypeValidation.none())
.recipe(Environment.builder()
.scanRuntimeClasspath("org.openrewrite")
.build()
.activateRecipes("org.openrewrite.apache.httpclient5.UpgradeApacheHttpClient_5")
);
}

@DocumentExample
@Test
void authScopeAnyTest() {
rewriteRun(
//language=java
java(
"""
import org.apache.http.auth.AuthScope;
class A {
void method() {
AuthScope any = AuthScope.ANY;
}
}
""", """
import org.apache.hc.client5.http.auth.AuthScope;
class A {
void method() {
AuthScope any = new AuthScope(null, -1);
}
}
"""
)
);
}
}

0 comments on commit 6567fbd

Please sign in to comment.