View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.coding;
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.CheckUtil;
27  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
28  
29  /**
30   * <p>
31   * Checks that each variable declaration is in its own statement
32   * and on its own line.
33   * </p>
34   * <p>
35   * Rationale: <a
36   * href="https://checkstyle.org/styleguides/sun-code-conventions-19990420/CodeConventions.doc5.html#a2992">
37   * the Java code conventions chapter 6.1</a> recommends that
38   * declarations should be one per line/statement.
39   * </p>
40   * <p>
41   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
42   * </p>
43   * <p>
44   * Violation Message Keys:
45   * </p>
46   * <ul>
47   * <li>
48   * {@code multiple.variable.declarations}
49   * </li>
50   * <li>
51   * {@code multiple.variable.declarations.comma}
52   * </li>
53   * </ul>
54   *
55   * @since 3.4
56   */
57  @StatelessCheck
58  public class MultipleVariableDeclarationsCheck extends AbstractCheck {
59  
60      /**
61       * A key is pointing to the warning message text in "messages.properties"
62       * file.
63       */
64      public static final String MSG_MULTIPLE = "multiple.variable.declarations";
65  
66      /**
67       * A key is pointing to the warning message text in "messages.properties"
68       * file.
69       */
70      public static final String MSG_MULTIPLE_COMMA = "multiple.variable.declarations.comma";
71  
72      @Override
73      public int[] getAcceptableTokens() {
74          return getRequiredTokens();
75      }
76  
77      @Override
78      public int[] getDefaultTokens() {
79          return getRequiredTokens();
80      }
81  
82      @Override
83      public int[] getRequiredTokens() {
84          return new int[] {TokenTypes.VARIABLE_DEF};
85      }
86  
87      @Override
88      public void visitToken(DetailAST ast) {
89          DetailAST nextNode = ast.getNextSibling();
90  
91          if (nextNode != null) {
92              final boolean isCommaSeparated = nextNode.getType() == TokenTypes.COMMA;
93  
94              if (isCommaSeparated
95                  || nextNode.getType() == TokenTypes.SEMI) {
96                  nextNode = nextNode.getNextSibling();
97              }
98  
99              if (nextNode != null
100                     && nextNode.getType() == TokenTypes.VARIABLE_DEF) {
101                 final DetailAST firstNode = CheckUtil.getFirstNode(ast);
102                 if (isCommaSeparated) {
103                     // Check if the multiple variable declarations are in a
104                     // for loop initializer. If they are, then no warning
105                     // should be displayed. Declaring multiple variables in
106                     // a for loop initializer is a good way to minimize
107                     // variable scope. Refer Feature Request Id - 2895985
108                     // for more details
109                     if (ast.getParent().getType() != TokenTypes.FOR_INIT) {
110                         log(firstNode, MSG_MULTIPLE_COMMA);
111                     }
112                 }
113                 else {
114                     final DetailAST lastNode = getLastNode(ast);
115                     final DetailAST firstNextNode = CheckUtil.getFirstNode(nextNode);
116 
117                     if (TokenUtil.areOnSameLine(firstNextNode, lastNode)) {
118                         log(firstNode, MSG_MULTIPLE);
119                     }
120                 }
121             }
122         }
123     }
124 
125     /**
126      * Finds sub-node for given node maximum (line, column) pair.
127      *
128      * @param node the root of tree for search.
129      * @return sub-node with maximum (line, column) pair.
130      */
131     private static DetailAST getLastNode(final DetailAST node) {
132         DetailAST currentNode = node;
133         final DetailAST child = node.getLastChild();
134         if (child != null) {
135             currentNode = getLastNode(child);
136         }
137 
138         return currentNode;
139     }
140 
141 }