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.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   * <ul>
32   * <li>
33   * Property {@code option} - Specify policy on how to pad parentheses.
34   * Type is {@code com.puppycrawl.tools.checkstyle.checks.whitespace.PadOption}.
35   * Default value is {@code nospace}.
36   * </li>
37   * </ul>
38   *
39   * <p>
40   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
41   * </p>
42   *
43   * <p>
44   * Violation Message Keys:
45   * </p>
46   * <ul>
47   * <li>
48   * {@code ws.followed}
49   * </li>
50   * <li>
51   * {@code ws.notFollowed}
52   * </li>
53   * <li>
54   * {@code ws.notPreceded}
55   * </li>
56   * <li>
57   * {@code ws.preceded}
58   * </li>
59   * </ul>
60   *
61   * @since 3.2
62   */
63  public class TypecastParenPadCheck extends AbstractParenPadCheck {
64  
65      @Override
66      public int[] getRequiredTokens() {
67          return new int[] {TokenTypes.RPAREN, TokenTypes.TYPECAST};
68      }
69  
70      @Override
71      public int[] getDefaultTokens() {
72          return getRequiredTokens();
73      }
74  
75      @Override
76      public int[] getAcceptableTokens() {
77          return getRequiredTokens();
78      }
79  
80      @Override
81      public void visitToken(DetailAST ast) {
82          // Strange logic in this method to guard against checking RPAREN tokens
83          // that are not associated with a TYPECAST token.
84          if (ast.getType() == TokenTypes.TYPECAST) {
85              processLeft(ast);
86          }
87          else if (ast.getParent().getType() == TokenTypes.TYPECAST
88                   && ast.getParent().findFirstToken(TokenTypes.RPAREN) == ast) {
89              processRight(ast);
90          }
91      }
92  
93  }