001/////////////////////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code and other text files for adherence to a set of rules. 003// Copyright (C) 2001-2025 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.whitespace; 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.TokenUtil; 027 028/** 029 * <div> 030 * Checks that there is no whitespace before the colon in a switch block. 031 * </div> 032 * 033 * @since 8.45 034 */ 035@StatelessCheck 036public class NoWhitespaceBeforeCaseDefaultColonCheck 037 extends AbstractCheck { 038 039 /** 040 * A key is pointing to the warning message text in "messages.properties" 041 * file. 042 */ 043 public static final String MSG_KEY = "ws.preceded"; 044 045 @Override 046 public int[] getDefaultTokens() { 047 return getRequiredTokens(); 048 } 049 050 @Override 051 public int[] getAcceptableTokens() { 052 return getRequiredTokens(); 053 } 054 055 @Override 056 public int[] getRequiredTokens() { 057 return new int[] {TokenTypes.COLON}; 058 } 059 060 @Override 061 public boolean isCommentNodesRequired() { 062 return true; 063 } 064 065 @Override 066 public void visitToken(DetailAST ast) { 067 if (isInSwitch(ast) && isWhiteSpaceBeforeColon(ast)) { 068 log(ast, MSG_KEY, ast.getText()); 069 } 070 } 071 072 /** 073 * Checks if the colon is inside a switch block. 074 * 075 * @param colonAst DetailAST to check. 076 * @return true, if colon case is inside a switch block. 077 */ 078 private static boolean isInSwitch(DetailAST colonAst) { 079 return TokenUtil.isOfType(colonAst.getParent(), TokenTypes.LITERAL_CASE, 080 TokenTypes.LITERAL_DEFAULT); 081 } 082 083 /** 084 * Checks if there is a whitespace before the colon of a switch case or switch default. 085 * 086 * @param colonAst DetailAST to check. 087 * @return true, if there is whitespace preceding colonAst. 088 */ 089 private static boolean isWhiteSpaceBeforeColon(DetailAST colonAst) { 090 final DetailAST parent = colonAst.getParent(); 091 final boolean result; 092 if (isOnDifferentLineWithPreviousToken(colonAst)) { 093 result = true; 094 } 095 else if (parent.getType() == TokenTypes.LITERAL_CASE) { 096 result = isWhitespaceBeforeColonOfCase(colonAst); 097 } 098 else { 099 result = isWhitespaceBeforeColonOfDefault(colonAst); 100 } 101 return result; 102 } 103 104 /** 105 * Checks if there is a whitespace before the colon of a switch case. 106 * 107 * @param colonAst DetailAST to check. 108 * @return true, if there is whitespace preceding colonAst. 109 */ 110 private static boolean isWhitespaceBeforeColonOfCase(DetailAST colonAst) { 111 final DetailAST previousSibling = colonAst.getPreviousSibling(); 112 int offset = 0; 113 if (previousSibling.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) { 114 offset = 1; 115 } 116 return colonAst.getColumnNo() != getLastColumnNumberOf(previousSibling) + offset; 117 } 118 119 /** 120 * Checks if there is a whitespace before the colon of a switch default. 121 * 122 * @param colonAst DetailAST to check. 123 * @return true, if there is whitespace preceding colonAst. 124 */ 125 private static boolean isWhitespaceBeforeColonOfDefault(DetailAST colonAst) { 126 final boolean result; 127 final DetailAST previousSibling = colonAst.getPreviousSibling(); 128 if (previousSibling == null) { 129 final DetailAST literalDefault = colonAst.getParent(); 130 result = colonAst.getColumnNo() 131 != literalDefault.getColumnNo() + literalDefault.getText().length(); 132 } 133 else { 134 result = 135 colonAst.getColumnNo() != getLastColumnNumberOf(previousSibling) + 1; 136 } 137 return result; 138 } 139 140 /** 141 * Checks if the colon is on same line as of case or default. 142 * 143 * @param colonAst DetailAST to check. 144 * @return true, if colon case is in different line as of case or default. 145 */ 146 private static boolean isOnDifferentLineWithPreviousToken(DetailAST colonAst) { 147 final DetailAST previousSibling; 148 final DetailAST parent = colonAst.getParent(); 149 if (parent.getType() == TokenTypes.LITERAL_CASE) { 150 previousSibling = colonAst.getPreviousSibling(); 151 } 152 else { 153 previousSibling = colonAst.getParent(); 154 } 155 return !TokenUtil.areOnSameLine(previousSibling, colonAst); 156 } 157 158 /** 159 * Returns the last column number of an ast. 160 * 161 * @param ast DetailAST to check. 162 * @return ast's last column number. 163 */ 164 private static int getLastColumnNumberOf(DetailAST ast) { 165 DetailAST lastChild = ast; 166 while (lastChild.hasChildren()) { 167 lastChild = lastChild.getLastChild(); 168 } 169 return lastChild.getColumnNo() + lastChild.getText().length(); 170 } 171 172}