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.Check;
022import com.puppycrawl.tools.checkstyle.api.TokenTypes;
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024
025/**
026 * Finds nested blocks.
027 *
028 * <p>
029 * For example this Check flags confusing code like
030 * </p>
031 * <pre>
032 * public void guessTheOutput()
033 * {
034 *     int whichIsWhich = 0;
035 *     {
036 *         int whichIsWhich = 2;
037 *     }
038 *     System.out.println("value = " + whichIsWhich);
039 * }
040 * </pre>
041 *
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 Check
088{
089    /**
090     * Whether nested blocks are allowed if they are the
091     * only child of a switch case.
092     */
093    private boolean allowInSwitchCase;
094
095    /**
096     * A key is pointing to the warning message text in "messages.properties"
097     * file.
098     */
099    public static final String MSG_KEY_BLOCK_NESTED = "block.nested";
100
101    @Override
102    public int[] getDefaultTokens()
103    {
104        return new int[] {TokenTypes.SLIST};
105    }
106
107    @Override
108    public void visitToken(DetailAST ast)
109    {
110        final DetailAST parent = ast.getParent();
111        if (parent.getType() == TokenTypes.SLIST) {
112            if (allowInSwitchCase
113                    && (parent.getParent().getType() == TokenTypes.CASE_GROUP)
114                    && (parent.getNumberOfChildren() == 1))
115            {
116                return;
117            }
118            log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY_BLOCK_NESTED);
119        }
120    }
121
122    /**
123     * Setter for allowInSwitchCase property.
124     * @param allowInSwitchCase whether nested blocks are allowed
125     *                 if they are the only child of a switch case.
126     */
127    public void setAllowInSwitchCase(boolean allowInSwitchCase)
128    {
129        this.allowInSwitchCase = allowInSwitchCase;
130    }
131}