001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.blocks;
020
021import com.puppycrawl.tools.checkstyle.api.DetailAST;
022import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023import com.puppycrawl.tools.checkstyle.checks.AbstractOptionCheck;
024
025/**
026 * Checks for empty blocks. The policy to verify is specified using the {@link
027 * BlockOption} class and defaults to {@link BlockOption#STMT}.
028 *
029 * <p> By default the check will check the following blocks:
030 *  {@link TokenTypes#LITERAL_WHILE LITERAL_WHILE},
031 *  {@link TokenTypes#LITERAL_TRY LITERAL_TRY},
032 *  {@link TokenTypes#LITERAL_CATCH LITERAL_CATCH},
033 *  {@link TokenTypes#LITERAL_FINALLY LITERAL_FINALLY},
034 *  {@link TokenTypes#LITERAL_DO LITERAL_DO},
035 *  {@link TokenTypes#LITERAL_IF LITERAL_IF},
036 *  {@link TokenTypes#LITERAL_ELSE LITERAL_ELSE},
037 *  {@link TokenTypes#LITERAL_FOR LITERAL_FOR},
038 *  {@link TokenTypes#STATIC_INIT STATIC_INIT},
039 *  {@link TokenTypes#LITERAL_SWITCH LITERAL_SWITCH}.
040 * </p>
041 *
042 * <p> An example of how to configure the check is:
043 * </p>
044 * <pre>
045 * &lt;module name="EmptyBlock"/&gt;
046 * </pre>
047 *
048 * <p> An example of how to configure the check for the {@link
049 * BlockOption#TEXT} policy and only catch blocks is:
050 * </p>
051 *
052 * <pre>
053 * &lt;module name="EmptyBlock"&gt;
054 *    &lt;property name="tokens" value="LITERAL_CATCH"/&gt;
055 *    &lt;property name="option" value="text"/&gt;
056 * &lt;/module&gt;
057 * </pre>
058 *
059 * @author Lars Kühne
060 */
061public class EmptyBlockCheck
062    extends AbstractOptionCheck<BlockOption>
063{
064    /**
065     * A key is pointing to the warning message text in "messages.properties"
066     * file.
067     */
068    public static final String MSG_KEY_BLOCK_NO_STMT = "block.noStmt";
069
070    /**
071     * A key is pointing to the warning message text in "messages.properties"
072     * file.
073     */
074    public static final String MSG_KEY_BLOCK_EMPTY = "block.empty";
075
076    /**
077     * Creates a new <code>EmptyBlockCheck</code> instance.
078     */
079    public EmptyBlockCheck()
080    {
081        super(BlockOption.STMT, BlockOption.class);
082    }
083
084    @Override
085    public int[] getDefaultTokens()
086    {
087        return new int[] {
088            TokenTypes.LITERAL_WHILE,
089            TokenTypes.LITERAL_TRY,
090            TokenTypes.LITERAL_CATCH,
091            TokenTypes.LITERAL_FINALLY,
092            TokenTypes.LITERAL_DO,
093            TokenTypes.LITERAL_IF,
094            TokenTypes.LITERAL_ELSE,
095            TokenTypes.LITERAL_FOR,
096            TokenTypes.INSTANCE_INIT,
097            TokenTypes.STATIC_INIT,
098            TokenTypes.LITERAL_SWITCH,
099            //TODO: does this handle TokenTypes.LITERAL_SYNCHRONIZED?
100        };
101    }
102
103    @Override
104    public void visitToken(DetailAST ast)
105    {
106        final DetailAST slistToken = ast.findFirstToken(TokenTypes.SLIST);
107        final DetailAST leftCurly = slistToken != null
108                ? slistToken : ast.findFirstToken(TokenTypes.LCURLY);
109        if (leftCurly != null) {
110            if (getAbstractOption() == BlockOption.STMT) {
111                boolean emptyBlock;
112                if (leftCurly.getType() == TokenTypes.LCURLY) {
113                    emptyBlock = leftCurly.getNextSibling().getType() != TokenTypes.CASE_GROUP;
114                }
115                else {
116                    emptyBlock = leftCurly.getChildCount() <= 1;
117                }
118                if (emptyBlock) {
119                    log(leftCurly.getLineNo(),
120                        leftCurly.getColumnNo(),
121                        MSG_KEY_BLOCK_NO_STMT,
122                        ast.getText());
123                }
124            }
125            else if (getAbstractOption() == BlockOption.TEXT
126                    && !hasText(leftCurly))
127            {
128                log(leftCurly.getLineNo(),
129                    leftCurly.getColumnNo(),
130                    MSG_KEY_BLOCK_EMPTY,
131                    ast.getText());
132            }
133        }
134    }
135
136    /**
137     * @param slistAST a <code>DetailAST</code> value
138     * @return whether the SLIST token contains any text.
139     */
140    protected boolean hasText(final DetailAST slistAST)
141    {
142        boolean retVal = false;
143
144        final DetailAST rightCurly = slistAST.findFirstToken(TokenTypes.RCURLY);
145        final DetailAST rcurlyAST = rightCurly != null
146                ? rightCurly : slistAST.getParent().findFirstToken(TokenTypes.RCURLY);
147        if (rcurlyAST != null) {
148            final int slistLineNo = slistAST.getLineNo();
149            final int slistColNo = slistAST.getColumnNo();
150            final int rcurlyLineNo = rcurlyAST.getLineNo();
151            final int rcurlyColNo = rcurlyAST.getColumnNo();
152            final String[] lines = getLines();
153            if (slistLineNo == rcurlyLineNo) {
154                // Handle braces on the same line
155                final String txt = lines[slistLineNo - 1]
156                    .substring(slistColNo + 1, rcurlyColNo);
157                if (txt.trim().length() != 0) {
158                    retVal = true;
159                }
160            }
161            else {
162                // check only whitespace of first & last lines
163                if ((lines[slistLineNo - 1]
164                     .substring(slistColNo + 1).trim().length() != 0)
165                    || (lines[rcurlyLineNo - 1]
166                        .substring(0, rcurlyColNo).trim().length() != 0))
167                {
168                    retVal = true;
169                }
170                else {
171                    // check if all lines are also only whitespace
172                    for (int i = slistLineNo; i < (rcurlyLineNo - 1); i++) {
173                        if (lines[i].trim().length() > 0) {
174                            retVal = true;
175                            break;
176                        }
177                    }
178                }
179            }
180        }
181        return retVal;
182    }
183}