|
5 | 5 | import com.azure.tools.apiview.processor.model.Diagnostic; |
6 | 6 | import com.github.javaparser.ast.CompilationUnit; |
7 | 7 | import com.github.javaparser.ast.expr.AnnotationExpr; |
| 8 | +import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations; |
8 | 9 |
|
9 | 10 | import java.util.*; |
10 | | -import java.util.function.Consumer; |
11 | 11 |
|
12 | 12 | import static com.azure.tools.apiview.processor.analysers.util.ASTUtils.*; |
13 | 13 | import static com.azure.tools.apiview.processor.model.DiagnosticKind.WARNING; |
|
18 | 18 | */ |
19 | 19 | public class BadAnnotationDiagnosticRule implements DiagnosticRule { |
20 | 20 |
|
21 | | - private List<BadAnnotation> badAnnotations; |
| 21 | + private final List<BadAnnotation> badAnnotations; |
22 | 22 |
|
23 | 23 | public BadAnnotationDiagnosticRule(BadAnnotation... badAnnotations) { |
24 | 24 | this.badAnnotations = Arrays.asList(badAnnotations); |
25 | 25 | } |
26 | 26 |
|
27 | 27 | @Override |
28 | 28 | public void scanIndividual(final CompilationUnit cu, final APIListing listing) { |
29 | | - Consumer<AnnotationExpr> annotationConsumer = a -> { |
30 | | - badAnnotations.forEach(badAnnotation -> { |
31 | | - // check if the badAnnotation is the same as the found annotation |
32 | | - if (badAnnotation.annotation.equals(a.getNameAsString())) { |
33 | | - // we've got a match - file it as a diagnostic |
34 | | - listing.addDiagnostic(new Diagnostic(WARNING, makeId(a), badAnnotation.errorMessage)); |
35 | | - } |
36 | | - }); |
37 | | - }; |
38 | 29 |
|
39 | 30 | getClasses(cu).forEach(typeDeclaration -> { |
40 | 31 | // check annotations on the type itself |
41 | | - typeDeclaration.getAnnotations().stream().forEach(annotationConsumer); |
| 32 | + typeDeclaration.getAnnotations() |
| 33 | + .forEach(annotation -> checkForBadAnnotations(listing, typeDeclaration, annotation)); |
42 | 34 |
|
43 | | - // check annotations on fields, constructors, methods |
44 | | - getPublicOrProtectedFields(typeDeclaration).flatMap(method -> method.getAnnotations().stream()).forEach(annotationConsumer); |
45 | | - getPublicOrProtectedConstructors(typeDeclaration).flatMap(method -> method.getAnnotations().stream()).forEach(annotationConsumer); |
46 | | - getPublicOrProtectedMethods(typeDeclaration).flatMap(method -> method.getAnnotations().stream()).forEach(annotationConsumer); |
| 35 | + // check annotations on fields |
| 36 | + getPublicOrProtectedFields(typeDeclaration) |
| 37 | + .forEach(field -> field.getAnnotations() |
| 38 | + .forEach(annotation -> checkForBadAnnotations(listing, field, annotation))); |
| 39 | + |
| 40 | + // check annotations on constructors |
| 41 | + getPublicOrProtectedConstructors(typeDeclaration) |
| 42 | + .forEach(constructor -> constructor.getAnnotations() |
| 43 | + .forEach(annotation -> checkForBadAnnotations(listing, constructor, annotation))); |
| 44 | + // check annotations on methods |
| 45 | + getPublicOrProtectedMethods(typeDeclaration) |
| 46 | + .forEach(method -> method.getAnnotations() |
| 47 | + .forEach(annotation -> checkForBadAnnotations(listing, method, annotation))); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + private void checkForBadAnnotations(APIListing listing, NodeWithAnnotations<?> nodeWithAnnotations, AnnotationExpr annotation) { |
| 52 | + badAnnotations.forEach(badAnnotation -> { |
| 53 | + // check if the badAnnotation is the same as the found annotation |
| 54 | + if (badAnnotation.annotation.equals(annotation.getNameAsString())) { |
| 55 | + // we've got a match - file it as a diagnostic |
| 56 | + listing.addDiagnostic(new Diagnostic(WARNING, makeId(annotation, nodeWithAnnotations), |
| 57 | + badAnnotation.errorMessage)); |
| 58 | + } |
47 | 59 | }); |
48 | 60 | } |
49 | 61 |
|
|
0 commit comments