Skip to content

Commit c5383a5

Browse files
authored
adjusting MaybeAddJakartaServletApi to be a ScanningRecipe so that it can correctly apply its precondition to the AddDependency recipe (#255)
1 parent d73534b commit c5383a5

File tree

2 files changed

+143
-26
lines changed

2 files changed

+143
-26
lines changed

src/main/java/org/openrewrite/java/migrate/jakarta/MaybeAddJakartaServletApi.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@
1515
*/
1616
package org.openrewrite.java.migrate.jakarta;
1717

18-
import org.openrewrite.ExecutionContext;
19-
import org.openrewrite.Preconditions;
20-
import org.openrewrite.Recipe;
21-
import org.openrewrite.TreeVisitor;
18+
import com.fasterxml.jackson.annotation.JsonIgnore;
19+
import org.openrewrite.*;
2220
import org.openrewrite.maven.AddDependency;
2321
import org.openrewrite.maven.search.DoesNotIncludeDependency;
2422

25-
import java.util.Collections;
26-
import java.util.List;
27-
28-
public class MaybeAddJakartaServletApi extends Recipe {
23+
public class MaybeAddJakartaServletApi extends ScanningRecipe<AddDependency.Scanned> {
2924

3025
@Override
3126
public String getDisplayName() {
@@ -37,26 +32,38 @@ public String getDescription() {
3732
return "Adds the `jakarta.servlet-api` dependency, unless the project already uses `spring-boot-starter-web`, which transitively includes a compatible implementation under a different GAV.";
3833
}
3934

35+
@JsonIgnore
36+
private final AddDependency addDependency = new AddDependency(
37+
"jakarta.servlet",
38+
"jakarta.servlet-api",
39+
"6.x",
40+
null,
41+
null,
42+
null,
43+
"javax.servlet.*",
44+
null,
45+
null,
46+
null,
47+
null,
48+
true
49+
);
50+
51+
@Override
52+
public AddDependency.Scanned getInitialValue(ExecutionContext ctx) {
53+
return addDependency.getInitialValue(ctx);
54+
}
55+
4056
@Override
41-
public TreeVisitor<?, ExecutionContext> getVisitor() {
42-
return Preconditions.check(new DoesNotIncludeDependency("org.springframework.boot", "spring-boot-starter-web", null, null), TreeVisitor.noop());
57+
public TreeVisitor<?, ExecutionContext> getScanner(AddDependency.Scanned acc) {
58+
return addDependency.getScanner(acc);
4359
}
4460

4561
@Override
46-
public List<Recipe> getRecipeList() {
47-
return Collections.singletonList(new AddDependency(
48-
"jakarta.servlet",
49-
"jakarta.servlet-api",
50-
"6.x",
51-
null,
52-
null,
53-
null,
54-
"javax.servlet.*",
55-
null,
56-
null,
57-
null,
58-
null,
59-
true
60-
));
62+
public TreeVisitor<?, ExecutionContext> getVisitor(AddDependency.Scanned acc) {
63+
return Preconditions.check(
64+
new DoesNotIncludeDependency("org.springframework.boot", "spring-boot-starter-web", null, null),
65+
addDependency.getVisitor(acc)
66+
);
6167
}
62-
}
68+
69+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2023 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.jakarta;
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 MaybeAddJakartaServletApiTest implements RewriteTest {
27+
@Override
28+
public void defaults(RecipeSpec spec) {
29+
spec.recipe(new MaybeAddJakartaServletApi())
30+
.parser(JavaParser.fromJavaVersion()
31+
.dependsOn("package javax.servlet;\npublic class Filter {}"));
32+
}
33+
34+
@Test
35+
void hasSpringBootStarterWeb() {
36+
rewriteRun(
37+
mavenProject("my-project",
38+
srcMainJava(java("""
39+
import javax.servlet.Filter;
40+
class A {
41+
Filter foo = null;
42+
}
43+
""")),
44+
pomXml("""
45+
<?xml version="1.0" encoding="UTF-8"?>
46+
<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">
47+
<modelVersion>4.0.0</modelVersion>
48+
<groupId>org.sample</groupId>
49+
<artifactId>sample</artifactId>
50+
<version>1.0.0</version>
51+
52+
<dependencies>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-web</artifactId>
56+
<version>2.7.0</version>
57+
</dependency>
58+
</dependencies>
59+
60+
</project>
61+
""")
62+
)
63+
);
64+
}
65+
66+
@Test
67+
void doesNotHaveSpringBootStarterWeb() {
68+
rewriteRun(
69+
mavenProject("my-project",
70+
srcMainJava(java("""
71+
import javax.servlet.Filter;
72+
class A {
73+
Filter foo = null;
74+
}
75+
""")),
76+
pomXml("""
77+
<?xml version="1.0" encoding="UTF-8"?>
78+
<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">
79+
<modelVersion>4.0.0</modelVersion>
80+
<groupId>org.sample</groupId>
81+
<artifactId>sample</artifactId>
82+
<version>1.0.0</version>
83+
84+
<dependencies>
85+
</dependencies>
86+
87+
</project>
88+
""",
89+
"""
90+
<?xml version="1.0" encoding="UTF-8"?>
91+
<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">
92+
<modelVersion>4.0.0</modelVersion>
93+
<groupId>org.sample</groupId>
94+
<artifactId>sample</artifactId>
95+
<version>1.0.0</version>
96+
97+
<dependencies>
98+
<dependency>
99+
<groupId>jakarta.servlet</groupId>
100+
<artifactId>jakarta.servlet-api</artifactId>
101+
<version>6.0.0</version>
102+
</dependency>
103+
</dependencies>
104+
105+
</project>
106+
""")
107+
)
108+
);
109+
}
110+
}

0 commit comments

Comments
 (0)