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"
44       * file.
45       */
46      public static final String MSG_KEY = "default.comes.last";
47  
48      /**
49       * A key is pointing to the warning message text in "messages.properties"
50       * file.
51       */
52      public static final String MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE =
53              "default.comes.last.in.casegroup";
54  
55      /** Control whether to allow {@code default} along with {@code case} if they are not last. */
56      private boolean skipIfLastAndSharedWithCase;
57  
58      /**
59       * Creates a new {@code DefaultComesLastCheck} instance.
60       */
61      public DefaultComesLastCheck() {
62          // no code by default
63      }
64  
65      @Override
66      public int[] getAcceptableTokens() {
67          return getRequiredTokens();
68      }
69  
70      @Override
71      public int[] getDefaultTokens() {
72          return getRequiredTokens();
73      }
74  
75      @Override
76      public int[] getRequiredTokens() {
77          return new int[] {
78              TokenTypes.LITERAL_DEFAULT,
79          };
80      }
81  
82      /**
83       * Setter to control whether to allow {@code default} along with
84       * {@code case} if they are not last.
85       *
86       * @param newValue whether to ignore checking.
87       * @since 7.7
88       */
89      public void setSkipIfLastAndSharedWithCase(boolean newValue) {
90          skipIfLastAndSharedWithCase = newValue;
91      }
92  
93      @Override
94      public void visitToken(DetailAST ast) {
95          final DetailAST defaultGroupAST = ast.getParent();
96  
97          // Switch rules are not subject to fall through.
98          final boolean isSwitchRule = defaultGroupAST.getType() == TokenTypes.SWITCH_RULE;
99  
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 }