001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2017 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 com.puppycrawl.tools.checkstyle.api.AbstractCheck;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025
026/**
027 * Finds nested blocks.
028 *
029 * <p>
030 * For example this Check flags confusing code like
031 * </p>
032 * <pre>
033 * public void guessTheOutput()
034 * {
035 *     int whichIsWhich = 0;
036 *     {
037 *         int whichIsWhich = 2;
038 *     }
039 *     System.out.println("value = " + whichIsWhich);
040 * }
041 * </pre>
042 * and debugging / refactoring leftovers such as
043 *
044 * <pre>
045 * // if (someOldCondition)
046 * {
047 *     System.out.println("unconditional");
048 * }
049 * </pre>
050 *
051 * <p>
052 * A case in a switch statement does not implicitly form a block.
053 * Thus to be able to introduce local variables that have case scope
054 * it is necessary to open a nested block. This is supported, set
055 * the allowInSwitchCase property to true and include all statements
056 * of the case in the block.
057 * </p>
058 *
059 * <pre>
060 * switch (a)
061 * {
062 *     case 0:
063 *         // Never OK, break outside block
064 *         {
065 *             x = 1;
066 *         }
067 *         break;
068 *     case 1:
069 *         // Never OK, statement outside block
070 *         System.out.println("Hello");
071 *         {
072 *             x = 2;
073 *             break;
074 *         }
075 *     case 1:
076 *         // OK if allowInSwitchCase is true
077 *         {
078 *             System.out.println("Hello");
079 *             x = 2;
080 *             break;
081 *         }
082 * }
083 * </pre>
084 *
085 * @author lkuehne
086 */
087public class AvoidNestedBlocksCheck extends AbstractCheck {
088    /**
089     * A key is pointing to the warning message text in "messages.properties"
090     * file.
091     */
092    public static final String MSG_KEY_BLOCK_NESTED = "block.nested";
093
094    /**
095     * Whether nested blocks are allowed if they are the
096     * only child of a switch case.
097     */
098    private boolean allowInSwitchCase;
099
100    @Override
101    public int[] getDefaultTokens() {
102        return getAcceptableTokens();
103    }
104
105    @Override
106    public int[] getAcceptableTokens() {
107        return new int[] {TokenTypes.SLIST};
108    }
109
110    @Override
111    public int[] getRequiredTokens() {
112        return getAcceptableTokens();
113    }
114
115    @Override
116    public void visitToken(DetailAST ast) {
117        final DetailAST parent = ast.getParent();
118        if (parent.getType() == TokenTypes.SLIST
119                && (!allowInSwitchCase
120                    || parent.getParent().getType() != TokenTypes.CASE_GROUP
121                    || parent.getNumberOfChildren() != 1)) {
122            log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY_BLOCK_NESTED);
123        }
124    }
125
126    /**
127     * Setter for allowInSwitchCase property.
128     * @param allowInSwitchCase whether nested blocks are allowed
129     *                 if they are the only child of a switch case.
130     */
131    public void setAllowInSwitchCase(boolean allowInSwitchCase) {
132        this.allowInSwitchCase = allowInSwitchCase;
133    }
134}