Skip to content

Commit

Permalink
Adopt classpathFromResources & add tomcat-embedded-core
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Sep 24, 2024
1 parent 534b043 commit 2c06389
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ recipeDependencies {

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

parserClasspath("org.apache.tomcat.embed:tomcat-embed-core:10.1.+")
}

val rewriteVersion = rewriteRecipe.rewriteVersion.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
//noinspection DataFlowIssue
J.ClassDeclaration newClassDeclaration = JavaTemplate.builder(tmpl.toString())
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpath("micrometer-commons", "spring-web", "tomcat-embed-core"))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons", "spring-web", "tomcat-embed-core"))
.imports(DEFAULTSERVERREQUESTOBSERVATIONCONVENTION_FQ, KEYVALUES_FQ, HTTPSERVLETREQUEST_FQ, HTTPSERVLETRESPONSE_FQ, SERVERREQUESTOBSERVATIONCONVENTION_FQ)
.build()
.apply(getCursor(), classDecl.getCoordinates().replace());
Expand Down Expand Up @@ -138,15 +138,15 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
m = JavaTemplate.builder("HttpServletResponse response = #{any()}.getResponse();")
.imports(HTTPSERVLETRESPONSE_FQ, SERVERREQUESTOBSERVATIONCONVENTION_FQ)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpath("tomcat-embed-core", "spring-web-6.+", "micrometer-commons", "micrometer-observation"))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tomcat-embed-core", "spring-web-6.+", "micrometer-commons", "micrometer-observation"))
.build()
.apply(updateCursor(m), m.getBody().getCoordinates().firstStatement(), methodParamIdentifier);
}
if (Boolean.TRUE.equals(addHttpServletRequest)) {
m = JavaTemplate.builder("HttpServletRequest request = #{any()}.getCarrier();")
.imports(HTTPSERVLETREQUEST_FQ, SERVERREQUESTOBSERVATIONCONVENTION_FQ, "io.micrometer.observation.transport.ReceiverContext")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpath("tomcat-embed-core", "spring-web-6.+", "micrometer-commons", "micrometer-observation"))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tomcat-embed-core", "spring-web-6.+", "micrometer-commons", "micrometer-observation"))
.build()
.apply(updateCursor(m), m.getBody().getCoordinates().firstStatement(), methodParamIdentifier);
}
Expand Down Expand Up @@ -193,7 +193,7 @@ private Statement refactorTagsUsage(ExecutionContext ctx, J.Assignment a) {
.apply(getCursor(), a.getCoordinates().replace(), init.getArguments().get(0), init.getArguments().get(1));
return JavaTemplate.builder("#{any()}.and(#{any(io.micrometer.common.KeyValue)})")
.imports(KEYVALUES_FQ)
.javaParser(JavaParser.fromJavaVersion().classpath("micrometer-commons"))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons"))
.build()
.apply(getCursor(), a.getCoordinates().replace(), returnIdentifier, createKeyValue);
} else if (TAGS_AND_STRING_ARRAY.matches(init)) {
Expand All @@ -208,7 +208,7 @@ private Statement refactorTagsUsage(ExecutionContext ctx, J.Assignment a) {
String keyValueVarArg = "#{any(io.micrometer.common.KeyValue)}";
String keyValueVarArgsCombined = String.join(", ", Collections.nCopies(createKeys.size(), keyValueVarArg));
return JavaTemplate.builder("values.and(" + keyValueVarArgsCombined + ")")
.javaParser(JavaParser.fromJavaVersion().classpath("micrometer-commons"))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons"))
.build()
.apply(getCursor(), a.getCoordinates().replace(), createKeys.toArray());
} else if (TAGS_AND_TAG_ARRAY.matches(init)) {
Expand All @@ -217,7 +217,7 @@ private Statement refactorTagsUsage(ExecutionContext ctx, J.Assignment a) {
" values.and(KeyValue.of(tag.getKey(), tag.getValue()));\n" +
"}\n";
return JavaTemplate.builder(template)
.javaParser(JavaParser.fromJavaVersion().classpath("micrometer-commons"))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons"))
.doAfterVariableSubstitution(System.out::println)
.build()
.apply(getCursor(), a.getCoordinates().replace(), iterable);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ class MigrateWebMvcTagsToObservationConventionTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new MigrateWebMvcTagsToObservationConvention()).parser(JavaParser.fromJavaVersion().classpath("micrometer-core", "spring-boot", "spring-context", "spring-beans", "spring-web", "tomcat-embed-core"));
spec.recipe(new MigrateWebMvcTagsToObservationConvention()).parser(JavaParser.fromJavaVersion().classpath(
"micrometer-core",
"spring-boot",
"spring-context",
"spring-beans",
"spring-web",
"tomcat-embed-core"));
}

@DocumentExample
Expand Down Expand Up @@ -94,7 +100,9 @@ public KeyValues getLowCardinalityKeyValues(ServerRequestObservationContext cont
return values;
}
}
"""));
"""
)
);
}

}

0 comments on commit 2c06389

Please sign in to comment.