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.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      /**
46       * Creates a new {@code NoWhitespaceBeforeCaseDefaultColonCheck} instance.
47       */
48      public NoWhitespaceBeforeCaseDefaultColonCheck() {
49          // no code by default
50      }
51  
52      @Override
53      public int[] getDefaultTokens() {
54          return getRequiredTokens();
55      }
56  
57      @Override
58      public int[] getAcceptableTokens() {
59          return getRequiredTokens();
60      }
61  
62      @Override
63      public int[] getRequiredTokens() {
64          return new int[] {TokenTypes.COLON};
65      }
66  
67      @Override
68      public boolean isCommentNodesRequired() {
69          return true;
70      }
71  
72      @Override
73      public void visitToken(DetailAST ast) {
74          if (isInSwitch(ast) && isWhiteSpaceBeforeColon(ast)) {
75              log(ast, MSG_KEY, ast.getText());
76          }
77      }
78  
79      /**
80       * Checks if the colon is inside a switch block.
81       *
82       * @param colonAst DetailAST to check.
83       * @return true, if colon case is inside a switch block.
84       */
85      private static boolean isInSwitch(DetailAST colonAst) {
86          return TokenUtil.isOfType(colonAst.getParent(), TokenTypes.LITERAL_CASE,
87                  TokenTypes.LITERAL_DEFAULT);
88      }
89  
90      /**
91       * Checks if there is a whitespace before the colon of a switch case or switch default.
92       *
93       * @param colonAst DetailAST to check.
94       * @return true, if there is whitespace preceding colonAst.
95       */
96      private static boolean isWhiteSpaceBeforeColon(DetailAST colonAst) {
97          final DetailAST parent = colonAst.getParent();
98          final boolean result;
99          if (isOnDifferentLineWithPreviousToken(colonAst)) {
100             result = true;
101         }
102         else if (parent.getType() == TokenTypes.LITERAL_CASE) {
103             result = isWhitespaceBeforeColonOfCase(colonAst);
104         }
105         else {
106             result = isWhitespaceBeforeColonOfDefault(colonAst);
107         }
108         return result;
109     }
110 
111     /**
112      * Checks if there is a whitespace before the colon of a switch case.
113      *
114      * @param colonAst DetailAST to check.
115      * @return true, if there is whitespace preceding colonAst.
116      */
117     private static boolean isWhitespaceBeforeColonOfCase(DetailAST colonAst) {
118         final DetailAST previousSibling = colonAst.getPreviousSibling();
119         int offset = 0;
120         if (previousSibling.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) {
121             offset = 1;
122         }
123         return colonAst.getColumnNo() != getLastColumnNumberOf(previousSibling) + offset;
124     }
125 
126     /**
127      * Checks if there is a whitespace before the colon of a switch default.
128      *
129      * @param colonAst DetailAST to check.
130      * @return true, if there is whitespace preceding colonAst.
131      */
132     private static boolean isWhitespaceBeforeColonOfDefault(DetailAST colonAst) {
133         final boolean result;
134         final DetailAST previousSibling = colonAst.getPreviousSibling();
135         if (previousSibling == null) {
136             final DetailAST literalDefault = colonAst.getParent();
137             result = colonAst.getColumnNo()
138                     != literalDefault.getColumnNo() + literalDefault.getText().length();
139         }
140         else {
141             result =
142                     colonAst.getColumnNo() != getLastColumnNumberOf(previousSibling) + 1;
143         }
144         return result;
145     }
146 
147     /**
148      * Checks if the colon is on same line as of case or default.
149      *
150      * @param colonAst DetailAST to check.
151      * @return true, if colon case is in different line as of case or default.
152      */
153     private static boolean isOnDifferentLineWithPreviousToken(DetailAST colonAst) {
154         final DetailAST previousSibling;
155         final DetailAST parent = colonAst.getParent();
156         if (parent.getType() == TokenTypes.LITERAL_CASE) {
157             previousSibling = colonAst.getPreviousSibling();
158         }
159         else {
160             previousSibling = colonAst.getParent();
161         }
162         return !TokenUtil.areOnSameLine(previousSibling, colonAst);
163     }
164 
165     /**
166      * Returns the last column number of an ast.
167      *
168      * @param ast DetailAST to check.
169      * @return ast's last column number.
170      */
171     private static int getLastColumnNumberOf(DetailAST ast) {
172         DetailAST lastChild = ast;
173         while (lastChild.hasChildren()) {
174             lastChild = lastChild.getLastChild();
175         }
176         return lastChild.getColumnNo() + lastChild.getText().length();
177     }
178 
179 }