Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* 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.jakarta;

import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.Tree;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.marker.JavaProject;
import org.openrewrite.java.search.FindAnnotations;
import org.openrewrite.java.tree.J;
import org.openrewrite.marker.SearchResult;

import java.util.HashSet;
import java.util.Set;

public class HasNoJakartaAnnotations extends ScanningRecipe<HasNoJakartaAnnotations.Accumulator> {
@Override
public String getDisplayName() {
return "Project has no Jakarta annotations";
}

@Override
public String getDescription() {
return "Mark all source as found per `JavaProject` where no Jakarta annotations are found. " +
"This is useful mostly as a precondition for recipes that require Jakarta annotations to be present";
}

@Value
public static class Accumulator {
Set<JavaProject> projectsWithDependency;
}

@Override
public Accumulator getInitialValue(ExecutionContext ctx) {
return new Accumulator(new HashSet<>());
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(HasNoJakartaAnnotations.Accumulator acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree preVisit(Tree tree, ExecutionContext ctx) {
stopAfterPreVisit();
if (tree instanceof J) {
tree.getMarkers().findFirst(JavaProject.class)
.filter(jp -> !acc.getProjectsWithDependency().contains(jp))
.filter(jp -> !FindAnnotations.find((J) tree, "@jakarta.annotation.*", true).isEmpty())
.ifPresent(jp -> acc.getProjectsWithDependency().add(jp));
}
return tree;
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(HasNoJakartaAnnotations.Accumulator acc) {
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public Tree preVisit(Tree tree, ExecutionContext ctx) {
stopAfterPreVisit();
return tree.getMarkers().findFirst(JavaProject.class)
.filter(it -> !acc.getProjectsWithDependency().contains(it))
.map(__ -> SearchResult.found(tree, "Project has no Jakarta annotations"))
.orElse(tree);
}
};
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/jakarta-ee-9.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@ name: org.openrewrite.java.migrate.jakarta.RemoveJakartaAnnotationDependency
displayName: Remove `jakarta.annotation-api` dependency when managed by Spring Boot
description: Counteract the `jakarta.annotation-api` added by `org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies` for Spring Boot applications.
preconditions:
- org.openrewrite.java.migrate.jakarta.HasNoJakartaAnnotations
- org.openrewrite.java.dependencies.DependencyInsight:
groupIdPattern: org.springframework.boot
artifactIdPattern: spring-boot-starter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.gradle.Assertions.buildGradle;
import static org.openrewrite.gradle.Assertions.settingsGradle;
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
import static org.openrewrite.java.Assertions.*;
import static org.openrewrite.maven.Assertions.pomXml;
import static org.openrewrite.xml.Assertions.xml;
Expand Down Expand Up @@ -60,6 +63,16 @@ public void foo() {}
}
""";

@Language("java")
private static final String jakartaAnnotation =
"""
package jakarta.annotation;
public @interface Nonnull {
}
public @interface Nullable {
}
""";

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(
Expand Down Expand Up @@ -442,9 +455,7 @@ void projectWithSpringBootStarterWeb() {
//language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -511,9 +522,7 @@ void projectWithSpringBoot3StarterWebShouldRemoveJakartaDependency() {
//language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
Expand All @@ -538,9 +547,7 @@ void projectWithSpringBoot3StarterWebShouldRemoveJakartaDependency() {
</project>
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -574,6 +581,242 @@ public class TestApplication {
);
}

@Test
void projectWithSpringBoot3StarterWebShouldNotRemoveJakartaDependencyWhenUsingNonnullAnnotation() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(javaxServlet, jakartaAnnotation)),
mavenProject(
"Sample",
//language=xml
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
"""
),
srcMainJava(
//language=java
java(
"""
import jakarta.annotation.Nonnull;

public class TestApplication {
@Nonnull
public String upperCase(@Nonnull String input) {
return input.toUpperCase();
}
}
"""
)
)
)
);
}

@Test
void projectWithSpringBoot3StarterWebShouldNotRemoveJakartaDependencyWhenUsingNullableAnnotation() {
rewriteRun(
spec -> spec.parser(JavaParser.fromJavaVersion().dependsOn(javaxServlet, jakartaAnnotation)),
mavenProject(
"Sample",
//language=xml
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
"""
),
srcMainJava(
//language=java
java(
"""
import jakarta.annotation.Nullable;

public class TestApplication {
@Nullable
public String safeUpperCase(@Nullable String input) {
return input == null ? null : input.toUpperCase();
}
}
"""
)
)
)
);
}

@Test
void multiProjectWithSpringBoot3StarterWebShouldRemoveJakartaDependencyWhenUsingNullableAnnotationWhenApplicable() {
rewriteRun(
spec -> spec.beforeRecipe(withToolingApi()).parser(JavaParser.fromJavaVersion().dependsOn(javaxServlet, jakartaAnnotation)),
mavenProject("multi-project-build",
//language=groovy
settingsGradle("""
include 'project-with-null-annotations'
include 'project-without-null-annotations'
"""),
mavenProject("project-with-null-annotations",
//language=groovy
buildGradle(
"""
plugins {
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
implementation 'jakarta.annotation:jakarta.annotation-api:1.3.5'
implementation 'org.springframework.boot:spring-boot-starter-web'
}
""",
"""
plugins {
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
implementation 'jakarta.annotation:jakarta.annotation-api:2.0.0'
implementation 'org.springframework.boot:spring-boot-starter-web'
}
"""
),
srcMainJava(
//language=java
java(
"""
import jakarta.annotation.Nullable;

public class TestApplication {
@Nullable
public String safeUpperCase(@Nullable String input) {
return input == null ? null : input.toUpperCase();
}
}
"""
)
)
),
mavenProject("project-without-null-annotations",
//language=groovy
buildGradle(
"""
plugins {
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
"""
)
)
)
);
}

@Test
void upgradeAnnotationApiFromV1ToV2() {
rewriteRun(
Expand Down