1919import org .openrewrite .Preconditions ;
2020import org .openrewrite .Recipe ;
2121import org .openrewrite .TreeVisitor ;
22+ import org .openrewrite .internal .ListUtils ;
23+ import org .openrewrite .java .AnnotationMatcher ;
2224import org .openrewrite .java .JavaIsoVisitor ;
25+ import org .openrewrite .java .RemoveAnnotation ;
2326import org .openrewrite .java .search .UsesType ;
2427import 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
2636public 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}
0 commit comments