Skip to content

Commit

Permalink
Apache HttpClient5 recipes should load classpath from resources (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Jan 25, 2024
1 parent 35af6e8 commit 624df94
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 69 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ recipeDependencies {

parserClasspath("com.nimbusds:nimbus-jose-jwt:9.13")
parserClasspath("net.minidev:json-smart:2.4.+")

parserClasspath("org.apache.httpcomponents.core5:httpcore5:5.1.+")
parserClasspath("org.apache.httpcomponents.client5:httpclient5:5.1.+")
}

val rewriteVersion = rewriteRecipe.rewriteVersion.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
JavaTemplate template = JavaTemplate
.builder(StringUtils.repeat("#{any()}, ", m.getArguments().size()) + "TimeUnit.#{}")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpath("httpclient5", "httpcore5"))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "httpclient5", "httpcore5"))
.imports("java.util.concurrent.TimeUnit")
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ public String getDescription() {
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 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void defaults(RecipeSpec spec) {
}

@Test
void testCookieConstantsMapping() {
void cookieConstantsMapping() {
rewriteRun(
//language=java
java(
Expand Down
Loading

0 comments on commit 624df94

Please sign in to comment.