forked from checkstyle/checkstyle
/
AnnotationLocationCheck.java
427 lines (394 loc) · 15.5 KB
/
AnnotationLocationCheck.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2019 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.checks.annotation;
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
/**
* Check location of annotation on language elements.
* By default, Check enforce to locate annotations immediately after
* documentation block and before target element, annotation should be located
* on separate line from target element.
* <p>
* Attention: Annotations among modifiers are ignored (looks like false-negative)
* as there might be a problem with annotations for return types.
* </p>
* <pre>public @Nullable Long getStartTimeOrNull() { ... }</pre>.
* <p>
* Such annotations are better to keep close to type.
* Due to limitations, Checkstyle can not examine the target of an annotation.
* </p>
*
* <p>
* Example:
* </p>
*
* <pre>
* @Override
* @Nullable
* public String getNameIfPresent() { ... }
* </pre>
*
* <p>
* The check has the following options:
* </p>
* <ul>
* <li>allowSamelineMultipleAnnotations - to allow annotation to be located on
* the same line as the target element. Default value is false.
* </li>
*
* <li>
* allowSamelineSingleParameterlessAnnotation - to allow single parameterless
* annotation to be located on the same line as the target element. Default value is false.
* </li>
*
* <li>
* allowSamelineParameterizedAnnotation - to allow parameterized annotation
* to be located on the same line as the target element. Default value is false.
* </li>
* </ul>
* <br>
* <p>
* Example to allow single parameterless annotation on the same line:
* </p>
* <pre>
* @Override public int hashCode() { ... }
* </pre>
*
* <p>Use the following configuration:
* <pre>
* <module name="AnnotationLocation">
* <property name="allowSamelineMultipleAnnotations" value="false"/>
* <property name="allowSamelineSingleParameterlessAnnotation"
* value="true"/>
* <property name="allowSamelineParameterizedAnnotation" value="false"
* />
* </module>
* </pre>
* <br>
* <p>
* Example to allow multiple parameterized annotations on the same line:
* </p>
* <pre>
* @SuppressWarnings("deprecation") @Mock DataLoader loader;
* </pre>
*
* <p>Use the following configuration:
* <pre>
* <module name="AnnotationLocation">
* <property name="allowSamelineMultipleAnnotations" value="true"/>
* <property name="allowSamelineSingleParameterlessAnnotation"
* value="true"/>
* <property name="allowSamelineParameterizedAnnotation" value="true"
* />
* </module>
* </pre>
* <br>
* <p>
* Example to allow multiple parameterless annotations on the same line:
* </p>
* <pre>
* @Partial @Mock DataLoader loader;
* </pre>
*
* <p>Use the following configuration:
* <pre>
* <module name="AnnotationLocation">
* <property name="allowSamelineMultipleAnnotations" value="true"/>
* <property name="allowSamelineSingleParameterlessAnnotation"
* value="true"/>
* <property name="allowSamelineParameterizedAnnotation" value="false"
* />
* </module>
* </pre>
* <br>
* <p>
* The following example demonstrates how the check validates annotation of method parameters,
* catch parameters, foreach, for-loop variable definitions.
* </p>
*
* <p>Configuration:
* <pre>
* <module name="AnnotationLocation">
* <property name="allowSamelineMultipleAnnotations" value="false"/>
* <property name="allowSamelineSingleParameterlessAnnotation"
* value="false"/>
* <property name="allowSamelineParameterizedAnnotation" value="false"
* />
* <property name="tokens" value="VARIABLE_DEF, PARAMETER_DEF"/>
* </module>
* </pre>
*
* <p>Code example
* {@code
* ...
* public void test(@MyAnnotation String s) { // OK
* ...
* for (@MyAnnotation char c : s.toCharArray()) { ... } // OK
* ...
* try { ... }
* catch (@MyAnnotation Exception ex) { ... } // OK
* ...
* for (@MyAnnotation int i = 0; i < 10; i++) { ... } // OK
* ...
* MathOperation c = (@MyAnnotation int a, @MyAnnotation int b) -> a + b; // OK
* ...
* }
* }
*
*/
@StatelessCheck
public class AnnotationLocationCheck extends AbstractCheck {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_LOCATION_ALONE = "annotation.location.alone";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_ANNOTATION_LOCATION = "annotation.location";
/** Array of single line annotation parents. */
private static final int[] SINGLELINE_ANNOTATION_PARENTS = {TokenTypes.FOR_EACH_CLAUSE,
TokenTypes.PARAMETER_DEF,
TokenTypes.FOR_INIT, };
/**
* If true, it allows single prameterless annotation to be located on the same line as
* target element.
*/
private boolean allowSamelineSingleParameterlessAnnotation = true;
/**
* If true, it allows parameterized annotation to be located on the same line as
* target element.
*/
private boolean allowSamelineParameterizedAnnotation;
/**
* If true, it allows annotation to be located on the same line as
* target element.
*/
private boolean allowSamelineMultipleAnnotations;
/**
* Sets if allow same line single parameterless annotation.
* @param allow User's value of allowSamelineSingleParameterlessAnnotation.
*/
public final void setAllowSamelineSingleParameterlessAnnotation(boolean allow) {
allowSamelineSingleParameterlessAnnotation = allow;
}
/**
* Sets if allow parameterized annotation to be located on the same line as
* target element.
* @param allow User's value of allowSamelineParameterizedAnnotation.
*/
public final void setAllowSamelineParameterizedAnnotation(boolean allow) {
allowSamelineParameterizedAnnotation = allow;
}
/**
* Sets if allow annotation to be located on the same line as
* target element.
* @param allow User's value of allowSamelineMultipleAnnotations.
*/
public final void setAllowSamelineMultipleAnnotations(boolean allow) {
allowSamelineMultipleAnnotations = allow;
}
@Override
public int[] getDefaultTokens() {
return new int[] {
TokenTypes.CLASS_DEF,
TokenTypes.INTERFACE_DEF,
TokenTypes.ENUM_DEF,
TokenTypes.METHOD_DEF,
TokenTypes.CTOR_DEF,
TokenTypes.VARIABLE_DEF,
};
}
@Override
public int[] getAcceptableTokens() {
return new int[] {
TokenTypes.CLASS_DEF,
TokenTypes.INTERFACE_DEF,
TokenTypes.ENUM_DEF,
TokenTypes.METHOD_DEF,
TokenTypes.CTOR_DEF,
TokenTypes.VARIABLE_DEF,
TokenTypes.PARAMETER_DEF,
TokenTypes.ANNOTATION_DEF,
TokenTypes.ANNOTATION_FIELD_DEF,
};
}
@Override
public int[] getRequiredTokens() {
return CommonUtil.EMPTY_INT_ARRAY;
}
@Override
public void visitToken(DetailAST ast) {
final DetailAST modifiersNode = ast.findFirstToken(TokenTypes.MODIFIERS);
checkAnnotations(modifiersNode, getExpectedAnnotationIndentation(modifiersNode));
}
/**
* Returns an expected annotation indentation.
* The expected indentation should be the same as the indentation of the node
* which is the parent of the target modifier node.
* @param modifierNode modifier node.
* @return the annotation indentation.
*/
private static int getExpectedAnnotationIndentation(DetailAST modifierNode) {
return modifierNode.getParent().getColumnNo();
}
/**
* Checks annotations positions in code:
* 1) Checks whether the annotations locations are correct.
* 2) Checks whether the annotations have the valid indentation level.
* @param modifierNode modifiers node.
* @param correctIndentation correct indentation of the annotation.
*/
private void checkAnnotations(DetailAST modifierNode, int correctIndentation) {
DetailAST annotation = modifierNode.getFirstChild();
while (annotation != null && annotation.getType() == TokenTypes.ANNOTATION) {
final boolean hasParameters = isParameterized(annotation);
if (!isCorrectLocation(annotation, hasParameters)) {
log(annotation.getLineNo(),
MSG_KEY_ANNOTATION_LOCATION_ALONE, getAnnotationName(annotation));
}
else if (annotation.getColumnNo() != correctIndentation && !hasNodeBefore(annotation)) {
log(annotation.getLineNo(), MSG_KEY_ANNOTATION_LOCATION,
getAnnotationName(annotation), annotation.getColumnNo(), correctIndentation);
}
annotation = annotation.getNextSibling();
}
}
/**
* Checks whether an annotation has parameters.
* @param annotation annotation node.
* @return true if the annotation has parameters.
*/
private static boolean isParameterized(DetailAST annotation) {
return TokenUtil.findFirstTokenByPredicate(annotation, ast -> {
return ast.getType() == TokenTypes.EXPR
|| ast.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR;
}).isPresent();
}
/**
* Returns the name of the given annotation.
* @param annotation annotation node.
* @return annotation name.
*/
private static String getAnnotationName(DetailAST annotation) {
DetailAST identNode = annotation.findFirstToken(TokenTypes.IDENT);
if (identNode == null) {
identNode = annotation.findFirstToken(TokenTypes.DOT).findFirstToken(TokenTypes.IDENT);
}
return identNode.getText();
}
/**
* Checks whether an annotation has a correct location.
* Annotation location is considered correct
* if {@link AnnotationLocationCheck#allowSamelineMultipleAnnotations} is set to true.
* The method also:
* 1) checks parameterized annotation location considering
* the value of {@link AnnotationLocationCheck#allowSamelineParameterizedAnnotation};
* 2) checks parameterless annotation location considering
* the value of {@link AnnotationLocationCheck#allowSamelineSingleParameterlessAnnotation};
* 3) checks annotation location considering the elements
* of {@link AnnotationLocationCheck#SINGLELINE_ANNOTATION_PARENTS};
* @param annotation annotation node.
* @param hasParams whether an annotation has parameters.
* @return true if the annotation has a correct location.
*/
private boolean isCorrectLocation(DetailAST annotation, boolean hasParams) {
final boolean allowingCondition;
if (hasParams) {
allowingCondition = allowSamelineParameterizedAnnotation;
}
else {
allowingCondition = allowSamelineSingleParameterlessAnnotation;
}
return allowSamelineMultipleAnnotations
|| allowingCondition && !hasNodeBefore(annotation)
|| !allowingCondition && (!hasNodeBeside(annotation)
|| isAllowedPosition(annotation, SINGLELINE_ANNOTATION_PARENTS));
}
/**
* Checks whether an annotation node has any node before on the same line.
* @param annotation annotation node.
* @return true if an annotation node has any node before on the same line.
*/
private static boolean hasNodeBefore(DetailAST annotation) {
final int annotationLineNo = annotation.getLineNo();
final DetailAST previousNode = annotation.getPreviousSibling();
return previousNode != null && annotationLineNo == previousNode.getLineNo();
}
/**
* Checks whether an annotation node has any node before or after on the same line.
* @param annotation annotation node.
* @return true if an annotation node has any node before or after on the same line.
*/
private static boolean hasNodeBeside(DetailAST annotation) {
return hasNodeBefore(annotation) || hasNodeAfter(annotation);
}
/**
* Checks whether an annotation node has any node after on the same line.
* @param annotation annotation node.
* @return true if an annotation node has any node after on the same line.
*/
private static boolean hasNodeAfter(DetailAST annotation) {
final int annotationLineNo = annotation.getLineNo();
DetailAST nextNode = annotation.getNextSibling();
if (nextNode == null) {
nextNode = annotation.getParent().getNextSibling();
}
return annotationLineNo == nextNode.getLineNo();
}
/**
* Checks whether position of annotation is allowed.
* @param annotation annotation token.
* @param allowedPositions an array of allowed annotation positions.
* @return true if position of annotation is allowed.
*/
private static boolean isAllowedPosition(DetailAST annotation, int... allowedPositions) {
boolean allowed = false;
for (int position : allowedPositions) {
if (isInSpecificCodeBlock(annotation, position)) {
allowed = true;
break;
}
}
return allowed;
}
/**
* Checks whether the scope of a node is restricted to a specific code block.
* @param node node.
* @param blockType block type.
* @return true if the scope of a node is restricted to a specific code block.
*/
private static boolean isInSpecificCodeBlock(DetailAST node, int blockType) {
boolean returnValue = false;
for (DetailAST token = node.getParent(); token != null; token = token.getParent()) {
final int type = token.getType();
if (type == blockType) {
returnValue = true;
break;
}
}
return returnValue;
}
}