001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks.coding; 021 022import com.puppycrawl.tools.checkstyle.StatelessCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026import com.puppycrawl.tools.checkstyle.utils.CheckUtil; 027import com.puppycrawl.tools.checkstyle.utils.TokenUtil; 028 029/** 030 * <p> 031 * Checks that each variable declaration is in its own statement 032 * and on its own line. 033 * </p> 034 * <p> 035 * Rationale: <a 036 * href="https://checkstyle.org/styleguides/sun-code-conventions-19990420/CodeConventions.doc5.html#a2992"> 037 * the Java code conventions chapter 6.1</a> recommends that 038 * declarations should be one per line/statement. 039 * </p> 040 * <p> 041 * To configure the check: 042 * </p> 043 * <pre> 044 * <module name="MultipleVariableDeclarations"/> 045 * </pre> 046 * 047 * @since 3.4 048 */ 049@StatelessCheck 050public class MultipleVariableDeclarationsCheck extends AbstractCheck { 051 052 /** 053 * A key is pointing to the warning message text in "messages.properties" 054 * file. 055 */ 056 public static final String MSG_MULTIPLE = "multiple.variable.declarations"; 057 058 /** 059 * A key is pointing to the warning message text in "messages.properties" 060 * file. 061 */ 062 public static final String MSG_MULTIPLE_COMMA = "multiple.variable.declarations.comma"; 063 064 @Override 065 public int[] getAcceptableTokens() { 066 return getRequiredTokens(); 067 } 068 069 @Override 070 public int[] getDefaultTokens() { 071 return getRequiredTokens(); 072 } 073 074 @Override 075 public int[] getRequiredTokens() { 076 return new int[] {TokenTypes.VARIABLE_DEF}; 077 } 078 079 @Override 080 public void visitToken(DetailAST ast) { 081 DetailAST nextNode = ast.getNextSibling(); 082 083 if (nextNode != null) { 084 final boolean isCommaSeparated = nextNode.getType() == TokenTypes.COMMA; 085 086 if (isCommaSeparated 087 || nextNode.getType() == TokenTypes.SEMI) { 088 nextNode = nextNode.getNextSibling(); 089 } 090 091 if (nextNode != null 092 && nextNode.getType() == TokenTypes.VARIABLE_DEF) { 093 final DetailAST firstNode = CheckUtil.getFirstNode(ast); 094 if (isCommaSeparated) { 095 // Check if the multiple variable declarations are in a 096 // for loop initializer. If they are, then no warning 097 // should be displayed. Declaring multiple variables in 098 // a for loop initializer is a good way to minimize 099 // variable scope. Refer Feature Request Id - 2895985 100 // for more details 101 if (ast.getParent().getType() != TokenTypes.FOR_INIT) { 102 log(firstNode, MSG_MULTIPLE_COMMA); 103 } 104 } 105 else { 106 final DetailAST lastNode = getLastNode(ast); 107 final DetailAST firstNextNode = CheckUtil.getFirstNode(nextNode); 108 109 if (TokenUtil.areOnSameLine(firstNextNode, lastNode)) { 110 log(firstNode, MSG_MULTIPLE); 111 } 112 } 113 } 114 } 115 } 116 117 /** 118 * Finds sub-node for given node maximum (line, column) pair. 119 * 120 * @param node the root of tree for search. 121 * @return sub-node with maximum (line, column) pair. 122 */ 123 private static DetailAST getLastNode(final DetailAST node) { 124 DetailAST currentNode = node; 125 final DetailAST child = node.getLastChild(); 126 if (child != null) { 127 currentNode = getLastNode(child); 128 } 129 130 return currentNode; 131 } 132 133}