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.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;
026
027/**
028 * <div>
029 * Check that the {@code default} is after all the cases in a {@code switch} statement.
030 * </div>
031 *
032 * <p>
033 * Rationale: Java allows {@code default} anywhere within the
034 * {@code switch} statement. But it is more readable if it comes after the last {@code case}.
035 * </p>
036 *
037 * @since 3.4
038 */
039@StatelessCheck
040public class DefaultComesLastCheck extends AbstractCheck {
041
042    /**
043     * A key is pointing to the warning message text in "messages.properties"
044     * file.
045     */
046    public static final String MSG_KEY = "default.comes.last";
047
048    /**
049     * A key is pointing to the warning message text in "messages.properties"
050     * file.
051     */
052    public static final String MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE =
053            "default.comes.last.in.casegroup";
054
055    /** Control whether to allow {@code default} along with {@code case} if they are not last. */
056    private boolean skipIfLastAndSharedWithCase;
057
058    /**
059     * Creates a new {@code DefaultComesLastCheck} instance.
060     */
061    public DefaultComesLastCheck() {
062        // no code by default
063    }
064
065    @Override
066    public int[] getAcceptableTokens() {
067        return getRequiredTokens();
068    }
069
070    @Override
071    public int[] getDefaultTokens() {
072        return getRequiredTokens();
073    }
074
075    @Override
076    public int[] getRequiredTokens() {
077        return new int[] {
078            TokenTypes.LITERAL_DEFAULT,
079        };
080    }
081
082    /**
083     * Setter to control whether to allow {@code default} along with
084     * {@code case} if they are not last.
085     *
086     * @param newValue whether to ignore checking.
087     * @since 7.7
088     */
089    public void setSkipIfLastAndSharedWithCase(boolean newValue) {
090        skipIfLastAndSharedWithCase = newValue;
091    }
092
093    @Override
094    public void visitToken(DetailAST ast) {
095        final DetailAST defaultGroupAST = ast.getParent();
096
097        // Switch rules are not subject to fall through.
098        final boolean isSwitchRule = defaultGroupAST.getType() == TokenTypes.SWITCH_RULE;
099
100        if (skipIfLastAndSharedWithCase && !isSwitchRule) {
101            if (isNextSiblingOf(ast, TokenTypes.LITERAL_CASE)) {
102                log(ast, MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE);
103            }
104            else if (ast.getPreviousSibling() == null
105                && isNextSiblingOf(defaultGroupAST,
106                                                   TokenTypes.CASE_GROUP)) {
107                log(ast, MSG_KEY);
108            }
109        }
110        else if (isNextSiblingOf(defaultGroupAST,
111                                            TokenTypes.CASE_GROUP)
112                    || isNextSiblingOf(defaultGroupAST,
113                                            TokenTypes.SWITCH_RULE)) {
114            log(ast, MSG_KEY);
115        }
116    }
117
118    /**
119     * Return true only if passed tokenType in argument is found or returns false.
120     *
121     * @param ast root node.
122     * @param tokenType tokentype to be processed.
123     * @return true if desired token is found or else false.
124     */
125    private static boolean isNextSiblingOf(DetailAST ast, int tokenType) {
126        return ast.getNextSibling() != null && ast.getNextSibling().getType() == tokenType;
127    }
128
129}