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