1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.utils;
21
22 import java.util.List;
23 import java.util.function.Predicate;
24
25 import com.puppycrawl.tools.checkstyle.api.DetailAST;
26 import com.puppycrawl.tools.checkstyle.api.FullIdent;
27 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
28
29
30
31
32
33 public final class AnnotationUtil {
34
35
36
37
38 private static final String THE_AST_IS_NULL = "the ast is null";
39
40
41
42
43
44 private AnnotationUtil() {
45 throw new UnsupportedOperationException("do not instantiate.");
46 }
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public static boolean containsAnnotation(final DetailAST ast,
69 String annotation) {
70 return getAnnotation(ast, annotation) != null;
71 }
72
73
74
75
76
77
78
79 public static boolean containsAnnotation(final DetailAST ast) {
80 if (ast == null) {
81 throw new IllegalArgumentException(THE_AST_IS_NULL);
82 }
83 final DetailAST holder = getAnnotationHolder(ast);
84 return holder != null && holder.findFirstToken(TokenTypes.ANNOTATION) != null;
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public static boolean containsAnnotation(DetailAST ast, List<String> annotations) {
102 if (ast == null) {
103 throw new IllegalArgumentException(THE_AST_IS_NULL);
104 }
105
106 if (annotations == null) {
107 throw new IllegalArgumentException("annotations cannot be null");
108 }
109
110 boolean result = false;
111
112 if (!annotations.isEmpty()) {
113 final DetailAST firstMatchingAnnotation = findFirstAnnotation(ast, annotationNode -> {
114 DetailAST identNode = annotationNode.findFirstToken(TokenTypes.IDENT);
115 if (identNode == null) {
116 identNode = annotationNode.findFirstToken(TokenTypes.DOT)
117 .findFirstToken(TokenTypes.IDENT);
118 }
119
120 return annotations.contains(identNode.getText());
121 });
122 result = firstMatchingAnnotation != null;
123 }
124
125 return result;
126 }
127
128
129
130
131
132
133
134
135
136 public static DetailAST getAnnotationHolder(DetailAST ast) {
137 if (ast == null) {
138 throw new IllegalArgumentException(THE_AST_IS_NULL);
139 }
140
141 final DetailAST annotationHolder;
142
143 if (ast.getType() == TokenTypes.ENUM_CONSTANT_DEF
144 || ast.getType() == TokenTypes.PACKAGE_DEF) {
145 annotationHolder = ast.findFirstToken(TokenTypes.ANNOTATIONS);
146 }
147 else {
148 annotationHolder = ast.findFirstToken(TokenTypes.MODIFIERS);
149 }
150
151 return annotationHolder;
152 }
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public static DetailAST getAnnotation(final DetailAST ast,
176 String annotation) {
177 if (ast == null) {
178 throw new IllegalArgumentException(THE_AST_IS_NULL);
179 }
180
181 if (annotation == null) {
182 throw new IllegalArgumentException("the annotation is null");
183 }
184
185 if (CommonUtil.isBlank(annotation)) {
186 throw new IllegalArgumentException(
187 "the annotation is empty or spaces");
188 }
189
190 return findFirstAnnotation(ast, annotationNode -> {
191 final DetailAST firstChild = annotationNode.findFirstToken(TokenTypes.AT);
192 final String name =
193 FullIdent.createFullIdent(firstChild.getNextSibling()).getText();
194 return annotation.equals(name);
195 });
196 }
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212 private static DetailAST findFirstAnnotation(final DetailAST ast,
213 Predicate<DetailAST> predicate) {
214 final DetailAST holder = getAnnotationHolder(ast);
215 DetailAST result = null;
216 for (DetailAST child = holder.getFirstChild();
217 child != null; child = child.getNextSibling()) {
218 if (child.getType() == TokenTypes.ANNOTATION && predicate.test(child)) {
219 result = child;
220 break;
221 }
222 }
223
224 return result;
225 }
226
227 }