Skip to content

Commit 7c3842e

Browse files
authored
Add com.sun.istack.NotNull to jakarta.validation.constraints.NotNull migration (#973)
This adds a ChangeType to the JavaxValidationMigrationToJakartaValidation recipe to migrate usages of the internal Sun JAXB annotation @com.sun.istack.NotNull to @jakarta.validation.constraints.NotNull. This addresses the third sub-issue in customer-requests#1526 where SpringBoot upgrade migrations were leaving com.sun.istack.NotNull imports unchanged, causing compilation errors. Fixes: moderneinc/customer-requests#1526
1 parent 4f2dca1 commit 7c3842e

2 files changed

Lines changed: 155 additions & 0 deletions

File tree

src/main/resources/META-INF/rewrite/jakarta-ee-9.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ recipeList:
252252
oldPackageName: javax.validation
253253
newPackageName: jakarta.validation
254254
recursive: true
255+
- org.openrewrite.java.ChangeType:
256+
oldFullyQualifiedTypeName: com.sun.istack.NotNull
257+
newFullyQualifiedTypeName: jakarta.validation.constraints.NotNull
255258
- org.openrewrite.RenameFile:
256259
fileMatcher: '**/javax.validation.ConstraintValidator'
257260
fileName: jakarta.validation.ConstraintValidator
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://docs.moderne.io/licensing/moderne-source-available-license
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.jakarta;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.Issue;
21+
import org.openrewrite.config.Environment;
22+
import org.openrewrite.java.JavaParser;
23+
import org.openrewrite.test.RecipeSpec;
24+
import org.openrewrite.test.RewriteTest;
25+
26+
import static org.openrewrite.java.Assertions.java;
27+
28+
class JavaxValidationMigrationToJakartaValidationTest implements RewriteTest {
29+
30+
@Override
31+
public void defaults(RecipeSpec spec) {
32+
spec.recipe(
33+
Environment.builder()
34+
.scanRuntimeClasspath("org.openrewrite.java.migrate")
35+
.build()
36+
.activateRecipes("org.openrewrite.java.migrate.jakarta.JavaxValidationMigrationToJakartaValidation")
37+
);
38+
}
39+
40+
@Issue("https://github.com/moderneinc/customer-requests/issues/1526")
41+
@Test
42+
void sunIstackNotNullToJakartaValidation() {
43+
rewriteRun(
44+
spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(
45+
//language=java
46+
"""
47+
package com.sun.istack;
48+
import java.lang.annotation.*;
49+
@Documented
50+
@Retention(RetentionPolicy.CLASS)
51+
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
52+
public @interface NotNull {
53+
}
54+
""",
55+
//language=java
56+
"""
57+
package jakarta.validation.constraints;
58+
import java.lang.annotation.*;
59+
@Documented
60+
@Retention(RetentionPolicy.RUNTIME)
61+
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
62+
public @interface NotNull {
63+
String message() default "{jakarta.validation.constraints.NotNull.message}";
64+
Class<?>[] groups() default {};
65+
Class<?>[] payload() default {};
66+
}
67+
"""
68+
)),
69+
//language=java
70+
java(
71+
"""
72+
import com.sun.istack.NotNull;
73+
74+
public class Example {
75+
@NotNull
76+
private String name;
77+
78+
public void setName(@NotNull String name) {
79+
this.name = name;
80+
}
81+
}
82+
""",
83+
"""
84+
import jakarta.validation.constraints.NotNull;
85+
86+
public class Example {
87+
@NotNull
88+
private String name;
89+
90+
public void setName(@NotNull String name) {
91+
this.name = name;
92+
}
93+
}
94+
"""
95+
)
96+
);
97+
}
98+
99+
@DocumentExample
100+
@Test
101+
void javaxValidationToJakartaValidation() {
102+
rewriteRun(
103+
spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(
104+
//language=java
105+
"""
106+
package javax.validation.constraints;
107+
import java.lang.annotation.*;
108+
@Documented
109+
@Retention(RetentionPolicy.RUNTIME)
110+
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
111+
public @interface NotNull {
112+
String message() default "{javax.validation.constraints.NotNull.message}";
113+
Class<?>[] groups() default {};
114+
Class<?>[] payload() default {};
115+
}
116+
""",
117+
//language=java
118+
"""
119+
package jakarta.validation.constraints;
120+
import java.lang.annotation.*;
121+
@Documented
122+
@Retention(RetentionPolicy.RUNTIME)
123+
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
124+
public @interface NotNull {
125+
String message() default "{jakarta.validation.constraints.NotNull.message}";
126+
Class<?>[] groups() default {};
127+
Class<?>[] payload() default {};
128+
}
129+
"""
130+
)),
131+
//language=java
132+
java(
133+
"""
134+
import javax.validation.constraints.NotNull;
135+
136+
public class Example {
137+
@NotNull
138+
private String name;
139+
}
140+
""",
141+
"""
142+
import jakarta.validation.constraints.NotNull;
143+
144+
public class Example {
145+
@NotNull
146+
private String name;
147+
}
148+
"""
149+
)
150+
);
151+
}
152+
}

0 commit comments

Comments
 (0)