Skip to content

Commit 44d79d2

Browse files
sullistimtebeek
andauthored
migrate Spring annotations to Jspecify (#572)
Co-authored-by: Tim te Beek <[email protected]>
1 parent f90a2ba commit 44d79d2

File tree

3 files changed

+120
-6
lines changed

3 files changed

+120
-6
lines changed

build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ dependencies {
5656
testRuntimeOnly("com.fasterxml.jackson.core:jackson-databind")
5757
testRuntimeOnly("org.codehaus.groovy:groovy:latest.release")
5858
testRuntimeOnly("jakarta.annotation:jakarta.annotation-api:2.1.1")
59+
testRuntimeOnly("org.springframework:spring-core:6.1.13")
5960
testRuntimeOnly("com.google.code.findbugs:jsr305:3.0.2")
6061
testRuntimeOnly(gradleApi())
6162
}

src/main/resources/META-INF/rewrite/jspecify.yml

+22
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ recipeList:
2626
- org.openrewrite.java.jspecify.MigrateFromJavaxAnnotationApi
2727
- org.openrewrite.java.jspecify.MigrateFromJakartaAnnotationApi
2828
- org.openrewrite.java.jspecify.MigrateFromJetbrainsAnnotations
29+
- org.openrewrite.java.jspecify.MigrateFromSpringFrameworkAnnotations
2930

3031
---
3132
type: specs.openrewrite.org/v1beta/recipe
@@ -90,3 +91,24 @@ recipeList:
9091
ignoreDefinition: true
9192
- org.openrewrite.staticanalysis.java.MoveFieldAnnotationToType:
9293
annotationType: org.jspecify.annotations.*
94+
---
95+
type: specs.openrewrite.org/v1beta/recipe
96+
name: org.openrewrite.java.jspecify.MigrateFromSpringFrameworkAnnotations
97+
displayName: Migrate from Spring Framework annotations to JSpecify
98+
description: Migrate from Spring Framework annotations to JSpecify.
99+
recipeList:
100+
- org.openrewrite.java.dependencies.AddDependency:
101+
groupId: org.jspecify
102+
artifactId: jspecify
103+
version: 1.0.0
104+
onlyIfUsing: org.springframework.lang.*ull*
105+
- org.openrewrite.java.ChangeType:
106+
oldFullyQualifiedTypeName: org.springframework.lang.Nullable
107+
newFullyQualifiedTypeName: org.jspecify.annotations.Nullable
108+
ignoreDefinition: true
109+
- org.openrewrite.java.ChangeType:
110+
oldFullyQualifiedTypeName: org.springframework.lang.NonNull
111+
newFullyQualifiedTypeName: org.jspecify.annotations.NonNull
112+
ignoreDefinition: true
113+
- org.openrewrite.staticanalysis.java.MoveFieldAnnotationToType:
114+
annotationType: org.jspecify.annotations.*

src/test/java/org/openrewrite/java/migrate/jspecify/MigrateToJspecifyTest.java

+97-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class MigrateToJspecifyTest implements RewriteTest {
3131
public void defaults(RecipeSpec spec) {
3232
spec
3333
.recipeFromResource("/META-INF/rewrite/jspecify.yml", "org.openrewrite.java.jspecify.MigrateToJspecify")
34-
.parser(JavaParser.fromJavaVersion().classpath("jsr305", "jakarta.annotation-api", "annotations"));
34+
.parser(JavaParser.fromJavaVersion().classpath("jsr305", "jakarta.annotation-api", "annotations", "spring-core"));
3535
}
3636

3737
@DocumentExample
@@ -45,7 +45,7 @@ void migrateFromJavaxAnnotationApiToJspecify() {
4545
"""
4646
import javax.annotation.Nonnull;
4747
import javax.annotation.Nullable;
48-
48+
4949
public class Test {
5050
@Nonnull
5151
public String field1;
@@ -54,7 +54,7 @@ public class Test {
5454
@Nullable
5555
public Foo.Bar foobar;
5656
}
57-
57+
5858
interface Foo {
5959
class Bar {
6060
@Nonnull
@@ -65,16 +65,16 @@ class Bar {
6565
"""
6666
import org.jspecify.annotations.NonNull;
6767
import org.jspecify.annotations.Nullable;
68-
68+
6969
public class Test {
7070
@NonNull
7171
public String field1;
7272
@Nullable
7373
public String field2;
74-
74+
7575
public Foo.@Nullable Bar foobar;
7676
}
77-
77+
7878
interface Foo {
7979
class Bar {
8080
@NonNull
@@ -308,4 +308,95 @@ class Bar {
308308
)
309309
);
310310
}
311+
312+
@Test
313+
void migrateFromSpringFrameworkAnnotationsToJspecify() {
314+
rewriteRun(
315+
mavenProject("foo",
316+
//language=java
317+
srcMainJava(
318+
java(
319+
"""
320+
import org.springframework.lang.NonNull;
321+
import org.springframework.lang.Nullable;
322+
323+
public class Test {
324+
@NonNull
325+
public String field1;
326+
@Nullable
327+
public String field2;
328+
@Nullable
329+
public Foo.Bar foobar;
330+
}
331+
332+
interface Foo {
333+
class Bar {
334+
@NonNull
335+
public String barField;
336+
}
337+
}
338+
""",
339+
"""
340+
import org.jspecify.annotations.NonNull;
341+
import org.jspecify.annotations.Nullable;
342+
343+
public class Test {
344+
@NonNull
345+
public String field1;
346+
@Nullable
347+
public String field2;
348+
349+
public Foo.@Nullable Bar foobar;
350+
}
351+
352+
interface Foo {
353+
class Bar {
354+
@NonNull
355+
public String barField;
356+
}
357+
}
358+
"""
359+
)
360+
),
361+
//language=xml
362+
pomXml(
363+
"""
364+
<project>
365+
<modelVersion>4.0.0</modelVersion>
366+
<groupId>com.example.foobar</groupId>
367+
<artifactId>foobar-core</artifactId>
368+
<version>1.0.0</version>
369+
<dependencies>
370+
<dependency>
371+
<groupId>org.springframework</groupId>
372+
<artifactId>spring-core</artifactId>
373+
<version>6.1.13</version>
374+
</dependency>
375+
</dependencies>
376+
</project>
377+
""",
378+
"""
379+
<project>
380+
<modelVersion>4.0.0</modelVersion>
381+
<groupId>com.example.foobar</groupId>
382+
<artifactId>foobar-core</artifactId>
383+
<version>1.0.0</version>
384+
<dependencies>
385+
<dependency>
386+
<groupId>org.jspecify</groupId>
387+
<artifactId>jspecify</artifactId>
388+
<version>1.0.0</version>
389+
</dependency>
390+
<dependency>
391+
<groupId>org.springframework</groupId>
392+
<artifactId>spring-core</artifactId>
393+
<version>6.1.13</version>
394+
</dependency>
395+
</dependencies>
396+
</project>
397+
"""
398+
)
399+
)
400+
);
401+
}
311402
}

0 commit comments

Comments
 (0)