Skip to content

Commit 2157f1a

Browse files
authored
Shorten MaintainTrailingSlashURLMappings code by using an inline visitor (#746)
1 parent 83bc171 commit 2157f1a

File tree

2 files changed

+25
-39
lines changed

2 files changed

+25
-39
lines changed

src/main/java/org/openrewrite/java/spring/boot3/MaintainTrailingSlashURLMappings.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,18 @@ public AtomicBoolean getInitialValue(ExecutionContext ctx) {
5757
public TreeVisitor<?, ExecutionContext> getScanner(AtomicBoolean acc) {
5858
return new JavaIsoVisitor<ExecutionContext>() {
5959
@Override
60-
public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
61-
if (!acc.get()) {
62-
acc.set(FindWebConfigurer.find(cu));
60+
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
61+
if (!acc.get() && classDecl.getImplements() != null) {
62+
for (TypeTree impl : classDecl.getImplements()) {
63+
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(impl.getType());
64+
if (fullyQualified != null &&
65+
(WEB_MVC_CONFIGURER.equals(fullyQualified.getFullyQualifiedName()) ||
66+
WEB_FLUX_CONFIGURER.equals(fullyQualified.getFullyQualifiedName()))) {
67+
acc.set(true);
68+
}
69+
}
6370
}
64-
return cu;
71+
return super.visitClassDeclaration(classDecl, ctx);
6572
}
6673
};
6774
}
@@ -74,33 +81,8 @@ public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean acc) {
7481
if (acc.get()) {
7582
return new AddSetUseTrailingSlashMatch().getVisitor().visit(tree, ctx);
7683
}
77-
7884
return new AddRouteTrailingSlash().getVisitor().visit(tree, ctx);
7985
}
8086
};
8187
}
82-
83-
private static class FindWebConfigurer extends JavaIsoVisitor<AtomicBoolean> {
84-
static boolean find(J j) {
85-
return new FindWebConfigurer()
86-
.reduce(j, new AtomicBoolean()).get();
87-
}
88-
89-
@Override
90-
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, AtomicBoolean found) {
91-
if (classDecl.getImplements() != null) {
92-
for (TypeTree impl : classDecl.getImplements()) {
93-
JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(impl.getType());
94-
if (fullyQualified != null &&
95-
(WEB_MVC_CONFIGURER.equals(fullyQualified.getFullyQualifiedName()) ||
96-
WEB_FLUX_CONFIGURER.equals(fullyQualified.getFullyQualifiedName()))
97-
) {
98-
found.set(true);
99-
return classDecl;
100-
}
101-
}
102-
}
103-
return classDecl;
104-
}
105-
}
10688
}

src/testWithSpringBoot_2_7/java/org/openrewrite/java/spring/boot2/MaintainTrailingSlashURLMappingsTest.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ public void defaults(RecipeSpec spec) {
3636
@DocumentExample
3737
@Test
3838
void noConfigOverridden() {
39+
//language=java
3940
rewriteRun(
4041
java(
4142
"""
4243
import org.springframework.web.bind.annotation.*;
43-
44+
4445
@RestController
4546
public class ExampleController {
46-
47+
4748
@GetMapping("/get")
4849
public String getExample() {
4950
return "This is a GET example.";
@@ -52,10 +53,10 @@ public String getExample() {
5253
""",
5354
"""
5455
import org.springframework.web.bind.annotation.*;
55-
56+
5657
@RestController
5758
public class ExampleController {
58-
59+
5960
@GetMapping({"/get", "/get/"})
6061
public String getExample() {
6162
return "This is a GET example.";
@@ -76,7 +77,7 @@ void noChangeWithConfigOverriddenByWebMvcConfigurer() {
7677
7778
@RestController
7879
public class ExampleController {
79-
80+
8081
@GetMapping("/get")
8182
public String getExample() {
8283
return "This is a GET example.";
@@ -104,14 +105,15 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
104105

105106
@Test
106107
void addSetUseTrailingSlashMatchForWebMvcConfigurer() {
108+
//language=java
107109
rewriteRun(
108110
java(
109111
"""
110112
import org.springframework.web.bind.annotation.*;
111113
112114
@RestController
113115
public class ExampleController {
114-
116+
115117
@GetMapping("/get")
116118
public String getExample() {
117119
return "This is a GET example.";
@@ -151,14 +153,15 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
151153

152154
@Test
153155
void noChangeWithConfigOverriddenByWebFluxConfigurer() {
156+
//language=java
154157
rewriteRun(
155158
java(
156159
"""
157160
import org.springframework.web.bind.annotation.*;
158-
161+
159162
@RestController
160163
public class ExampleController {
161-
164+
162165
@GetMapping("/get")
163166
public String getExample() {
164167
return "This is a GET example.";
@@ -186,14 +189,15 @@ public void configurePathMatching(PathMatchConfigurer configurer) {
186189

187190
@Test
188191
void addSetUseTrailingSlashForWebFluxConfigurer() {
192+
//language=java
189193
rewriteRun(
190194
java(
191195
"""
192196
import org.springframework.web.bind.annotation.*;
193-
197+
194198
@RestController
195199
public class ExampleController {
196-
200+
197201
@GetMapping("/get")
198202
public String getExample() {
199203
return "This is a GET example.";

0 commit comments

Comments
 (0)