From 44d79d29b7bed57264c7e91655872d4b239ba6dd Mon Sep 17 00:00:00 2001 From: sullis Date: Wed, 9 Oct 2024 09:14:09 -0400 Subject: [PATCH] migrate Spring annotations to Jspecify (#572) Co-authored-by: Tim te Beek --- build.gradle.kts | 1 + .../resources/META-INF/rewrite/jspecify.yml | 22 ++++ .../jspecify/MigrateToJspecifyTest.java | 103 +++++++++++++++++- 3 files changed, 120 insertions(+), 6 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 37a30aa1fc..968201e6ee 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -56,6 +56,7 @@ dependencies { testRuntimeOnly("com.fasterxml.jackson.core:jackson-databind") testRuntimeOnly("org.codehaus.groovy:groovy:latest.release") testRuntimeOnly("jakarta.annotation:jakarta.annotation-api:2.1.1") + testRuntimeOnly("org.springframework:spring-core:6.1.13") testRuntimeOnly("com.google.code.findbugs:jsr305:3.0.2") testRuntimeOnly(gradleApi()) } diff --git a/src/main/resources/META-INF/rewrite/jspecify.yml b/src/main/resources/META-INF/rewrite/jspecify.yml index d3ee6e1164..2448245d0d 100644 --- a/src/main/resources/META-INF/rewrite/jspecify.yml +++ b/src/main/resources/META-INF/rewrite/jspecify.yml @@ -26,6 +26,7 @@ recipeList: - org.openrewrite.java.jspecify.MigrateFromJavaxAnnotationApi - org.openrewrite.java.jspecify.MigrateFromJakartaAnnotationApi - org.openrewrite.java.jspecify.MigrateFromJetbrainsAnnotations + - org.openrewrite.java.jspecify.MigrateFromSpringFrameworkAnnotations --- type: specs.openrewrite.org/v1beta/recipe @@ -90,3 +91,24 @@ recipeList: ignoreDefinition: true - org.openrewrite.staticanalysis.java.MoveFieldAnnotationToType: annotationType: org.jspecify.annotations.* +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.jspecify.MigrateFromSpringFrameworkAnnotations +displayName: Migrate from Spring Framework annotations to JSpecify +description: Migrate from Spring Framework annotations to JSpecify. +recipeList: + - org.openrewrite.java.dependencies.AddDependency: + groupId: org.jspecify + artifactId: jspecify + version: 1.0.0 + onlyIfUsing: org.springframework.lang.*ull* + - org.openrewrite.java.ChangeType: + oldFullyQualifiedTypeName: org.springframework.lang.Nullable + newFullyQualifiedTypeName: org.jspecify.annotations.Nullable + ignoreDefinition: true + - org.openrewrite.java.ChangeType: + oldFullyQualifiedTypeName: org.springframework.lang.NonNull + newFullyQualifiedTypeName: org.jspecify.annotations.NonNull + ignoreDefinition: true + - org.openrewrite.staticanalysis.java.MoveFieldAnnotationToType: + annotationType: org.jspecify.annotations.* diff --git a/src/test/java/org/openrewrite/java/migrate/jspecify/MigrateToJspecifyTest.java b/src/test/java/org/openrewrite/java/migrate/jspecify/MigrateToJspecifyTest.java index fa963d2891..5861aa04af 100644 --- a/src/test/java/org/openrewrite/java/migrate/jspecify/MigrateToJspecifyTest.java +++ b/src/test/java/org/openrewrite/java/migrate/jspecify/MigrateToJspecifyTest.java @@ -31,7 +31,7 @@ class MigrateToJspecifyTest implements RewriteTest { public void defaults(RecipeSpec spec) { spec .recipeFromResource("/META-INF/rewrite/jspecify.yml", "org.openrewrite.java.jspecify.MigrateToJspecify") - .parser(JavaParser.fromJavaVersion().classpath("jsr305", "jakarta.annotation-api", "annotations")); + .parser(JavaParser.fromJavaVersion().classpath("jsr305", "jakarta.annotation-api", "annotations", "spring-core")); } @DocumentExample @@ -45,7 +45,7 @@ void migrateFromJavaxAnnotationApiToJspecify() { """ import javax.annotation.Nonnull; import javax.annotation.Nullable; - + public class Test { @Nonnull public String field1; @@ -54,7 +54,7 @@ public class Test { @Nullable public Foo.Bar foobar; } - + interface Foo { class Bar { @Nonnull @@ -65,16 +65,16 @@ class Bar { """ import org.jspecify.annotations.NonNull; import org.jspecify.annotations.Nullable; - + public class Test { @NonNull public String field1; @Nullable public String field2; - + public Foo.@Nullable Bar foobar; } - + interface Foo { class Bar { @NonNull @@ -308,4 +308,95 @@ class Bar { ) ); } + + @Test + void migrateFromSpringFrameworkAnnotationsToJspecify() { + rewriteRun( + mavenProject("foo", + //language=java + srcMainJava( + java( + """ + import org.springframework.lang.NonNull; + import org.springframework.lang.Nullable; + + public class Test { + @NonNull + public String field1; + @Nullable + public String field2; + @Nullable + public Foo.Bar foobar; + } + + interface Foo { + class Bar { + @NonNull + public String barField; + } + } + """, + """ + import org.jspecify.annotations.NonNull; + import org.jspecify.annotations.Nullable; + + public class Test { + @NonNull + public String field1; + @Nullable + public String field2; + + public Foo.@Nullable Bar foobar; + } + + interface Foo { + class Bar { + @NonNull + public String barField; + } + } + """ + ) + ), + //language=xml + pomXml( + """ + + 4.0.0 + com.example.foobar + foobar-core + 1.0.0 + + + org.springframework + spring-core + 6.1.13 + + + + """, + """ + + 4.0.0 + com.example.foobar + foobar-core + 1.0.0 + + + org.jspecify + jspecify + 1.0.0 + + + org.springframework + spring-core + 6.1.13 + + + + """ + ) + ) + ); + } }