View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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 that annotations are located on the same line with their targets.
32   * Verifying with this check is not good practice, but it is using by some style guides.
33   * </div>
34   *
35   * @since 8.2
36   */
37  @StatelessCheck
38  public class AnnotationOnSameLineCheck extends AbstractCheck {
39  
40      /** A key is pointing to the warning message text in "messages.properties" file. */
41      public static final String MSG_KEY_ANNOTATION_ON_SAME_LINE = "annotation.same.line";
42  
43      @Override
44      public int[] getDefaultTokens() {
45          return new int[] {
46              TokenTypes.CLASS_DEF,
47              TokenTypes.INTERFACE_DEF,
48              TokenTypes.ENUM_DEF,
49              TokenTypes.METHOD_DEF,
50              TokenTypes.CTOR_DEF,
51              TokenTypes.VARIABLE_DEF,
52              TokenTypes.RECORD_DEF,
53              TokenTypes.COMPACT_CTOR_DEF,
54          };
55      }
56  
57      @Override
58      public int[] getAcceptableTokens() {
59          return new int[] {
60              TokenTypes.CLASS_DEF,
61              TokenTypes.INTERFACE_DEF,
62              TokenTypes.ENUM_DEF,
63              TokenTypes.METHOD_DEF,
64              TokenTypes.CTOR_DEF,
65              TokenTypes.VARIABLE_DEF,
66              TokenTypes.PARAMETER_DEF,
67              TokenTypes.ANNOTATION_DEF,
68              TokenTypes.TYPECAST,
69              TokenTypes.LITERAL_THROWS,
70              TokenTypes.IMPLEMENTS_CLAUSE,
71              TokenTypes.TYPE_ARGUMENT,
72              TokenTypes.LITERAL_NEW,
73              TokenTypes.DOT,
74              TokenTypes.ANNOTATION_FIELD_DEF,
75              TokenTypes.RECORD_DEF,
76              TokenTypes.COMPACT_CTOR_DEF,
77          };
78      }
79  
80      @Override
81      public int[] getRequiredTokens() {
82          return CommonUtil.EMPTY_INT_ARRAY;
83      }
84  
85      @Override
86      public void visitToken(DetailAST ast) {
87          DetailAST nodeWithAnnotations = ast;
88          if (ast.getType() == TokenTypes.TYPECAST) {
89              nodeWithAnnotations = ast.findFirstToken(TokenTypes.TYPE);
90          }
91          DetailAST modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.MODIFIERS);
92          if (modifiersNode == null) {
93              modifiersNode = nodeWithAnnotations.findFirstToken(TokenTypes.ANNOTATIONS);
94          }
95          if (modifiersNode != null) {
96              for (DetailAST annotationNode = modifiersNode.getFirstChild();
97                      annotationNode != null;
98                      annotationNode = annotationNode.getNextSibling()) {
99                  if (annotationNode.getType() == TokenTypes.ANNOTATION
100                         && !TokenUtil.areOnSameLine(annotationNode,
101                         modifiersNode.getNextSibling())) {
102                     log(annotationNode, MSG_KEY_ANNOTATION_ON_SAME_LINE,
103                           getAnnotationName(annotationNode));
104                 }
105             }
106         }
107     }
108 
109     /**
110      * Returns the name of the given annotation.
111      *
112      * @param annotation annotation node.
113      * @return annotation name.
114      */
115     private static String getAnnotationName(DetailAST annotation) {
116         DetailAST identNode = annotation.findFirstToken(TokenTypes.IDENT);
117         if (identNode == null) {
118             identNode = annotation.findFirstToken(TokenTypes.DOT).getLastChild();
119         }
120         return identNode.getText();
121     }
122 
123 }