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