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

Replace getStatusLine on HttpResponse #400

Merged
merged 4 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.*;
import org.openrewrite.java.tree.J;

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

@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.";
}

@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"))
.imports("org.apache.hc.core5.http.message.StatusLine")
.build();

@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 template.apply(updateCursor(m), m.getCoordinates().replace(), m.getSelect());
}
return m;
}
};
}
}
17 changes: 17 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 @@ -39,6 +39,7 @@ recipeList:
- 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.StatusLine

---
type: specs.openrewrite.org/v1beta/recipe
Expand Down Expand Up @@ -412,3 +413,19 @@ recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.apache.hc.client5.http.config.RequestConfig.Builder setSocketTimeout(int)
newMethodName: setResponseTimeout
---
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
recipeList:
- org.openrewrite.java.SimplifyMethodChain:
methodPatternChain: ['org.apache.hc.core5.http.HttpResponse getStatusLine()', 'org.apache.hc.core5.http.message.StatusLine getStatusCode()']
newMethodName: getCode
- org.openrewrite.java.SimplifyMethodChain:
methodPatternChain: ['org.apache.hc.core5.http.HttpResponse getStatusLine()', 'org.apache.hc.core5.http.message.StatusLine getReasonPhrase()']
newMethodName: getReasonPhrase
- org.openrewrite.java.SimplifyMethodChain:
methodPatternChain: [ 'org.apache.hc.core5.http.HttpResponse getStatusLine()', 'org.apache.hc.core5.http.message.StatusLine getProtocolVersion()' ]
newMethodName: getVersion
- org.openrewrite.java.apache.httpclient5.NewStatusLine
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,59 @@ void method() {
""")
);
}

@Test
void removeStatusLineHttpResponse() {
rewriteRun(
//language=java
java("""
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ProtocolVersion;

import java.io.IOException;

class A {
void method() throws IOException {
HttpGet httpGet = new HttpGet("https://moderne.io");
CloseableHttpClient instance = HttpClientBuilder.create().build();
CloseableHttpResponse response = instance.execute(httpGet);

System.out.println("response.getStatusLine() :: " + response.getStatusLine());
int statusCode = response.getStatusLine().getStatusCode();
String reason = response.getStatusLine().getReasonPhrase();
ProtocolVersion version = response.getStatusLine().getProtocolVersion();
}
}
""",
"""
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.ProtocolVersion;
import org.apache.hc.core5.http.message.StatusLine;

import java.io.IOException;

class A {
void method() throws IOException {
HttpGet httpGet = new HttpGet("https://moderne.io");
CloseableHttpClient instance = HttpClientBuilder.create().build();
CloseableHttpResponse response = instance.execute(httpGet);

System.out.println("response.getStatusLine() :: " + new StatusLine(response));
int statusCode = response.getCode();
String reason = response.getReasonPhrase();
ProtocolVersion version = response.getVersion();
}
}
""")
);
}

}