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