From 191a3c485937ef376eeaa944600ea3164590d07b Mon Sep 17 00:00:00 2001 From: Guillaume HUSTA Date: Tue, 16 Apr 2024 12:50:58 +0200 Subject: [PATCH 1/4] feat: Missing "Add explicit Common Annotations dependencies" in "Migrate to Java 11" Refs: #455 --- build.gradle.kts | 1 + .../add-common-annotations-dependencies.yml | 47 ++++++++ .../META-INF/rewrite/java-version-11.yml | 1 + .../AddCommonAnnotationsDependenciesTest.java | 106 ++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml create mode 100644 src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java diff --git a/build.gradle.kts b/build.gradle.kts index 7a5213e84e..81a5e8716e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -51,5 +51,6 @@ dependencies { testRuntimeOnly("com.fasterxml.jackson.core:jackson-core") testRuntimeOnly("com.fasterxml.jackson.core:jackson-databind") testRuntimeOnly("org.codehaus.groovy:groovy:latest.release") + testRuntimeOnly("jakarta.annotation:jakarta.annotation-api:1.3.5") testRuntimeOnly(gradleApi()) } diff --git a/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml b/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml new file mode 100644 index 0000000000..c13d86ef51 --- /dev/null +++ b/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml @@ -0,0 +1,47 @@ +# +# Copyright 2024 the original author or authors. +#

+# 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 +#

+# https://www.apache.org/licenses/LICENSE-2.0 +#

