View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.whitespace;
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  import com.puppycrawl.tools.checkstyle.utils.TokenUtil;
27  
28  /**
29   * <div>
30   * Checks that there is no whitespace before the colon in a switch block.
31   * </div>
32   *
33   * @since 8.45
34   */
35  @StatelessCheck
36  public class NoWhitespaceBeforeCaseDefaultColonCheck
37      extends AbstractCheck {
38  
39      /**
40       * A key is pointing to the warning message text in "messages.properties"
41       * file.
42       */
43      public static final String MSG_KEY = "ws.preceded";
44  
45      @Override
46      public int[] getDefaultTokens() {
47          return getRequiredTokens();
48      }
49  
50      @Override
51      public int[] getAcceptableTokens() {
52          return getRequiredTokens();
53      }
54  
55      @Override
56      public int[] getRequiredTokens() {
57          return new int[] {TokenTypes.COLON};
58      }
59  
60      @Override
61      public boolean isCommentNodesRequired() {
62          return true;
63      }
64  
65      @Override
66      public void visitToken(DetailAST ast) {
67          if (isInSwitch(ast) && isWhiteSpaceBeforeColon(ast)) {
68              log(ast, MSG_KEY, ast.getText());
69          }
70      }
71  
72      /**
73       * Checks if the colon is inside a switch block.
74       *
75       * @param colonAst DetailAST to check.
76       * @return true, if colon case is inside a switch block.
77       */
78      private static boolean isInSwitch(DetailAST colonAst) {
79          return TokenUtil.isOfType(colonAst.getParent(), TokenTypes.LITERAL_CASE,
80                  TokenTypes.LITERAL_DEFAULT);
81      }
82  
83      /**
84       * Checks if there is a whitespace before the colon of a switch case or switch default.
85       *
86       * @param colonAst DetailAST to check.
87       * @return true, if there is whitespace preceding colonAst.
88       */
89      private static boolean isWhiteSpaceBeforeColon(DetailAST colonAst) {
90          final DetailAST parent = colonAst.getParent();
91          final boolean result;
92          if (isOnDifferentLineWithPreviousToken(colonAst)) {
93              result = true;
94          }
95          else if (parent.getType() == TokenTypes.LITERAL_CASE) {
96              result = isWhitespaceBeforeColonOfCase(colonAst);
97          }
98          else {
99              result = isWhitespaceBeforeColonOfDefault(colonAst);
100         }
101         return result;
102     }
103 
104     /**
105      * Checks if there is a whitespace before the colon of a switch case.
106      *
107      * @param colonAst DetailAST to check.
108      * @return true, if there is whitespace preceding colonAst.
109      */
110     private static boolean isWhitespaceBeforeColonOfCase(DetailAST colonAst) {
111         final DetailAST previousSibling = colonAst.getPreviousSibling();
112         int offset = 0;
113         if (previousSibling.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) {
114             offset = 1;
115         }
116         return colonAst.getColumnNo() != getLastColumnNumberOf(previousSibling) + offset;
117     }
118 
119     /**
120      * Checks if there is a whitespace before the colon of a switch default.
121      *
122      * @param colonAst DetailAST to check.
123      * @return true, if there is whitespace preceding colonAst.
124      */
125     private static boolean isWhitespaceBeforeColonOfDefault(DetailAST colonAst) {
126         final boolean result;
127         final DetailAST previousSibling = colonAst.getPreviousSibling();
128         if (previousSibling == null) {
129             final DetailAST literalDefault = colonAst.getParent();
130             result = colonAst.getColumnNo()
131                     != literalDefault.getColumnNo() + literalDefault.getText().length();
132         }
133         else {
134             result =
135                     colonAst.getColumnNo() != getLastColumnNumberOf(previousSibling) + 1;
136         }
137         return result;
138     }
139 
140     /**
141      * Checks if the colon is on same line as of case or default.
142      *
143      * @param colonAst DetailAST to check.
144      * @return true, if colon case is in different line as of case or default.
145      */
146     private static boolean isOnDifferentLineWithPreviousToken(DetailAST colonAst) {
147         final DetailAST previousSibling;
148         final DetailAST parent = colonAst.getParent();
149         if (parent.getType() == TokenTypes.LITERAL_CASE) {
150             previousSibling = colonAst.getPreviousSibling();
151         }
152         else {
153             previousSibling = colonAst.getParent();
154         }
155         return !TokenUtil.areOnSameLine(previousSibling, colonAst);
156     }
157 
158     /**
159      * Returns the last column number of an ast.
160      *
161      * @param ast DetailAST to check.
162      * @return ast's last column number.
163      */
164     private static int getLastColumnNumberOf(DetailAST ast) {
165         DetailAST lastChild = ast;
166         while (lastChild.hasChildren()) {
167             lastChild = lastChild.getLastChild();
168         }
169         return lastChild.getColumnNo() + lastChild.getText().length();
170     }
171 
172 }