View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * <ul>
37   * <li>
38   * Property {@code skipIfLastAndSharedWithCase} - Control whether to allow {@code default}
39   * along with {@code case} if they are not last.
40   * Type is {@code boolean}.
41   * Default value is {@code false}.
42   * </li>
43   * </ul>
44   *
45   * <p>
46   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
47   * </p>
48   *
49   * <p>
50   * Violation Message Keys:
51   * </p>
52   * <ul>
53   * <li>
54   * {@code default.comes.last}
55   * </li>
56   * <li>
57   * {@code default.comes.last.in.casegroup}
58   * </li>
59   * </ul>
60   *
61   * @since 3.4
62   */
63  @StatelessCheck
64  public class DefaultComesLastCheck extends AbstractCheck {
65  
66      /**
67       * A key is pointing to the warning message text in "messages.properties"
68       * file.
69       */
70      public static final String MSG_KEY = "default.comes.last";
71  
72      /**
73       * A key is pointing to the warning message text in "messages.properties"
74       * file.
75       */
76      public static final String MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE =
77              "default.comes.last.in.casegroup";
78  
79      /** Control whether to allow {@code default} along with {@code case} if they are not last. */
80      private boolean skipIfLastAndSharedWithCase;
81  
82      @Override
83      public int[] getAcceptableTokens() {
84          return getRequiredTokens();
85      }
86  
87      @Override
88      public int[] getDefaultTokens() {
89          return getRequiredTokens();
90      }
91  
92      @Override
93      public int[] getRequiredTokens() {
94          return new int[] {
95              TokenTypes.LITERAL_DEFAULT,
96          };
97      }
98  
99      /**
100      * Setter to control whether to allow {@code default} along with
101      * {@code case} if they are not last.
102      *
103      * @param newValue whether to ignore checking.
104      * @since 7.7
105      */
106     public void setSkipIfLastAndSharedWithCase(boolean newValue) {
107         skipIfLastAndSharedWithCase = newValue;
108     }
109 
110     @Override
111     public void visitToken(DetailAST ast) {
112         final DetailAST defaultGroupAST = ast.getParent();
113 
114         // Switch rules are not subject to fall through.
115         final boolean isSwitchRule = defaultGroupAST.getType() == TokenTypes.SWITCH_RULE;
116 
117         if (skipIfLastAndSharedWithCase && !isSwitchRule) {
118             if (isNextSiblingOf(ast, TokenTypes.LITERAL_CASE)) {
119                 log(ast, MSG_KEY_SKIP_IF_LAST_AND_SHARED_WITH_CASE);
120             }
121             else if (ast.getPreviousSibling() == null
122                 && isNextSiblingOf(defaultGroupAST,
123                                                    TokenTypes.CASE_GROUP)) {
124                 log(ast, MSG_KEY);
125             }
126         }
127         else if (isNextSiblingOf(defaultGroupAST,
128                                             TokenTypes.CASE_GROUP)
129                     || isNextSiblingOf(defaultGroupAST,
130                                             TokenTypes.SWITCH_RULE)) {
131             log(ast, MSG_KEY);
132         }
133     }
134 
135     /**
136      * Return true only if passed tokenType in argument is found or returns false.
137      *
138      * @param ast root node.
139      * @param tokenType tokentype to be processed.
140      * @return true if desired token is found or else false.
141      */
142     private static boolean isNextSiblingOf(DetailAST ast, int tokenType) {
143         return ast.getNextSibling() != null && ast.getNextSibling().getType() == tokenType;
144     }
145 
146 }