-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
AnnotationUtil.java
270 lines (239 loc) · 9.52 KB
/
AnnotationUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.utils;
import java.util.Set;
import java.util.function.Predicate;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FullIdent;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
/**
* Contains utility methods designed to work with annotations.
*
*/
public final class AnnotationUtil {
/**
* Common message.
*/
private static final String THE_AST_IS_NULL = "the ast is null";
/** {@link Override Override} annotation name. */
private static final String OVERRIDE = "Override";
/** Fully-qualified {@link Override Override} annotation name. */
private static final String FQ_OVERRIDE = "java.lang." + OVERRIDE;
/** Simple and fully-qualified {@link Override Override} annotation names. */
private static final Set<String> OVERRIDE_ANNOTATIONS = Set.of(OVERRIDE, FQ_OVERRIDE);
/**
* Private utility constructor.
*
* @throws UnsupportedOperationException if called
*/
private AnnotationUtil() {
throw new UnsupportedOperationException("do not instantiate.");
}
/**
* Checks if the AST is annotated with the passed in annotation.
*
* <p>
* This method will not look for imports or package
* statements to detect the passed in annotation.
* </p>
*
* <p>
* To check if an AST contains a passed in annotation
* taking into account fully-qualified names
* (ex: java.lang.Override, Override)
* this method will need to be called twice. Once for each
* name given.
* </p>
*
* @param ast the current node
* @param annotation the annotation name to check for
* @return true if contains the annotation
*/
public static boolean containsAnnotation(final DetailAST ast,
String annotation) {
return getAnnotation(ast, annotation) != null;
}
/**
* Checks if the AST is annotated with any annotation.
*
* @param ast the current node
* @return {@code true} if the AST contains at least one annotation
* @throws IllegalArgumentException when ast is null
*/
public static boolean containsAnnotation(final DetailAST ast) {
if (ast == null) {
throw new IllegalArgumentException(THE_AST_IS_NULL);
}
final DetailAST holder = getAnnotationHolder(ast);
return holder != null && holder.findFirstToken(TokenTypes.ANNOTATION) != null;
}
/**
* Checks if the given AST element is annotated with any of the specified annotations.
*
* <p>
* This method accepts both simple and fully-qualified names,
* e.g. "Override" will match both java.lang.Override and Override.
* </p>
*
* @param ast The type or method definition.
* @param annotations A collection of annotations to look for.
* @return {@code true} if the given AST element is annotated with
* at least one of the specified annotations;
* {@code false} otherwise.
* @throws IllegalArgumentException when ast or annotations are null
*/
public static boolean containsAnnotation(DetailAST ast, Set<String> annotations) {
if (ast == null) {
throw new IllegalArgumentException(THE_AST_IS_NULL);
}
if (annotations == null) {
throw new IllegalArgumentException("annotations cannot be null");
}
boolean result = false;
if (!annotations.isEmpty()) {
final DetailAST firstMatchingAnnotation = findFirstAnnotation(ast, annotationNode -> {
final String annotationFullIdent = getAnnotationFullIdent(annotationNode);
return annotations.contains(annotationFullIdent);
});
result = firstMatchingAnnotation != null;
}
return result;
}
/**
* Gets the full ident text of the annotation AST.
*
* @param annotationNode The annotation AST.
* @return The full ident text.
*/
private static String getAnnotationFullIdent(DetailAST annotationNode) {
final DetailAST identNode = annotationNode.findFirstToken(TokenTypes.IDENT);
final String annotationString;
// If no `IDENT` is found, then we have a `DOT` -> more than 1 qualifier
if (identNode == null) {
final DetailAST dotNode = annotationNode.findFirstToken(TokenTypes.DOT);
annotationString = FullIdent.createFullIdent(dotNode).getText();
}
else {
annotationString = identNode.getText();
}
return annotationString;
}
/**
* Checks if the AST is annotated with {@code Override} or
* {@code java.lang.Override} annotation.
*
* @param ast the current node
* @return {@code true} if the AST contains Override annotation
* @throws IllegalArgumentException when ast is null
*/
public static boolean hasOverrideAnnotation(DetailAST ast) {
return containsAnnotation(ast, OVERRIDE_ANNOTATIONS);
}
/**
* Gets the AST that holds a series of annotations for the
* potentially annotated AST. Returns {@code null}
* if the passed in AST does not have an Annotation Holder.
*
* @param ast the current node
* @return the Annotation Holder
* @throws IllegalArgumentException when ast is null
*/
public static DetailAST getAnnotationHolder(DetailAST ast) {
if (ast == null) {
throw new IllegalArgumentException(THE_AST_IS_NULL);
}
final DetailAST annotationHolder;
if (ast.getType() == TokenTypes.ENUM_CONSTANT_DEF
|| ast.getType() == TokenTypes.PACKAGE_DEF) {
annotationHolder = ast.findFirstToken(TokenTypes.ANNOTATIONS);
}
else {
annotationHolder = ast.findFirstToken(TokenTypes.MODIFIERS);
}
return annotationHolder;
}
/**
* Checks if the AST is annotated with the passed in annotation
* and returns the AST representing that annotation.
*
* <p>
* This method will not look for imports or package
* statements to detect the passed in annotation.
* </p>
*
* <p>
* To check if an AST contains a passed in annotation
* taking into account fully-qualified names
* (ex: java.lang.Override, Override)
* this method will need to be called twice. Once for each
* name given.
* </p>
*
* @param ast the current node
* @param annotation the annotation name to check for
* @return the AST representing that annotation
* @throws IllegalArgumentException when ast or annotations are null; when annotation is blank
*/
public static DetailAST getAnnotation(final DetailAST ast,
String annotation) {
if (ast == null) {
throw new IllegalArgumentException(THE_AST_IS_NULL);
}
if (annotation == null) {
throw new IllegalArgumentException("the annotation is null");
}
if (CommonUtil.isBlank(annotation)) {
throw new IllegalArgumentException(
"the annotation is empty or spaces");
}
return findFirstAnnotation(ast, annotationNode -> {
final DetailAST firstChild = annotationNode.findFirstToken(TokenTypes.AT);
final String name =
FullIdent.createFullIdent(firstChild.getNextSibling()).getText();
return annotation.equals(name);
});
}
/**
* Checks if the given AST is annotated with at least one annotation that
* matches the given predicate and returns the AST representing the first
* matching annotation.
*
* <p>
* This method will not look for imports or package
* statements to detect the passed in annotation.
* </p>
*
* @param ast the current node
* @param predicate The predicate which decides if an annotation matches
* @return the AST representing that annotation
*/
private static DetailAST findFirstAnnotation(final DetailAST ast,
Predicate<DetailAST> predicate) {
final DetailAST holder = getAnnotationHolder(ast);
DetailAST result = null;
for (DetailAST child = holder.getFirstChild();
child != null; child = child.getNextSibling()) {
if (child.getType() == TokenTypes.ANNOTATION && predicate.test(child)) {
result = child;
break;
}
}
return result;
}
}