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