Skip to content

Commit 2ea2faa

Browse files
committed
- detect repeating and remove methods annotation
1 parent 60339e0 commit 2ea2faa

File tree

2 files changed

+62
-27
lines changed

2 files changed

+62
-27
lines changed

src/main/java/org/openrewrite/java/spring/NotRepeatSpringAnnotationsInSubclasses.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@
1919
import org.openrewrite.Preconditions;
2020
import org.openrewrite.Recipe;
2121
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.internal.ListUtils;
23+
import org.openrewrite.java.AnnotationMatcher;
2224
import org.openrewrite.java.JavaIsoVisitor;
25+
import org.openrewrite.java.RemoveAnnotation;
2326
import org.openrewrite.java.search.UsesType;
2427
import org.openrewrite.java.tree.J;
28+
import org.openrewrite.java.tree.JavaType;
29+
import org.openrewrite.java.tree.TypeUtils;
30+
31+
import java.util.Collections;
32+
import java.util.List;
33+
import java.util.Optional;
34+
import java.util.stream.Collectors;
2535

2636
public class NotRepeatSpringAnnotationsInSubclasses extends Recipe {
2737

@@ -37,17 +47,41 @@ public String getDescription() {
3747

3848
@Override
3949
public TreeVisitor<?, ExecutionContext> getVisitor() {
40-
return Preconditions.check(new UsesType<>("org.springframework.web.bind.annotation.PostMapping", false), new JavaIsoVisitor<ExecutionContext>() {
50+
//return Preconditions.check(new UsesType<>("org.springframework.web.bind.annotation.PostMapping", false), new JavaIsoVisitor<ExecutionContext>() {
51+
return new JavaIsoVisitor<ExecutionContext>() {
4152
@Override
4253
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
4354
J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx);
4455
return cd;
4556
}
4657

4758
@Override
48-
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext executionContext) {
49-
return super.visitMethodDeclaration(method, executionContext);
59+
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) {
60+
J.MethodDeclaration md = super.visitMethodDeclaration(method, ctx);
61+
62+
Optional<JavaType.Method> overriddenMethod = TypeUtils.findOverriddenMethod(md.getMethodType());
63+
if (overriddenMethod.isPresent()) {
64+
65+
JavaType.Method overrideMethod = overriddenMethod.get();
66+
67+
List<JavaType.FullyQualified> baseAnnotations = overrideMethod.getAnnotations();
68+
List<JavaType.FullyQualified> methodAnnotations = md.getMethodType().getAnnotations();
69+
List<JavaType.FullyQualified> nonRepeated = methodAnnotations.stream()
70+
.filter(a -> baseAnnotations.stream().noneMatch(b -> TypeUtils.isOfType(a, b)))
71+
.collect(Collectors.toList());
72+
73+
List<J.Annotation> annotations = ListUtils.map(md.getLeadingAnnotations(),
74+
a -> {
75+
if (nonRepeated.stream().noneMatch(n -> TypeUtils.isOfType(a.getType(), ((JavaType.Annotation)n).getType())))
76+
return (J.Annotation) new RemoveAnnotation(a.getType().toString()).getVisitor().visit(a, ctx, getCursor().getParentOrThrow());
77+
return a;
78+
});
79+
md = md.withLeadingAnnotations(annotations);
80+
81+
82+
}
83+
return md;
5084
}
51-
});
85+
};
5286
}
5387
}

src/testWithSpringBoot_1_5/java/org/openrewrite/java/spring/NotRepeatSpringAnnotationsInSubclassesTest.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,49 @@ class NotRepeatSpringAnnotationsInSubclassesTest implements RewriteTest {
2828
public void defaults(RecipeSpec spec) {
2929
spec.recipe(new NotRepeatSpringAnnotationsInSubclasses())
3030
.parser(JavaParser.fromJavaVersion().classpath("spring-beans", "spring-boot",
31-
"spring-context", "spring-core", "spring-web"));
32-
}
33-
34-
@Test
35-
void removeLeadingAutowiredAnnotation() {
36-
//language=java
37-
rewriteRun(
38-
java(
39-
"""
40-
import org.springframework.web.bind.annotation.PathVariable;
31+
"spring-context", "spring-core", "spring-web")
32+
.dependsOn(
33+
"""
34+
import org.springframework.web.bind.annotation.PathVariable;
4135
import org.springframework.web.bind.annotation.PostMapping;
4236
import org.springframework.web.bind.annotation.RequestBody;
37+
4338
public interface UserApi {
4439
@PostMapping("/users/{id}")
4540
String updateUser(
4641
@PathVariable("id") Long id,
4742
@RequestBody UserData request
4843
);
44+
4945
class UserData {
5046
private String firstName;
5147
private String lastName;
52-
public String getFirstName() {return firstName;}
48+
49+
public String getFirstName() {
50+
return firstName;
51+
}
52+
5353
public void setFirstName(String firstName) {
5454
this.firstName = firstName;
5555
}
56+
5657
public String getLastName() {
5758
return lastName;
5859
}
60+
5961
public void setLastName(String lastName) {
6062
this.lastName = lastName;
61-
}
62-
"""
63-
),
64-
return lastName;
65-
}
66-
67-
public void setLastName(String lastName) {
68-
this.lastName = lastName;
69-
}
7063
}
7164
}
72-
"""),
65+
}
66+
"""
67+
));
68+
}
69+
70+
@Test
71+
void removeLeadingAutowiredAnnotation() {
72+
//language=java
73+
rewriteRun(
7374
java(
7475
"""
7576
import org.springframework.web.bind.annotation.PathVariable;
@@ -80,8 +81,8 @@ public void setLastName(String lastName) {
8081
@RestController
8182
public class UserController implements UserApi {
8283
83-
@PostMapping("/users/{id}")
84-
String updateUser(
84+
@Override @PostMapping("/users/{id}")
85+
public String updateUser(
8586
@PathVariable("id") Long id,
8687
@RequestBody UserData request
8788
) {

0 commit comments

Comments
 (0)