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