diff --git a/build.gradle.kts b/build.gradle.kts index 5562e582e..ba23e5d85 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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() @@ -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.+") diff --git a/src/main/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConvention.java b/src/main/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConvention.java index d458d08e8..205f67cf1 100644 --- a/src/main/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConvention.java +++ b/src/main/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConvention.java @@ -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()); @@ -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; @@ -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); } @@ -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; } } @@ -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 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 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; } diff --git a/src/main/resources/META-INF/rewrite/classpath/jakarta.servlet-api-6.1.0.jar b/src/main/resources/META-INF/rewrite/classpath/jakarta.servlet-api-6.1.0.jar new file mode 100644 index 000000000..ea475fa9d Binary files /dev/null and b/src/main/resources/META-INF/rewrite/classpath/jakarta.servlet-api-6.1.0.jar differ diff --git a/src/main/resources/META-INF/rewrite/classpath/tomcat-embed-core-10.1.30.jar b/src/main/resources/META-INF/rewrite/classpath/tomcat-embed-core-10.1.30.jar deleted file mode 100644 index c5e169891..000000000 Binary files a/src/main/resources/META-INF/rewrite/classpath/tomcat-embed-core-10.1.30.jar and /dev/null differ diff --git a/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConventionTest.java b/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConventionTest.java index 54706df95..4ef2d88eb 100644 --- a/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConventionTest.java +++ b/src/testWithSpringBoot_3_0/java/org/openrewrite/java/spring/boot3/MigrateWebMvcTagsToObservationConventionTest.java @@ -33,7 +33,7 @@ public void defaults(RecipeSpec spec) { "spring-context", "spring-beans", "spring-web", - "tomcat-embed-core")); + "jakarta.servlet-api")); } @DocumentExample