KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > whitespace > ParenPadCheck


1 ////////////////////////////////////////////////////////////////////////////////
2
// checkstyle: Checks Java source code for adherence to a set of rules.
3
// Copyright (C) 2001-2005 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

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  * <p>Checks the padding of parentheses; that is whether a space is required
27  * after a left parenthesis and before a right parenthesis, or such spaces are
28  * forbidden, with the exception that it does
29  * not check for padding of the right parenthesis at an empty for iterator.
30  * Use Check {@link EmptyForIteratorPadCheck EmptyForIteratorPad} to validate
31  * empty for iterators.
32  * <p>
33  * </p>
34  * The policy to verify is specified using the {@link PadOption} class and
35  * defaults to {@link PadOption#NOSPACE}.
36  * </p>
37  * <p> By default the check will check parentheses that occur with the following
38  * tokens:
39  * {@link TokenTypes#CTOR_CALL CTOR_CALL},
40  * {@link TokenTypes#LPAREN LPAREN},
41  * {@link TokenTypes#METHOD_CALL METHOD_CALL},
42  * {@link TokenTypes#RPAREN RPAREN},
43  * {@link TokenTypes#SUPER_CTOR_CALL SUPER_CTOR_CALL},
44  * </p>
45  * <p>
46  * An example of how to configure the check is:
47  * </p>
48  * <pre>
49  * &lt;module name="ParenPad"/&gt;
50  * </pre>
51  * <p>
52  * An example of how to configure the check to require spaces for the
53  * parentheses of constructor, method, and super constructor invocations is:
54  * </p>
55  * <pre>
56  * &lt;module name="ParenPad"&gt;
57  * &lt;property name="tokens"
58  * value="CTOR_CALL, METHOD_CALL, SUPER_CTOR_CALL"/&gt;
59  * &lt;property name="option" value="space"/&gt;
60  * &lt;/module&gt;
61  * </pre>
62  * @author Oliver Burn
63  * @version 1.0
64  */

65 public class ParenPadCheck extends AbstractParenPadCheck
66 {
67     /** {@inheritDoc} */
68     public int[] getDefaultTokens()
69     {
70         return new int[] {TokenTypes.RPAREN,
71                           TokenTypes.LPAREN,
72                           TokenTypes.CTOR_CALL,
73                           TokenTypes.SUPER_CTOR_CALL,
74                           TokenTypes.METHOD_CALL,
75         };
76     }
77
78     /** {@inheritDoc} */
79     public void visitToken(DetailAST aAST)
80     {
81         // Strange logic in this method to guard against checking RPAREN tokens
82
// that are associated with a TYPECAST token.
83
if (aAST.getType() != TokenTypes.RPAREN) {
84             if ((aAST.getType() == TokenTypes.CTOR_CALL)
85                 || (aAST.getType() == TokenTypes.SUPER_CTOR_CALL))
86             {
87                 aAST = (DetailAST) aAST.getFirstChild();
88             }
89             if (!isPreceedsEmptyForInit(aAST)) {
90                 processLeft(aAST);
91             }
92         }
93         else if ((aAST.getParent() == null)
94                  || (aAST.getParent().getType() != TokenTypes.TYPECAST)
95                  || (aAST.getParent().findFirstToken(TokenTypes.RPAREN)
96                      != aAST))
97         {
98             if (!isFollowsEmptyForIterator(aAST)) {
99                 processRight(aAST);
100             }
101         }
102     }
103
104     /**
105      * @param aAST the token to check
106      * @return whether a token follows an empty for iterator
107      */

108     private boolean isFollowsEmptyForIterator(DetailAST aAST)
109     {
110         boolean followsEmptyForIterator = false;
111         final DetailAST parent = aAST.getParent();
112         //Only traditional for statements are examined, not for-each statements
113
if ((parent != null)
114             && (parent.getType() == TokenTypes.LITERAL_FOR)
115             && (parent.findFirstToken(TokenTypes.FOR_EACH_CLAUSE) == null))
116         {
117             final DetailAST forIterator =
118                 parent.findFirstToken(TokenTypes.FOR_ITERATOR);
119             followsEmptyForIterator = (forIterator.getChildCount() == 0)
120                 && (aAST == forIterator.getNextSibling());
121         }
122         return followsEmptyForIterator;
123     }
124
125     /**
126      * @param aAST the token to check
127      * @return whether a token preceeds an empty for initializer
128      */

129     private boolean isPreceedsEmptyForInit(DetailAST aAST)
130     {
131         boolean preceedsEmptyForInintializer = false;
132         final DetailAST parent = aAST.getParent();
133         //Only traditional for statements are examined, not for-each statements
134
if ((parent != null)
135             && (parent.getType() == TokenTypes.LITERAL_FOR)
136             && (parent.findFirstToken(TokenTypes.FOR_EACH_CLAUSE) == null))
137         {
138             final DetailAST forIterator =
139                     parent.findFirstToken(TokenTypes.FOR_INIT);
140             preceedsEmptyForInintializer = (forIterator.getChildCount() == 0)
141                     && (aAST == forIterator.getPreviousSibling());
142         }
143         return preceedsEmptyForInintializer;
144     }
145 }
146
Popular Tags