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.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    @Override
059    public int[] getAcceptableTokens() {
060        return getRequiredTokens();
061    }
062
063    @Override
064    public int[] getDefaultTokens() {
065        return getRequiredTokens();
066    }
067
068    @Override
069    public int[] getRequiredTokens() {
070        return new int[] {
071            TokenTypes.LITERAL_DEFAULT,
072        };
073    }
074
075    /**
076     * Setter to control whether to allow {@code default} along with
077     * {@code case} if they are not last.
078     *
079     * @param newValue whether to ignore checking.
080     * @since 7.7
081     */
082    public void setSkipIfLastAndSharedWithCase(boolean newValue) {
083        skipIfLastAndSharedWithCase = newValue;
084    }
085
086    @Override
087    public void visitToken(DetailAST ast) {
088        final DetailAST defaultGroupAST = ast.getParent();
089
090        // Switch rules are not subject to fall through.
091        final boolean isSwitchRule = defaultGroupAST.getType() == TokenTypes.SWITCH_RULE;
092
093        if (skipIfLastAndSharedWithCase && !isSwitchRule) {
094            if (isNextSiblingOf(ast, TokenTypes.LITERAL_CASE)) {
095                log(ast, MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE);
096            }
097            else if (ast.getPreviousSibling() == null
098                && isNextSiblingOf(defaultGroupAST,
099                                                   TokenTypes.CASE_GROUP)) {
100                log(ast, MSG_KEY);
101            }
102        }
103        else if (isNextSiblingOf(defaultGroupAST,
104                                            TokenTypes.CASE_GROUP)
105                    || isNextSiblingOf(defaultGroupAST,
106                                            TokenTypes.SWITCH_RULE)) {
107            log(ast, MSG_KEY);
108        }
109    }
110
111    /**
112     * Return true only if passed tokenType in argument is found or returns false.
113     *
114     * @param ast root node.
115     * @param tokenType tokentype to be processed.
116     * @return true if desired token is found or else false.
117     */
118    private static boolean isNextSiblingOf(DetailAST ast, int tokenType) {
119        return ast.getNextSibling() != null && ast.getNextSibling().getType() == tokenType;
120    }
121
122}