|
15 | 15 | */ |
16 | 16 | package org.openrewrite.java.testing.junit5; |
17 | 17 |
|
18 | | -import com.fasterxml.jackson.databind.introspect.Annotated; |
19 | 18 | import org.openrewrite.AutoConfigure; |
| 19 | +import org.openrewrite.Formatting; |
20 | 20 | import org.openrewrite.java.AddAnnotation; |
21 | 21 | import org.openrewrite.java.AutoFormat; |
| 22 | +import org.openrewrite.java.JavaIsoRefactorVisitor; |
22 | 23 | import org.openrewrite.java.JavaRefactorVisitor; |
23 | 24 | import org.openrewrite.java.tree.Expression; |
24 | 25 | import org.openrewrite.java.tree.J; |
|
30 | 31 | import java.util.stream.Collectors; |
31 | 32 | import java.util.stream.Stream; |
32 | 33 |
|
| 34 | +import static java.util.stream.Collectors.toList; |
| 35 | +import static org.openrewrite.Tree.randomId; |
| 36 | + |
33 | 37 | /** |
34 | 38 | * Transforms the Junit4 @Category, which can list multiple categories, into one @Tag annotation per category listed |
35 | 39 | */ |
36 | 40 | @AutoConfigure |
37 | | -public class CategoryToTag extends JavaRefactorVisitor { |
| 41 | +public class CategoryToTag extends JavaIsoRefactorVisitor { |
38 | 42 | private static final String categoryAnnotation = "org.junit.experimental.categories.Category"; |
39 | 43 | private static final JavaType.Class tagType = JavaType.Class.build("org.junit.jupiter.api.Tag"); |
40 | 44 |
|
41 | 45 | @Override |
42 | | - public J visitClassDecl(J.ClassDecl classDecl) { |
43 | | - J.ClassDecl cd = refactor(classDecl, super::visitClassDecl); |
44 | | - if(!cd.findAnnotations("@" + categoryAnnotation).isEmpty()) { |
| 46 | + public J.ClassDecl visitClassDecl(J.ClassDecl classDecl) { |
| 47 | + J.ClassDecl cd = super.visitClassDecl(classDecl); |
| 48 | + List<J.Annotation> categoryAnnotations = cd.findAnnotationsOnClass("@" + categoryAnnotation); |
| 49 | + if(!categoryAnnotations.isEmpty()) { |
45 | 50 | andThen(new Scoped(cd)); |
46 | 51 | } |
47 | 52 | return cd; |
48 | 53 | } |
49 | 54 |
|
50 | 55 | @Override |
51 | | - public J visitMethod(J.MethodDecl method) { |
52 | | - J.MethodDecl cd = refactor(method, super::visitMethod); |
53 | | - if(!cd.findAnnotations("@" + categoryAnnotation).isEmpty()) { |
54 | | - andThen(new Scoped(cd)); |
| 56 | + public J.MethodDecl visitMethod(J.MethodDecl method) { |
| 57 | + J.MethodDecl m = super.visitMethod(method); |
| 58 | + List<J.Annotation> categoryAnnotations = m.findAnnotations("@" + categoryAnnotation); |
| 59 | + if(!categoryAnnotations.isEmpty()) { |
| 60 | + andThen(new Scoped(m)); |
55 | 61 | } |
56 | | - return cd; |
| 62 | + return m; |
57 | 63 | } |
58 | 64 |
|
59 | | - public static class Scoped extends JavaRefactorVisitor { |
60 | | - final J statement; |
61 | | - public Scoped(J statement) { |
62 | | - this.statement = statement; |
| 65 | + public static class Scoped extends JavaIsoRefactorVisitor { |
| 66 | + final J scope; |
| 67 | + public Scoped(J scope) { |
| 68 | + this.scope = scope; |
63 | 69 | } |
64 | 70 |
|
65 | 71 | @Override |
66 | | - public J visitClassDecl(J.ClassDecl classDecl) { |
67 | | - J.ClassDecl c = refactor(classDecl, super::visitClassDecl); |
68 | | - |
69 | | - if (statement.isScope(classDecl) && !classDecl.findAnnotations("@" + categoryAnnotation).isEmpty()) { |
70 | | - |
71 | | - classDecl.findAnnotations("@" + categoryAnnotation).stream().forEach( annot -> { |
72 | | - Expression categoryArgs = annot.getArgs().getArgs().iterator().next(); |
73 | | - |
74 | | - Stream<Expression> categories = categoryArgs instanceof J.NewArray ? |
75 | | - ((J.NewArray) categoryArgs).getInitializer().getElements().stream() : |
76 | | - Stream.of(categoryArgs); |
77 | | - |
78 | | - categories.forEach(arg -> { |
79 | | - andThen(new AddAnnotation.Scoped( |
80 | | - classDecl, |
81 | | - "org.junit.jupiter.api.Tag", |
82 | | - arg.withPrefix(""))); |
83 | | - }); |
84 | | - } |
85 | | - ); |
| 72 | + public J.ClassDecl visitClassDecl(J.ClassDecl classDecl) { |
| 73 | + J.ClassDecl c = super.visitClassDecl(classDecl); |
| 74 | + if (scope.isScope(classDecl)) { |
| 75 | + AtomicInteger index = new AtomicInteger(0); |
| 76 | + c = c.withAnnotations(c.getAnnotations().stream() |
| 77 | + .flatMap(this::categoryAnnotationToTagAnnotations) |
| 78 | + .map(annotation -> { |
| 79 | + andThen(new AutoFormat(annotation)); |
| 80 | + if (index.getAndIncrement() != 0) { |
| 81 | + return annotation.withPrefix("\n"); |
| 82 | + } else { |
| 83 | + return annotation; |
| 84 | + } |
| 85 | + }) |
| 86 | + .collect(Collectors.toList())); |
86 | 87 |
|
87 | | - c = c.withAnnotations( |
88 | | - c.getAnnotations().stream() |
89 | | - .filter(annot -> |
90 | | - !TypeUtils.isOfClassType(annot.getAnnotationType().getType(), categoryAnnotation) |
91 | | - ) |
92 | | - .collect(Collectors.toList()) |
93 | | - ); |
| 88 | + maybeRemoveImport(categoryAnnotation); |
| 89 | + maybeAddImport(tagType); |
94 | 90 | } |
95 | | - maybeRemoveImport(categoryAnnotation); |
96 | | - maybeAddImport(tagType); |
97 | 91 |
|
98 | 92 | return c; |
99 | 93 | } |
100 | 94 |
|
101 | 95 | @Override |
102 | | - public J visitMethod(J.MethodDecl methodDecl) { |
103 | | - J.MethodDecl m = refactor(methodDecl, super::visitMethod); |
104 | | - |
105 | | - if (statement.isScope(methodDecl) && !methodDecl.findAnnotations("@" + categoryAnnotation).isEmpty()) { |
| 96 | + public J.MethodDecl visitMethod(J.MethodDecl m) { |
| 97 | + if (scope.isScope(m)) { |
| 98 | + AtomicInteger index = new AtomicInteger(0); |
106 | 99 | m = m.withAnnotations(m.getAnnotations().stream() |
107 | | - .flatMap(annot -> { |
108 | | - if(TypeUtils.isOfClassType(annot.getAnnotationType().getType(), categoryAnnotation)) { |
109 | | - if(annot.getArgs() == null) return Stream.empty(); |
110 | | - |
111 | | - AtomicInteger index = new AtomicInteger(0); |
112 | | - |
113 | | - Expression value = annot.getArgs().getArgs().iterator().next(); |
114 | | - //create stream of the values depending on what the value type is |
115 | | - Stream<Expression> categories = value instanceof J.NewArray ? |
116 | | - ((J.NewArray) value).getInitializer().getElements().stream() : |
117 | | - Stream.of(value); |
118 | | - |
119 | | - return categories |
120 | | - .map(arg -> { |
121 | | - J.Annotation annotation = |
122 | | - J.Annotation.buildAnnotation( |
123 | | - annot.getFormatting(), |
124 | | - tagType, |
125 | | - Collections.singletonList(arg.withPrefix("")) |
126 | | - ); |
127 | | - |
128 | | - if (index.getAndIncrement() != 0) { |
129 | | - annotation = annotation.withPrefix("\n"); |
130 | | - andThen(new AutoFormat(annotation)); |
131 | | - } |
132 | | - |
133 | | - return annotation; |
134 | | - }); |
| 100 | + .flatMap(this::categoryAnnotationToTagAnnotations) |
| 101 | + .map(annotation -> { |
| 102 | + andThen(new AutoFormat(annotation)); |
| 103 | + if (index.getAndIncrement() != 0) { |
| 104 | + return annotation.withPrefix("\n"); |
| 105 | + } else { |
| 106 | + return annotation; |
135 | 107 | } |
136 | | - return Stream.of(annot); |
137 | 108 | }) |
138 | 109 | .collect(Collectors.toList())); |
139 | | - } |
140 | | - maybeRemoveImport(categoryAnnotation); |
141 | | - maybeAddImport(tagType); |
142 | 110 |
|
| 111 | + maybeRemoveImport(categoryAnnotation); |
| 112 | + maybeAddImport(tagType); |
| 113 | + } |
143 | 114 | return m; |
144 | 115 | } |
| 116 | + |
| 117 | + private Stream<J.Annotation> categoryAnnotationToTagAnnotations(J.Annotation maybeCategory) { |
| 118 | + if(TypeUtils.isOfClassType(maybeCategory.getAnnotationType().getType(), categoryAnnotation)) { |
| 119 | + Expression annotationArgument = maybeCategory.getArgs().getArgs().iterator().next(); |
| 120 | + |
| 121 | + Stream<J.FieldAccess> categories; |
| 122 | + if (annotationArgument instanceof J.NewArray) { |
| 123 | + categories = ((J.NewArray) annotationArgument).getInitializer().getElements().stream() |
| 124 | + .map(J.FieldAccess.class::cast); |
| 125 | + } else { |
| 126 | + categories = Stream.of((J.FieldAccess) annotationArgument); |
| 127 | + } |
| 128 | + |
| 129 | + return categories.map(category -> { |
| 130 | + String targetName = ((J.Ident) category.getTarget()).getSimpleName(); |
| 131 | + J.Literal tagValue = new J.Literal( |
| 132 | + randomId(), |
| 133 | + targetName, |
| 134 | + "\"" + targetName + "\"", |
| 135 | + JavaType.Primitive.String, |
| 136 | + Formatting.EMPTY); |
| 137 | + J.Annotation tagAnnotation = J.Annotation.buildAnnotation( |
| 138 | + maybeCategory.getFormatting(), |
| 139 | + tagType, |
| 140 | + Collections.singletonList(tagValue) |
| 141 | + ); |
| 142 | + maybeRemoveImport(TypeUtils.asFullyQualified(category.getTarget().getType())); |
| 143 | + return tagAnnotation; |
| 144 | + }); |
| 145 | + } else { |
| 146 | + return Stream.of(maybeCategory); |
| 147 | + } |
| 148 | + } |
145 | 149 | } |
146 | 150 | } |
0 commit comments