19
19
import org .openrewrite .Preconditions ;
20
20
import org .openrewrite .Recipe ;
21
21
import org .openrewrite .TreeVisitor ;
22
+ import org .openrewrite .internal .ListUtils ;
23
+ import org .openrewrite .java .AnnotationMatcher ;
22
24
import org .openrewrite .java .JavaIsoVisitor ;
25
+ import org .openrewrite .java .RemoveAnnotation ;
23
26
import org .openrewrite .java .search .UsesType ;
24
27
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 ;
25
35
26
36
public class NotRepeatSpringAnnotationsInSubclasses extends Recipe {
27
37
@@ -37,17 +47,41 @@ public String getDescription() {
37
47
38
48
@ Override
39
49
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 >() {
41
52
@ Override
42
53
public J .ClassDeclaration visitClassDeclaration (J .ClassDeclaration classDecl , ExecutionContext ctx ) {
43
54
J .ClassDeclaration cd = super .visitClassDeclaration (classDecl , ctx );
44
55
return cd ;
45
56
}
46
57
47
58
@ 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 ;
50
84
}
51
- }) ;
85
+ };
52
86
}
53
87
}
0 commit comments