Source code: com/puppycrawl/tools/checkstyle/checks/whitespace/TypecastParenPadCheck.java
1 ////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code for adherence to a set of rules.
3 // Copyright (C) 2001-2003 Oliver Burn
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 package com.puppycrawl.tools.checkstyle.checks.whitespace;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23
24 /**
25 * <p>Checks the padding of parentheses for typecasts. That is whether a space
26 * is required after a left parenthesis and before a right parenthesis, or such
27 * spaces are forbidden.
28 * <p>
29 * </p>
30 * The policy to verify is specified using the {@link PadOption} class and
31 * defaults to {@link PadOption#NOSPACE}.
32 * </p>
33 * <p>
34 * An example of how to configure the check is:
35 * </p>
36 * <pre>
37 * <module name="TypecastParenPad"/>
38 * </pre>
39 * <p>
40 * An example of how to configure the check to require spaces for the
41 * parentheses of constructor, method, and super constructor invocations is:
42 * </p>
43 * <pre>
44 * <module name="TypecastParenPad">
45 * <property name="option" value="space"/>
46 * </module>
47 * </pre>
48 * @author Oliver Burn
49 * @version 1.0
50 */
51 public class TypecastParenPadCheck
52 extends AbstractParenPadCheck
53 {
54 /** @see com.puppycrawl.tools.checkstyle.api.Check */
55 public int[] getRequiredTokens()
56 {
57 return new int[] {TokenTypes.RPAREN, TokenTypes.TYPECAST};
58 }
59
60 /** @see com.puppycrawl.tools.checkstyle.api.Check */
61 public int[] getDefaultTokens()
62 {
63 return getRequiredTokens();
64 }
65
66 /** @see com.puppycrawl.tools.checkstyle.api.Check */
67 public void visitToken(DetailAST aAST)
68 {
69 // Strange logic in this method to guard against checking RPAREN tokens
70 // that are not associated with a TYPECAST token.
71 if (aAST.getType() == TokenTypes.TYPECAST) {
72 processLeft(aAST);
73 }
74 else if ((aAST.getParent() != null)
75 && (aAST.getParent().getType() == TokenTypes.TYPECAST))
76 {
77 processRight(aAST);
78 }
79 }
80 }