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