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.api.DetailAST;
23  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
24  
25  /**
26   * <div>
27   * Checks the policy on the padding of parentheses for typecasts. That is, whether a space
28   * is required after a left parenthesis and before a right parenthesis, or such
29   * spaces are forbidden.
30   * </div>
31   *
32   * @since 3.2
33   */
34  public class TypecastParenPadCheck extends AbstractParenPadCheck {
35  
36      /**
37       * A key is pointing to the warning message text in "messages.properties"
38       * file.
39       */
40      public static final String MSG_WS_FOLLOWED = "ws.followed";
41  
42      /**
43       * A key is pointing to the warning message text in "messages.properties"
44       * file.
45       */
46      public static final String MSG_WS_NOT_FOLLOWED = "ws.notFollowed";
47  
48      /**
49       * A key is pointing to the warning message text in "messages.properties"
50       * file.
51       */
52      public static final String MSG_WS_PRECEDED = "ws.preceded";
53  
54      /**
55       * A key is pointing to the warning message text in "messages.properties"
56       * file.
57       */
58      public static final String MSG_WS_NOT_PRECEDED = "ws.notPreceded";
59  
60      /**
61       * Creates a new {@code TypecastParenPadCheck} instance.
62       */
63      public TypecastParenPadCheck() {
64          super(MSG_WS_FOLLOWED, MSG_WS_NOT_FOLLOWED, MSG_WS_PRECEDED, MSG_WS_NOT_PRECEDED);
65      }
66  
67      @Override
68      public int[] getRequiredTokens() {
69          return new int[] {TokenTypes.RPAREN, TokenTypes.TYPECAST};
70      }
71  
72      @Override
73      public int[] getDefaultTokens() {
74          return getRequiredTokens();
75      }
76  
77      @Override
78      public int[] getAcceptableTokens() {
79          return getRequiredTokens();
80      }
81  
82      @Override
83      public void visitToken(DetailAST ast) {
84          // Strange logic in this method to guard against checking RPAREN tokens
85          // that are not associated with a TYPECAST token.
86          if (ast.getType() == TokenTypes.TYPECAST) {
87              processLeft(ast);
88          }
89          else if (ast.getParent().getType() == TokenTypes.TYPECAST
90                   && ast.getParent().findFirstToken(TokenTypes.RPAREN) == ast) {
91              processRight(ast);
92          }
93      }
94  
95  }