001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 the original author or authors.
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////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.blocks;
021
022import java.util.Locale;
023
024import com.puppycrawl.tools.checkstyle.StatelessCheck;
025import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
026import com.puppycrawl.tools.checkstyle.api.DetailAST;
027import com.puppycrawl.tools.checkstyle.api.TokenTypes;
028import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
029
030/**
031 * <p>
032 * Checks for empty blocks. This check does not validate sequential blocks.
033 * </p>
034 * <p>
035 * Sequential blocks won't be checked. Also, no violations for fallthrough:
036 * </p>
037 * <pre>
038 * switch (a) {
039 *   case 1:                          // no violation
040 *   case 2:                          // no violation
041 *   case 3: someMethod(); { }        // no violation
042 *   default: break;
043 * }
044 * </pre>
045 * <p>
046 * This check processes LITERAL_CASE and LITERAL_DEFAULT separately.
047 * So, if tokens=LITERAL_DEFAULT, following code will not trigger any violation,
048 * as the empty block belongs to LITERAL_CASE:
049 * </p>
050 * <p>
051 * Configuration:
052 * </p>
053 * <pre>
054 * &lt;module name=&quot;EmptyBlock&quot;&gt;
055 *   &lt;property name=&quot;tokens&quot; value=&quot;LITERAL_DEFAULT&quot;/&gt;
056 * &lt;/module&gt;
057 * </pre>
058 * <p>
059 * Result:
060 * </p>
061 * <pre>
062 * switch (a) {
063 *   default:        // no violation for "default:" as empty block belong to "case 1:"
064 *   case 1: { }
065 * }
066 * </pre>
067 * <ul>
068 * <li>
069 * Property {@code option} - specify the policy on block contents.
070 * Default value is {@code statement}.
071 * </li>
072 * <li>
073 * Property {@code tokens} - tokens to check
074 * Default value is:
075 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_WHILE">
076 * LITERAL_WHILE</a>,
077 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_TRY">
078 * LITERAL_TRY</a>,
079 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_FINALLY">
080 * LITERAL_FINALLY</a>,
081 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_DO">
082 * LITERAL_DO</a>,
083 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_IF">
084 * LITERAL_IF</a>,
085 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_ELSE">
086 * LITERAL_ELSE</a>,
087 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_FOR">
088 * LITERAL_FOR</a>,
089 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#INSTANCE_INIT">
090 * INSTANCE_INIT</a>,
091 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#STATIC_INIT">
092 * STATIC_INIT</a>,
093 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SWITCH">
094 * LITERAL_SWITCH</a>,
095 * <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_SYNCHRONIZED">
096 * LITERAL_SYNCHRONIZED</a>.
097 * </li>
098 * </ul>
099 * <p>
100 * To configure the check:
101 * </p>
102 * <pre>
103 * &lt;module name="EmptyBlock"/&gt;
104 * </pre>
105 * <p>
106 * To configure the check for the {@code text} policy and only {@code try} blocks:
107 * </p>
108 * <pre>
109 * &lt;module name=&quot;EmptyBlock&quot;&gt;
110 *   &lt;property name=&quot;option&quot; value=&quot;text&quot;/&gt;
111 *   &lt;property name=&quot;tokens&quot; value=&quot;LITERAL_TRY&quot;/&gt;
112 * &lt;/module&gt;
113 * </pre>
114 *
115 * @since 3.0
116 */
117@StatelessCheck
118public class EmptyBlockCheck
119    extends AbstractCheck {
120
121    /**
122     * A key is pointing to the warning message text in "messages.properties"
123     * file.
124     */
125    public static final String MSG_KEY_BLOCK_NO_STATEMENT = "block.noStatement";
126
127    /**
128     * A key is pointing to the warning message text in "messages.properties"
129     * file.
130     */
131    public static final String MSG_KEY_BLOCK_EMPTY = "block.empty";
132
133    /** Specify the policy on block contents. */
134    private BlockOption option = BlockOption.STATEMENT;
135
136    /**
137     * Setter to specify the policy on block contents.
138     *
139     * @param optionStr string to decode option from
140     * @throws IllegalArgumentException if unable to decode
141     */
142    public void setOption(String optionStr) {
143        option = BlockOption.valueOf(optionStr.trim().toUpperCase(Locale.ENGLISH));
144    }
145
146    @Override
147    public int[] getDefaultTokens() {
148        return new int[] {
149            TokenTypes.LITERAL_WHILE,
150            TokenTypes.LITERAL_TRY,
151            TokenTypes.LITERAL_FINALLY,
152            TokenTypes.LITERAL_DO,
153            TokenTypes.LITERAL_IF,
154            TokenTypes.LITERAL_ELSE,
155            TokenTypes.LITERAL_FOR,
156            TokenTypes.INSTANCE_INIT,
157            TokenTypes.STATIC_INIT,
158            TokenTypes.LITERAL_SWITCH,
159            TokenTypes.LITERAL_SYNCHRONIZED,
160        };
161    }
162
163    @Override
164    public int[] getAcceptableTokens() {
165        return new int[] {
166            TokenTypes.LITERAL_WHILE,
167            TokenTypes.LITERAL_TRY,
168            TokenTypes.LITERAL_CATCH,
169            TokenTypes.LITERAL_FINALLY,
170            TokenTypes.LITERAL_DO,
171            TokenTypes.LITERAL_IF,
172            TokenTypes.LITERAL_ELSE,
173            TokenTypes.LITERAL_FOR,
174            TokenTypes.INSTANCE_INIT,
175            TokenTypes.STATIC_INIT,
176            TokenTypes.LITERAL_SWITCH,
177            TokenTypes.LITERAL_SYNCHRONIZED,
178            TokenTypes.LITERAL_CASE,
179            TokenTypes.LITERAL_DEFAULT,
180            TokenTypes.ARRAY_INIT,
181        };
182    }
183
184    @Override
185    public int[] getRequiredTokens() {
186        return CommonUtil.EMPTY_INT_ARRAY;
187    }
188
189    @Override
190    public void visitToken(DetailAST ast) {
191        final DetailAST leftCurly = findLeftCurly(ast);
192        if (leftCurly != null) {
193            if (option == BlockOption.STATEMENT) {
194                final boolean emptyBlock;
195                if (leftCurly.getType() == TokenTypes.LCURLY) {
196                    emptyBlock = leftCurly.getNextSibling().getType() != TokenTypes.CASE_GROUP;
197                }
198                else {
199                    emptyBlock = leftCurly.getChildCount() <= 1;
200                }
201                if (emptyBlock) {
202                    log(leftCurly,
203                        MSG_KEY_BLOCK_NO_STATEMENT,
204                        ast.getText());
205                }
206            }
207            else if (!hasText(leftCurly)) {
208                log(leftCurly,
209                    MSG_KEY_BLOCK_EMPTY,
210                    ast.getText());
211            }
212        }
213    }
214
215    /**
216     * Checks if SLIST token contains any text.
217     *
218     * @param slistAST a {@code DetailAST} value
219     * @return whether the SLIST token contains any text.
220     */
221    private boolean hasText(final DetailAST slistAST) {
222        final DetailAST rightCurly = slistAST.findFirstToken(TokenTypes.RCURLY);
223        final DetailAST rcurlyAST;
224
225        if (rightCurly == null) {
226            rcurlyAST = slistAST.getParent().findFirstToken(TokenTypes.RCURLY);
227        }
228        else {
229            rcurlyAST = rightCurly;
230        }
231        final int slistLineNo = slistAST.getLineNo();
232        final int slistColNo = slistAST.getColumnNo();
233        final int rcurlyLineNo = rcurlyAST.getLineNo();
234        final int rcurlyColNo = rcurlyAST.getColumnNo();
235        final String[] lines = getLines();
236        boolean returnValue = false;
237        if (slistLineNo == rcurlyLineNo) {
238            // Handle braces on the same line
239            final String txt = lines[slistLineNo - 1]
240                    .substring(slistColNo + 1, rcurlyColNo);
241            if (!CommonUtil.isBlank(txt)) {
242                returnValue = true;
243            }
244        }
245        else {
246            final String firstLine = lines[slistLineNo - 1].substring(slistColNo + 1);
247            final String lastLine = lines[rcurlyLineNo - 1].substring(0, rcurlyColNo);
248            // check if all lines are also only whitespace
249            returnValue = !(CommonUtil.isBlank(firstLine) && CommonUtil.isBlank(lastLine))
250                    || !checkIsAllLinesAreWhitespace(lines, slistLineNo, rcurlyLineNo);
251        }
252        return returnValue;
253    }
254
255    /**
256     * Checks is all lines in array contain whitespaces only.
257     *
258     * @param lines
259     *            array of lines
260     * @param lineFrom
261     *            check from this line number
262     * @param lineTo
263     *            check to this line numbers
264     * @return true if lines contain only whitespaces
265     */
266    private static boolean checkIsAllLinesAreWhitespace(String[] lines, int lineFrom, int lineTo) {
267        boolean result = true;
268        for (int i = lineFrom; i < lineTo - 1; i++) {
269            if (!CommonUtil.isBlank(lines[i])) {
270                result = false;
271                break;
272            }
273        }
274        return result;
275    }
276
277    /**
278     * Calculates the left curly corresponding to the block to be checked.
279     *
280     * @param ast a {@code DetailAST} value
281     * @return the left curly corresponding to the block to be checked
282     */
283    private static DetailAST findLeftCurly(DetailAST ast) {
284        final DetailAST leftCurly;
285        final DetailAST slistAST = ast.findFirstToken(TokenTypes.SLIST);
286        if ((ast.getType() == TokenTypes.LITERAL_CASE
287                || ast.getType() == TokenTypes.LITERAL_DEFAULT)
288                && ast.getNextSibling() != null
289                && ast.getNextSibling().getFirstChild() != null
290                && ast.getNextSibling().getFirstChild().getType() == TokenTypes.SLIST) {
291            leftCurly = ast.getNextSibling().getFirstChild();
292        }
293        else if (slistAST == null) {
294            leftCurly = ast.findFirstToken(TokenTypes.LCURLY);
295        }
296        else {
297            leftCurly = slistAST;
298        }
299        return leftCurly;
300    }
301
302}