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