View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  
27  /**
28   * <div>
29   * Check that the {@code default} is after all the cases in a {@code switch} statement.
30   * </div>
31   *
32   * <p>
33   * Rationale: Java allows {@code default} anywhere within the
34   * {@code switch} statement. But it is more readable if it comes after the last {@code case}.
35   * </p>
36   *
37   * @since 3.4
38   */
39  @StatelessCheck
40  public class DefaultComesLastCheck extends AbstractCheck {
41  
42      /**
43       * A key is pointing to the warning message text in "messages.properties" file.
44       */
45      public static final String MSG_KEY = "default.comes.last";
46  
47      /**
48       * A key is pointing to the warning message text in "messages.properties" file.
49       */
50      public static final String MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE =
51              "default.comes.last.in.casegroup";
52  
53      /** Control whether to allow {@code default} along with {@code case} if they are not last. */
54      private boolean skipIfLastAndSharedWithCase;
55  
56      /**
57       * Creates a new {@code DefaultComesLastCheck} instance.
58       */
59      public DefaultComesLastCheck() {
60          // no code by default
61      }
62  
63      @Override
64      public int[] getAcceptableTokens() {
65          return getRequiredTokens();
66      }
67  
68      @Override
69      public int[] getDefaultTokens() {
70          return getRequiredTokens();
71      }
72  
73      @Override
74      public int[] getRequiredTokens() {
75          return new int[] {
76              TokenTypes.LITERAL_DEFAULT,
77          };
78      }
79  
80      /**
81       * Setter to control whether to allow {@code default} along with
82       * {@code case} if they are not last.
83       *
84       * @param newValue whether to ignore checking.
85       * @since 7.7
86       */
87      public void setSkipIfLastAndSharedWithCase(boolean newValue) {
88          skipIfLastAndSharedWithCase = newValue;
89      }
90  
91      @Override
92      public void visitToken(DetailAST ast) {
93          final DetailAST defaultGroupAST = ast.getParent();
94  
95          // Switch rules are not subject to fall through.
96          final boolean isSwitchRule = defaultGroupAST.getType() == TokenTypes.SWITCH_RULE;
97  
98          if (skipIfLastAndSharedWithCase && !isSwitchRule) {
99              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 }