KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > blocks > EmptyBlockCheck


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
package com.puppycrawl.tools.checkstyle.checks.blocks;
20
21 import com.puppycrawl.tools.checkstyle.api.DetailAST;
22 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
23 import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
24
25 /**
26  * Checks for empty blocks. The policy to verify is specified using the {@link
27  * BlockOption} class and defaults to {@link BlockOption#STMT}.
28  *
29  * <p> By default the check will check the following blocks:
30  * {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE},
31  * {@link TokenTypes#LITERAL_TRY LITERAL_TRY},
32  * {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH},
33  * {@link TokenTypes#LITERAL_FINALLY LITERAL_FINALLY},
34  * {@link TokenTypes#LITERAL_DO LITERAL_DO},
35  * {@link TokenTypes#LITERAL_IF LITERAL_IF},
36  * {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE},
37  * {@link TokenTypes#LITERAL_FOR LITERAL_FOR},
38  * {@link TokenTypes#STATIC_INIT STATIC_INIT}.
39  * </p>
40  *
41  * <p> An example of how to configure the check is:
42  * </p>
43  * <pre>
44  * &lt;module name="EmptyBlock"/&gt;
45  * </pre>
46  *
47  * <p> An example of how to configure the check for the {@link
48  * BlockOption#TEXT} policy and only catch blocks is:
49  * </p>
50  *
51  * <pre>
52  * &lt;module name="EmptyBlock"&gt;
53  * &lt;property name="tokens" value="LITERAL_CATCH"/&gt;
54  * &lt;property name="option" value="text"/&gt;
55  * &lt;/module&gt;
56  * </pre>
57  *
58  * @author Lars Kühne
59  */

60 public class EmptyBlockCheck
61     extends AbstractOptionCheck
62 {
63     /**
64      * Creates a new <code>EmptyBlockCheck</code> instance.
65      */

66     public EmptyBlockCheck()
67     {
68         super(BlockOption.STMT);
69     }
70
71     /** {@inheritDoc} */
72     public int[] getDefaultTokens()
73     {
74         return new int[] {
75             TokenTypes.LITERAL_WHILE,
76             TokenTypes.LITERAL_TRY,
77             TokenTypes.LITERAL_CATCH,
78             TokenTypes.LITERAL_FINALLY,
79             TokenTypes.LITERAL_DO,
80             TokenTypes.LITERAL_IF,
81             TokenTypes.LITERAL_ELSE,
82             TokenTypes.LITERAL_FOR,
83             TokenTypes.INSTANCE_INIT,
84             TokenTypes.STATIC_INIT,
85             // TODO: need to handle....
86
//TokenTypes.LITERAL_SWITCH,
87
//TODO: does this handle TokenTypes.LITERAL_SYNCHRONIZED?
88
};
89     }
90
91     /** {@inheritDoc} */
92     public void visitToken(DetailAST aAST)
93     {
94         final DetailAST slistAST = aAST.findFirstToken(TokenTypes.SLIST);
95         if (slistAST != null) {
96             if (getAbstractOption() == BlockOption.STMT) {
97                 if (slistAST.getChildCount() <= 1) {
98                     log(slistAST.getLineNo(),
99                         slistAST.getColumnNo(),
100                         "block.noStmt",
101                         aAST.getText());
102                 }
103             }
104             else if (getAbstractOption() == BlockOption.TEXT) {
105                 if (!hasText(slistAST)) {
106                     log(slistAST.getLineNo(),
107                         slistAST.getColumnNo(),
108                         "block.empty",
109                         aAST.getText());
110                 }
111             }
112         }
113     }
114
115     /**
116      * @param aSlistAST a <code>DetailAST</code> value
117      * @return whether the SLIST token contains any text.
118      */

119     private boolean hasText(final DetailAST aSlistAST)
120     {
121         boolean retVal = false;
122
123         final DetailAST rcurlyAST = aSlistAST.findFirstToken(TokenTypes.RCURLY);
124         if (rcurlyAST != null) {
125             final int slistLineNo = aSlistAST.getLineNo();
126             final int slistColNo = aSlistAST.getColumnNo();
127             final int rcurlyLineNo = rcurlyAST.getLineNo();
128             final int rcurlyColNo = rcurlyAST.getColumnNo();
129             final String JavaDoc[] lines = getLines();
130             if (slistLineNo == rcurlyLineNo) {
131                 // Handle braces on the same line
132
final String JavaDoc txt = lines[slistLineNo - 1]
133                     .substring(slistColNo + 1, rcurlyColNo);
134                 if (txt.trim().length() != 0) {
135                     retVal = true;
136                 }
137             }
138             else {
139                 // check only whitespace of first & last lines
140
if ((lines[slistLineNo - 1]
141                      .substring(slistColNo + 1).trim().length() != 0)
142                     || (lines[rcurlyLineNo - 1]
143                         .substring(0, rcurlyColNo).trim().length() != 0))
144                 {
145                     retVal = true;
146                 }
147                 else {
148                     // check if all lines are also only whitespace
149
for (int i = slistLineNo; i < (rcurlyLineNo - 1); i++) {
150                         if (lines[i].trim().length() > 0) {
151                             retVal = true;
152                             break;
153                         }
154                     }
155                 }
156             }
157         }
158         return retVal;
159     }
160 }
161
Popular Tags