Skip to content

Commit

Permalink
Fix most warnings and issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Laurens-W committed Sep 24, 2024
1 parent 2c06389 commit 2391101
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 47 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ 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.+")
parserClasspath("jakarta.servlet:jakarta.servlet-api:6.1.+")
}

val rewriteVersion = rewriteRecipe.rewriteVersion.get()
Expand Down Expand Up @@ -251,7 +251,7 @@ dependencies {
"testWithSpringBoot_3_0RuntimeOnly"("org.springframework.security:spring-security-config:6.0.+")
"testWithSpringBoot_3_0RuntimeOnly"("org.springframework.security:spring-security-web:6.0.+")
"testWithSpringBoot_3_0RuntimeOnly"("org.springframework.security:spring-security-ldap:6.0.+")
"testWithSpringBoot_3_0RuntimeOnly"("org.apache.tomcat.embed:tomcat-embed-core:10.1.+")
"testWithSpringBoot_3_0RuntimeOnly"("jakarta.servlet:jakarta.servlet-api:6.1.+")

"testWithSpringBoot_3_2RuntimeOnly"("org.springframework.boot:spring-boot-starter:3.2.+")
"testWithSpringBoot_3_2RuntimeOnly"("org.springframework.boot:spring-boot-starter-test:3.2.+")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,9 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
tmpl.append(" return values;");
tmpl.append(" }\n");
tmpl.append("}");
//noinspection DataFlowIssue
J.ClassDeclaration newClassDeclaration = JavaTemplate.builder(tmpl.toString())
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons", "spring-web", "tomcat-embed-core"))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons", "spring-web-6.+", "jakarta.servlet-api"))
.imports(DEFAULTSERVERREQUESTOBSERVATIONCONVENTION_FQ, KEYVALUES_FQ, HTTPSERVLETREQUEST_FQ, HTTPSERVLETRESPONSE_FQ, SERVERREQUESTOBSERVATIONCONVENTION_FQ)
.build()
.apply(getCursor(), classDecl.getCoordinates().replace());
Expand All @@ -114,6 +113,7 @@ public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, Ex
if (stmt instanceof J.MethodDeclaration && ((J.MethodDeclaration) stmt).getSimpleName().equals("getTags")) {
J.MethodDeclaration md = (J.MethodDeclaration) finalNewClassDeclaration.getBody().getStatements().get(0);
md = md.withPrefix(stmt.getPrefix());
//noinspection DataFlowIssue
return md.withBody(md.getBody().withStatements(ListUtils.insertAll(md.getBody().getStatements(), md.getBody().getStatements().size() - 1, getTagsBodyStatements)));
}
return stmt;
Expand All @@ -134,19 +134,19 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
J.Identifier methodParamIdentifier = methodParam.getVariables().get(0).getName();
Boolean addHttpServletResponse = getCursor().pollMessage("addHttpServletResponse");
Boolean addHttpServletRequest = getCursor().pollMessage("addHttpServletRequest");
if (Boolean.TRUE.equals(addHttpServletResponse)) {
if (Boolean.TRUE.equals(addHttpServletResponse) && m.getBody() != null) {
m = JavaTemplate.builder("HttpServletResponse response = #{any()}.getResponse();")
.imports(HTTPSERVLETRESPONSE_FQ, SERVERREQUESTOBSERVATIONCONVENTION_FQ)
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tomcat-embed-core", "spring-web-6.+", "micrometer-commons", "micrometer-observation"))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.servlet-api", "spring-web-6.+", "micrometer-commons", "micrometer-observation"))
.build()
.apply(updateCursor(m), m.getBody().getCoordinates().firstStatement(), methodParamIdentifier);
}
if (Boolean.TRUE.equals(addHttpServletRequest)) {
if (Boolean.TRUE.equals(addHttpServletRequest) && m.getBody() != null) {
m = JavaTemplate.builder("HttpServletRequest request = #{any()}.getCarrier();")
.imports(HTTPSERVLETREQUEST_FQ, SERVERREQUESTOBSERVATIONCONVENTION_FQ, "io.micrometer.observation.transport.ReceiverContext")
.contextSensitive()
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "tomcat-embed-core", "spring-web-6.+", "micrometer-commons", "micrometer-observation"))
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "jakarta.servlet-api", "spring-web-6.+", "micrometer-commons", "micrometer-observation"))
.build()
.apply(updateCursor(m), m.getBody().getCoordinates().firstStatement(), methodParamIdentifier);
}
Expand All @@ -163,6 +163,7 @@ public Statement visitStatement(Statement statement, ExecutionContext ctx) {
if (TAGS_OF_TAG_ARRAY.matches(init)) {
maybeRemoveImport(TAGS_FQ);
maybeRemoveImport(WEBMVCTAGS_FQ);
//noinspection DataFlowIssue
return null;
}
}
Expand All @@ -181,46 +182,50 @@ public Statement visitStatement(Statement statement, ExecutionContext ctx) {
private Statement refactorTagsUsage(ExecutionContext ctx, J.Assignment a) {
J.MethodInvocation init = ((J.MethodInvocation) a.getAssignment());
J.MethodDeclaration insideMethod = getCursor().firstEnclosing(J.MethodDeclaration.class);
J.Identifier returnIdentifier = (J.Identifier) ((J.Return) insideMethod.getBody().getStatements().get(insideMethod.getBody().getStatements().size() - 1)).getExpression();
if (insideMethod != null && insideMethod.getBody()!= null) {
J.Identifier returnIdentifier = (J.Identifier) ((J.Return) insideMethod.getBody().getStatements().get(insideMethod.getBody().getStatements().size() - 1)).getExpression();

maybeAddImport(KEYVALUE_FQ);
maybeRemoveImport(TAG_FQ);
if (TAGS_AND_STRING_STRING.matches(init)) {
J.MethodInvocation createKeyValue = JavaTemplate.builder("KeyValue.of(#{any(java.lang.String)}, #{any(java.lang.String)})")
.imports(KEYVALUE_FQ)
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons-1.11.+"))
.build()
.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().classpathFromResources(ctx, "micrometer-commons"))
.build()
.apply(getCursor(), a.getCoordinates().replace(), returnIdentifier, createKeyValue);
} else if (TAGS_AND_STRING_ARRAY.matches(init)) {
List<J.MethodInvocation> createKeys = new ArrayList<>();
for (int i = 0; i < init.getArguments().size(); i += 2) {
createKeys.add(JavaTemplate.builder("KeyValue.of(#{any(java.lang.String)}, #{any(java.lang.String)})")
.imports(KEYVALUE_FQ)
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons-1.11.+"))
.build()
.apply(getCursor(), a.getCoordinates().replace(), init.getArguments().get(i), init.getArguments().get(i + 1)));
if (returnIdentifier != null) {
maybeAddImport(KEYVALUE_FQ);
maybeRemoveImport(TAG_FQ);
if (TAGS_AND_STRING_STRING.matches(init)) {
J.MethodInvocation createKeyValue = JavaTemplate.builder("KeyValue.of(#{any(java.lang.String)}, #{any(java.lang.String)})")
.imports(KEYVALUE_FQ)
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons-1.11.+"))
.build()
.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().classpathFromResources(ctx, "micrometer-commons"))
.build()
.apply(getCursor(), a.getCoordinates().replace(), returnIdentifier, createKeyValue);
} else if (TAGS_AND_STRING_ARRAY.matches(init)) {
List<J.MethodInvocation> createKeys = new ArrayList<>();
for (int i = 0; i < init.getArguments().size(); i += 2) {
createKeys.add(JavaTemplate.builder("KeyValue.of(#{any(java.lang.String)}, #{any(java.lang.String)})")
.imports(KEYVALUE_FQ)
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons-1.11.+"))
.build()
.apply(getCursor(), a.getCoordinates().replace(), init.getArguments().get(i), init.getArguments().get(i + 1)));
}
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().classpathFromResources(ctx, "micrometer-commons"))
.build()
.apply(getCursor(), a.getCoordinates().replace(), createKeys.toArray());
} else if (TAGS_AND_TAG_ARRAY.matches(init)) {
J.Identifier iterable = (J.Identifier) init.getArguments().get(0);
String template = "for (Tag tag : #{any()}) {\n" +
" values.and(KeyValue.of(tag.getKey(), tag.getValue()));\n" +
"}\n";
return JavaTemplate.builder(template)
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons"))
.doAfterVariableSubstitution(System.out::println)
.build()
.apply(getCursor(), a.getCoordinates().replace(), iterable);
}
}
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().classpathFromResources(ctx, "micrometer-commons"))
.build()
.apply(getCursor(), a.getCoordinates().replace(), createKeys.toArray());
} else if (TAGS_AND_TAG_ARRAY.matches(init)) {
J.Identifier iterable = (J.Identifier) init.getArguments().get(0);
String template = "for (Tag tag : #{any()}) {\n" +
" values.and(KeyValue.of(tag.getKey(), tag.getValue()));\n" +
"}\n";
return JavaTemplate.builder(template)
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "micrometer-commons"))
.doAfterVariableSubstitution(System.out::println)
.build()
.apply(getCursor(), a.getCoordinates().replace(), iterable);
}
return a;
}
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void defaults(RecipeSpec spec) {
"spring-context",
"spring-beans",
"spring-web",
"tomcat-embed-core"));
"jakarta.servlet-api"));
}

@DocumentExample
Expand Down

0 comments on commit 2391101

Please sign in to comment.