Skip to content

Commit

Permalink
migrate Spring annotations to Jspecify (#572)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim te Beek <tim@moderne.io>
  • Loading branch information
sullis and timtebeek authored Oct 9, 2024
1 parent f90a2ba commit 44d79d2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 6 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/META-INF/rewrite/jspecify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.*
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,7 +45,7 @@ void migrateFromJavaxAnnotationApiToJspecify() {
"""
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class Test {
@Nonnull
public String field1;
Expand All @@ -54,7 +54,7 @@ public class Test {
@Nullable
public Foo.Bar foobar;
}
interface Foo {
class Bar {
@Nonnull
Expand All @@ -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
Expand Down Expand Up @@ -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(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.foobar</groupId>
<artifactId>foobar-core</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.1.13</version>
</dependency>
</dependencies>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.foobar</groupId>
<artifactId>foobar-core</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.jspecify</groupId>
<artifactId>jspecify</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.1.13</version>
</dependency>
</dependencies>
</project>
"""
)
)
);
}
}

0 comments on commit 44d79d2

Please sign in to comment.