+# 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. +# +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies +displayName: Add explicit Common Annotations dependencies +description: > + This recipe will add the necessary `annotation-api` dependency from Jakarta EE 8 to maintain compatibility with Java + version 11 or greater. +tags: + - javax + - java11 + - jsr250 + - jakarta +recipeList: + # Add or update the jakarta.annotation-api to a maven project. This artifact still uses the javax.annotation name space. + - org.openrewrite.java.dependencies.ChangeDependency: + oldGroupId: javax.annotation + oldArtifactId: javax.annotation-api + newGroupId: jakarta.annotation + newArtifactId: jakarta.annotation-api + newVersion: 1.3.x + - org.openrewrite.maven.ChangeManagedDependencyGroupIdAndArtifactId: + oldGroupId: javax.annotation + oldArtifactId: javax.annotation-api + newGroupId: jakarta.annotation + newArtifactId: jakarta.annotation-api + newVersion: 1.3.x + - org.openrewrite.java.dependencies.AddDependency: + groupId: jakarta.annotation + artifactId: jakarta.annotation-api + version: 1.3.x + onlyIfUsing: javax.annotation..* + acceptTransitive: true \ No newline at end of file diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index fe747eb623..c0f1a4d7e4 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -37,6 +37,7 @@ recipeList: - org.openrewrite.java.migrate.javax.AddJaxbDependencies - org.openrewrite.java.migrate.javax.AddJaxwsDependencies - org.openrewrite.java.migrate.javax.AddInjectDependencies + - org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies # Remediate deprecations - org.openrewrite.staticanalysis.BigDecimalRoundingConstantsToEnums - org.openrewrite.staticanalysis.PrimitiveWrapperClassConstructorToValueOf diff --git a/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java b/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java new file mode 100644 index 0000000000..699a0dea70 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java @@ -0,0 +1,106 @@ +package org.openrewrite.java.migrate.javax; + +import org.junit.jupiter.api.Test; +import org.openrewrite.config.Environment; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.java.Assertions.javaVersion; +import static org.openrewrite.java.Assertions.mavenProject; +import static org.openrewrite.java.Assertions.srcMainJava; +import static org.openrewrite.java.Assertions.version; +import static org.openrewrite.maven.Assertions.pomXml; + +@SuppressWarnings("LanguageMismatch") +class AddCommonAnnotationsDependenciesTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe( + Environment.builder() + .scanYamlResources() + .build() + .activateRecipes("org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies")) + .allSources(src -> src.markers(javaVersion(8))); + } + + @Test + void addDependencyIfAnnotationJsr250Present() { + rewriteRun( + spec -> spec.parser(JavaParser.fromJavaVersion().classpath("jakarta.annotation-api")), + mavenProject("my-project", + //language=java + srcMainJava(version(java(""" + import javax.annotation.Generated; + + @Generated("Hello") + class A { + } + """), 8)), + //language=xml + pomXml(""" + + + 4.0.0 + org.sample + sample + 1.0.0 + + + + + + """, + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + + jakarta.annotation + jakarta.annotation-api + 1.3.5 + + + + + """) + ) + ); + } + + @Test + void dontAddDependencyWhenAnnotationJsr250Absent() { + rewriteRun( + mavenProject("my-project", + //language=java + srcMainJava(java(""" + class A { + } + """, + src -> src.markers(javaVersion(8)))), + //language=xml + pomXml(""" + + + 4.0.0 + org.sample + sample + 1.0.0 + + + + + + """) + ) + ); + } + +} From f106fa39ba342fd6446d921c9c0f4c4317702ff3 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 20 Apr 2024 18:59:54 +0200 Subject: [PATCH 2/4] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../AddCommonAnnotationsDependenciesTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java b/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java index 699a0dea70..927863615e 100644 --- a/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java +++ b/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.migrate.javax; import org.junit.jupiter.api.Test; From 71554d56a155c0f7bcb844fe529d6d398fa3ddf3 Mon Sep 17 00:00:00 2001 From: Guillaume Husta Date: Mon, 22 Apr 2024 10:11:44 +0200 Subject: [PATCH 3/4] Apply suggestions from code review Co-authored-by: Tim te Beek --- .../rewrite/add-common-annotations-dependencies.yml | 9 +-------- .../javax/AddCommonAnnotationsDependenciesTest.java | 1 - 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml b/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml index c13d86ef51..fd20c35fd8 100644 --- a/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml +++ b/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml @@ -18,8 +18,7 @@ type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies displayName: Add explicit Common Annotations dependencies description: > - This recipe will add the necessary `annotation-api` dependency from Jakarta EE 8 to maintain compatibility with Java - version 11 or greater. + Add the necessary `annotation-api` dependency from Jakarta EE 8 to maintain compatibility with Java version 11 or greater. tags: - javax - java11 @@ -33,12 +32,6 @@ recipeList: newGroupId: jakarta.annotation newArtifactId: jakarta.annotation-api newVersion: 1.3.x - - org.openrewrite.maven.ChangeManagedDependencyGroupIdAndArtifactId: - oldGroupId: javax.annotation - oldArtifactId: javax.annotation-api - newGroupId: jakarta.annotation - newArtifactId: jakarta.annotation-api - newVersion: 1.3.x - org.openrewrite.java.dependencies.AddDependency: groupId: jakarta.annotation artifactId: jakarta.annotation-api diff --git a/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java b/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java index 927863615e..4f118f28a6 100644 --- a/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java +++ b/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java @@ -28,7 +28,6 @@ import static org.openrewrite.java.Assertions.version; import static org.openrewrite.maven.Assertions.pomXml; -@SuppressWarnings("LanguageMismatch") class AddCommonAnnotationsDependenciesTest implements RewriteTest { @Override From e46e6aa0a531edc0275a7e748f5edac423c0c10d Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 22 Apr 2024 14:00:11 +0200 Subject: [PATCH 4/4] Minor touch ups --- .../add-common-annotations-dependencies.yml | 2 +- .../rewrite/add-inject-dependencies.yml | 4 +- .../AddCommonAnnotationsDependenciesTest.java | 74 +++++-------------- 3 files changed, 21 insertions(+), 59 deletions(-) diff --git a/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml b/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml index fd20c35fd8..40d42df6e9 100644 --- a/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml +++ b/src/main/resources/META-INF/rewrite/add-common-annotations-dependencies.yml @@ -37,4 +37,4 @@ recipeList: artifactId: jakarta.annotation-api version: 1.3.x onlyIfUsing: javax.annotation..* - acceptTransitive: true \ No newline at end of file + acceptTransitive: true diff --git a/src/main/resources/META-INF/rewrite/add-inject-dependencies.yml b/src/main/resources/META-INF/rewrite/add-inject-dependencies.yml index 183fdff99f..da3dd7edeb 100644 --- a/src/main/resources/META-INF/rewrite/add-inject-dependencies.yml +++ b/src/main/resources/META-INF/rewrite/add-inject-dependencies.yml @@ -18,8 +18,7 @@ type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.migrate.javax.AddInjectDependencies displayName: Add explicit Inject dependencies description: > - This recipe will add the necessary `inject-api` dependency from Jakarta EE 8 to maintain compatibility with Java - version 11 or greater. + Add the necessary `inject-api` dependency from Jakarta EE 8 to maintain compatibility with Java version 11 or greater. tags: - javax - java11 @@ -33,7 +32,6 @@ recipeList: version: 1.0.3 onlyIfUsing: javax.inject.* acceptTransitive: true - - org.openrewrite.java.dependencies.UpgradeDependencyVersion: groupId: jakarta.inject artifactId: jakarta.inject-api diff --git a/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java b/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java index 4f118f28a6..e6575b925d 100644 --- a/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java +++ b/src/test/java/org/openrewrite/java/migrate/javax/AddCommonAnnotationsDependenciesTest.java @@ -16,27 +16,20 @@ package org.openrewrite.java.migrate.javax; import org.junit.jupiter.api.Test; -import org.openrewrite.config.Environment; import org.openrewrite.java.JavaParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; -import static org.openrewrite.java.Assertions.java; -import static org.openrewrite.java.Assertions.javaVersion; -import static org.openrewrite.java.Assertions.mavenProject; -import static org.openrewrite.java.Assertions.srcMainJava; -import static org.openrewrite.java.Assertions.version; +import static org.openrewrite.java.Assertions.*; import static org.openrewrite.maven.Assertions.pomXml; class AddCommonAnnotationsDependenciesTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec.recipe( - Environment.builder() - .scanYamlResources() - .build() - .activateRecipes("org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies")) + spec.recipeFromResource( + "/META-INF/rewrite/add-common-annotations-dependencies.yml", + "org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies") .allSources(src -> src.markers(javaVersion(8))); } @@ -46,25 +39,26 @@ void addDependencyIfAnnotationJsr250Present() { spec -> spec.parser(JavaParser.fromJavaVersion().classpath("jakarta.annotation-api")), mavenProject("my-project", //language=java - srcMainJava(version(java(""" - import javax.annotation.Generated; - - @Generated("Hello") - class A { - } - """), 8)), + srcMainJava( + java( + """ + import javax.annotation.Generated; + + @Generated("Hello") + class A { + } + """ + ) + ), //language=xml - pomXml(""" + pomXml( + """ 4.0.0 org.sample sample 1.0.0 - - - - """, """ @@ -74,7 +68,6 @@ class A { org.sample sample 1.0.0 - jakarta.annotation @@ -82,39 +75,10 @@ class A { 1.3.5 - - """) - ) - ); - } - - @Test - void dontAddDependencyWhenAnnotationJsr250Absent() { - rewriteRun( - mavenProject("my-project", - //language=java - srcMainJava(java(""" - class A { - } - """, - src -> src.markers(javaVersion(8)))), - //language=xml - pomXml(""" - - - 4.0.0 - org.sample - sample - 1.0.0 - - - - - - """) + """ + ) ) ); } - }