Skip to content

Commit e954a3e

Browse files
ghustatimtebeekgithub-actions[bot]
authored
feat: Missing "Add explicit Common Annotations dependencies" in "Migrate to Java 11" (#456)
* feat: Missing "Add explicit Common Annotations dependencies" in "Migrate to Java 11" Refs: #455 * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Tim te Beek <[email protected]> * Minor touch ups --------- Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: Tim te Beek <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 6920ea6 commit e954a3e

File tree

5 files changed

+127
-3
lines changed

5 files changed

+127
-3
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ dependencies {
5151
testRuntimeOnly("com.fasterxml.jackson.core:jackson-core")
5252
testRuntimeOnly("com.fasterxml.jackson.core:jackson-databind")
5353
testRuntimeOnly("org.codehaus.groovy:groovy:latest.release")
54+
testRuntimeOnly("jakarta.annotation:jakarta.annotation-api:1.3.5")
5455
testRuntimeOnly(gradleApi())
5556
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#
2+
# Copyright 2024 the original author or authors.
3+
# <p>
4+
# Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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+
---
17+
type: specs.openrewrite.org/v1beta/recipe
18+
name: org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies
19+
displayName: Add explicit Common Annotations dependencies
20+
description: >
21+
Add the necessary `annotation-api` dependency from Jakarta EE 8 to maintain compatibility with Java version 11 or greater.
22+
tags:
23+
- javax
24+
- java11
25+
- jsr250
26+
- jakarta
27+
recipeList:
28+
# Add or update the jakarta.annotation-api to a maven project. This artifact still uses the javax.annotation name space.
29+
- org.openrewrite.java.dependencies.ChangeDependency:
30+
oldGroupId: javax.annotation
31+
oldArtifactId: javax.annotation-api
32+
newGroupId: jakarta.annotation
33+
newArtifactId: jakarta.annotation-api
34+
newVersion: 1.3.x
35+
- org.openrewrite.java.dependencies.AddDependency:
36+
groupId: jakarta.annotation
37+
artifactId: jakarta.annotation-api
38+
version: 1.3.x
39+
onlyIfUsing: javax.annotation..*
40+
acceptTransitive: true

src/main/resources/META-INF/rewrite/add-inject-dependencies.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ type: specs.openrewrite.org/v1beta/recipe
1818
name: org.openrewrite.java.migrate.javax.AddInjectDependencies
1919
displayName: Add explicit Inject dependencies
2020
description: >
21-
This recipe will add the necessary `inject-api` dependency from Jakarta EE 8 to maintain compatibility with Java
22-
version 11 or greater.
21+
Add the necessary `inject-api` dependency from Jakarta EE 8 to maintain compatibility with Java version 11 or greater.
2322
tags:
2423
- javax
2524
- java11
@@ -33,7 +32,6 @@ recipeList:
3332
version: 1.0.3
3433
onlyIfUsing: javax.inject.*
3534
acceptTransitive: true
36-
3735
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
3836
groupId: jakarta.inject
3937
artifactId: jakarta.inject-api

src/main/resources/META-INF/rewrite/java-version-11.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ recipeList:
3737
- org.openrewrite.java.migrate.javax.AddJaxbDependencies
3838
- org.openrewrite.java.migrate.javax.AddJaxwsDependencies
3939
- org.openrewrite.java.migrate.javax.AddInjectDependencies
40+
- org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies
4041
# Remediate deprecations
4142
- org.openrewrite.staticanalysis.BigDecimalRoundingConstantsToEnums
4243
- org.openrewrite.staticanalysis.PrimitiveWrapperClassConstructorToValueOf
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.javax;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.java.JavaParser;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.java.Assertions.*;
24+
import static org.openrewrite.maven.Assertions.pomXml;
25+
26+
class AddCommonAnnotationsDependenciesTest implements RewriteTest {
27+
28+
@Override
29+
public void defaults(RecipeSpec spec) {
30+
spec.recipeFromResource(
31+
"/META-INF/rewrite/add-common-annotations-dependencies.yml",
32+
"org.openrewrite.java.migrate.javax.AddCommonAnnotationsDependencies")
33+
.allSources(src -> src.markers(javaVersion(8)));
34+
}
35+
36+
@Test
37+
void addDependencyIfAnnotationJsr250Present() {
38+
rewriteRun(
39+
spec -> spec.parser(JavaParser.fromJavaVersion().classpath("jakarta.annotation-api")),
40+
mavenProject("my-project",
41+
//language=java
42+
srcMainJava(
43+
java(
44+
"""
45+
import javax.annotation.Generated;
46+
47+
@Generated("Hello")
48+
class A {
49+
}
50+
"""
51+
)
52+
),
53+
//language=xml
54+
pomXml(
55+
"""
56+
<?xml version="1.0" encoding="UTF-8"?>
57+
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
58+
<modelVersion>4.0.0</modelVersion>
59+
<groupId>org.sample</groupId>
60+
<artifactId>sample</artifactId>
61+
<version>1.0.0</version>
62+
</project>
63+
""",
64+
"""
65+
<?xml version="1.0" encoding="UTF-8"?>
66+
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
67+
<modelVersion>4.0.0</modelVersion>
68+
<groupId>org.sample</groupId>
69+
<artifactId>sample</artifactId>
70+
<version>1.0.0</version>
71+
<dependencies>
72+
<dependency>
73+
<groupId>jakarta.annotation</groupId>
74+
<artifactId>jakarta.annotation-api</artifactId>
75+
<version>1.3.5</version>
76+
</dependency>
77+
</dependencies>
78+
</project>
79+
"""
80+
)
81+
)
82+
);
83+
}
84+
}

0 commit comments

Comments
 (0)