1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ///////////////////////////////////////////////////////////////////////////////////////////////
19
20 package com.puppycrawl.tools.checkstyle.checks.annotation;
21
22 import com.puppycrawl.tools.checkstyle.StatelessCheck;
23 import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24 import com.puppycrawl.tools.checkstyle.api.DetailAST;
25 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26 import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
27 import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
28
29 /**
30 * <div>
31 * Checks location of annotation on language elements.
32 * By default, Check enforce to locate annotations before target element,
33 * annotation should be located on separate line from target element.
34 * This check also verifies that the annotations are on the same indenting level
35 * as the annotated element if they are not on the same line.
36 * </div>
37 *
38 * <p>
39 * Attention: Elements that cannot have JavaDoc comments like local variables are not in the
40 * scope of this check even though a token type like {@code VARIABLE_DEF} would match them.
41 * </p>
42 *
43 * <p>
44 * Attention: Annotations among modifiers are ignored (looks like false-negative)
45 * as there might be a problem with annotations for return types:
46 * </p>
47 * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
48 * public @Nullable Long getStartTimeOrNull() { ... }
49 * </code></pre></div>
50 *
51 * <p>
52 * Such annotations are better to keep close to type.
53 * Due to limitations, Checkstyle can not examine the target of an annotation.
54 * </p>
55 *
56 * <p>
57 * Example:
58 * </p>
59 * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
60 * @Override
61 * @Nullable
62 * public String getNameIfPresent() { ... }
63 * </code></pre></div>
64 *
65 * <p>
66 * Notes:
67 * This check does <strong>not</strong> enforce annotations to be placed
68 * immediately after the documentation block. If that behavior is desired, consider also using
69 * <a href="https://checkstyle.org/checks/javadoc/invalidjavadocposition.html#InvalidJavadocPosition">
70 * InvalidJavadocPosition</a>.
71 * </p>
72 *
73 * <p>
74 * The property {@code allowSamelineMultipleAnnotations} has the
75 * dominant effect and allows both single and multiple annotations on
76 * the same line, regardless of whether they are parameterized or parameterless.
77 * </p>
78 *
79 * @since 6.0
80 */
81 @StatelessCheck
82 public class AnnotationLocationCheck extends AbstractCheck {
83
84 /**
85 * A key is pointing to the warning message text in "messages.properties"
86 * file.
87 */
88 public static final String MSG_KEY_ANNOTATION_LOCATION_ALONE = "annotation.location.alone";
89
90 /**
91 * A key is pointing to the warning message text in "messages.properties"
92 * file.
93 */
94 public static final String MSG_KEY_ANNOTATION_LOCATION = "annotation.location";
95
96 /**
97 * Allow single parameterless annotation to be located on the same line as
98 * target element.
99 */
100 private boolean allowSamelineSingleParameterlessAnnotation = true;
101
102 /**
103 * Allow one and only parameterized annotation to be located on the same line as
104 * target element.
105 */
106 private boolean allowSamelineParameterizedAnnotation;
107
108 /**
109 * Allow annotation(s) to be located on the same line as
110 * target element.
111 */
112 private boolean allowSamelineMultipleAnnotations;
113
114 /**
115 * Setter to allow single parameterless annotation to be located on the same line as
116 * target element.
117 *
118 * @param allow User's value of allowSamelineSingleParameterlessAnnotation.
119 * @since 6.1
120 */
121 public final void setAllowSamelineSingleParameterlessAnnotation(boolean allow) {
122 allowSamelineSingleParameterlessAnnotation = allow;
123 }
124
125 /**
126 * Setter to allow one and only parameterized annotation to be located on the same line as
127 * target element.
128 *
129 * @param allow User's value of allowSamelineParameterizedAnnotation.
130 * @since 6.4
131 */
132 public final void setAllowSamelineParameterizedAnnotation(boolean allow) {
133 allowSamelineParameterizedAnnotation = allow;
134 }
135
136 /**
137 * Setter to allow annotation(s) to be located on the same line as
138 * target element.
139 *
140 * @param allow User's value of allowSamelineMultipleAnnotations.
141 * @since 6.0
142 */
143 public final void setAllowSamelineMultipleAnnotations(boolean allow) {
144 allowSamelineMultipleAnnotations = allow;
145 }
146
147 @Override
148 public int[] getDefaultTokens() {
149 return new int[] {
150 TokenTypes.CLASS_DEF,
151 TokenTypes.INTERFACE_DEF,
152 TokenTypes.PACKAGE_DEF,
153 TokenTypes.ENUM_CONSTANT_DEF,
154 TokenTypes.ENUM_DEF,
155 TokenTypes.METHOD_DEF,
156 TokenTypes.CTOR_DEF,
157 TokenTypes.VARIABLE_DEF,
158 TokenTypes.RECORD_DEF,
159 TokenTypes.COMPACT_CTOR_DEF,
160 };
161 }
162
163 @Override
164 public int[] getAcceptableTokens() {
165 return new int[] {
166 TokenTypes.CLASS_DEF,
167 TokenTypes.INTERFACE_DEF,
168 TokenTypes.PACKAGE_DEF,
169 TokenTypes.ENUM_CONSTANT_DEF,
170 TokenTypes.ENUM_DEF,
171 TokenTypes.METHOD_DEF,
172 TokenTypes.CTOR_DEF,
173 TokenTypes.VARIABLE_DEF,
174 TokenTypes.ANNOTATION_DEF,
175 TokenTypes.ANNOTATION_FIELD_DEF,
176 TokenTypes.RECORD_DEF,
177 TokenTypes.COMPACT_CTOR_DEF,
178 };
179 }
180
181 @Override
182 public int[] getRequiredTokens() {
183 return CommonUtil.EMPTY_INT_ARRAY;
184 }
185
186 @Override
187 public void visitToken(DetailAST ast) {
188 // ignore variable def tokens that are not field definitions
189 if (ast.getType() != TokenTypes.VARIABLE_DEF
190 || ast.getParent().getType() == TokenTypes.OBJBLOCK) {
191 DetailAST node = ast.findFirstToken(TokenTypes.MODIFIERS);
192 if (node == null) {
193 node = ast.findFirstToken(TokenTypes.ANNOTATIONS);
194 }
195 checkAnnotations(node, getExpectedAnnotationIndentation(node));
196 }
197 }
198
199 /**
200 * Returns an expected annotation indentation.
201 * The expected indentation should be the same as the indentation of the target node.
202 *
203 * @param node modifiers or annotations node.
204 * @return the annotation indentation.
205 */
206 private static int getExpectedAnnotationIndentation(DetailAST node) {
207 return node.getColumnNo();
208 }
209
210 /**
211 * Checks annotations positions in code:
212 * 1) Checks whether the annotations locations are correct.
213 * 2) Checks whether the annotations have the valid indentation level.
214 *
215 * @param modifierNode modifiers node.
216 * @param correctIndentation correct indentation of the annotation.
217 */
218 private void checkAnnotations(DetailAST modifierNode, int correctIndentation) {
219 DetailAST annotation = modifierNode.getFirstChild();
220
221 while (annotation != null && annotation.getType() == TokenTypes.ANNOTATION) {
222 final boolean hasParameters = isParameterized(annotation);
223
224 if (!isCorrectLocation(annotation, hasParameters)) {
225 log(annotation,
226 MSG_KEY_ANNOTATION_LOCATION_ALONE, getAnnotationName(annotation));
227 }
228 else if (annotation.getColumnNo() != correctIndentation && !hasNodeBefore(annotation)) {
229 log(annotation, MSG_KEY_ANNOTATION_LOCATION,
230 getAnnotationName(annotation), annotation.getColumnNo(), correctIndentation);
231 }
232 annotation = annotation.getNextSibling();
233 }
234 }
235
236 /**
237 * Checks whether an annotation has parameters.
238 *
239 * @param annotation annotation node.
240 * @return true if the annotation has parameters.
241 */
242 private static boolean isParameterized(DetailAST annotation) {
243 return TokenUtil.findFirstTokenByPredicate(annotation, ast -> {
244 return ast.getType() == TokenTypes.EXPR
245 || ast.getType() == TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR;
246 }).isPresent();
247 }
248
249 /**
250 * Returns the name of the given annotation.
251 *
252 * @param annotation annotation node.
253 * @return annotation name.
254 */
255 private static String getAnnotationName(DetailAST annotation) {
256 DetailAST identNode = annotation.findFirstToken(TokenTypes.IDENT);
257 if (identNode == null) {
258 identNode = annotation.findFirstToken(TokenTypes.DOT).findFirstToken(TokenTypes.IDENT);
259 }
260 return identNode.getText();
261 }
262
263 /**
264 * Checks whether an annotation has a correct location.
265 * Annotation location is considered correct
266 * if {@link AnnotationLocationCheck#allowSamelineMultipleAnnotations} is set to true.
267 * The method also:
268 * 1) checks parameterized annotation location considering
269 * the value of {@link AnnotationLocationCheck#allowSamelineParameterizedAnnotation};
270 * 2) checks parameterless annotation location considering
271 * the value of {@link AnnotationLocationCheck#allowSamelineSingleParameterlessAnnotation};
272 * 3) checks annotation location;
273 *
274 * @param annotation annotation node.
275 * @param hasParams whether an annotation has parameters.
276 * @return true if the annotation has a correct location.
277 */
278 private boolean isCorrectLocation(DetailAST annotation, boolean hasParams) {
279 final boolean allowingCondition;
280
281 if (hasParams) {
282 allowingCondition = allowSamelineParameterizedAnnotation;
283 }
284 else {
285 allowingCondition = allowSamelineSingleParameterlessAnnotation;
286 }
287 return allowSamelineMultipleAnnotations
288 || allowingCondition && !hasNodeBefore(annotation)
289 || !hasNodeBeside(annotation);
290 }
291
292 /**
293 * Checks whether an annotation node has any node before on the same line.
294 *
295 * @param annotation annotation node.
296 * @return true if an annotation node has any node before on the same line.
297 */
298 private static boolean hasNodeBefore(DetailAST annotation) {
299 final int annotationLineNo = annotation.getLineNo();
300 final DetailAST previousNode = annotation.getPreviousSibling();
301
302 return previousNode != null && annotationLineNo == previousNode.getLineNo();
303 }
304
305 /**
306 * Checks whether an annotation node has any node before or after on the same line.
307 *
308 * @param annotation annotation node.
309 * @return true if an annotation node has any node before or after on the same line.
310 */
311 private static boolean hasNodeBeside(DetailAST annotation) {
312 return hasNodeBefore(annotation) || hasNodeAfter(annotation);
313 }
314
315 /**
316 * Checks whether an annotation node has any node after on the same line.
317 *
318 * @param annotation annotation node.
319 * @return true if an annotation node has any node after on the same line.
320 */
321 private static boolean hasNodeAfter(DetailAST annotation) {
322 final int annotationLineNo = annotation.getLineNo();
323 DetailAST nextNode = annotation.getNextSibling();
324
325 if (nextNode == null) {
326 nextNode = annotation.getParent().getNextSibling();
327 }
328
329 return annotationLineNo == nextNode.getLineNo();
330 }
331
332 }