001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 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    /**
092     * A key is pointing to the warning message text in "messages.properties"
093     * file.
094     */
095    public static final String MSG_KEY_BLOCK_NESTED = "block.nested";
096
097    /**
098     * Whether nested blocks are allowed if they are the
099     * only child of a switch case.
100     */
101    private boolean allowInSwitchCase;
102
103    @Override
104    public int[] getDefaultTokens() {
105        return getRequiredTokens();
106    }
107
108    @Override
109    public int[] getAcceptableTokens() {
110        return getRequiredTokens();
111    }
112
113    @Override
114    public int[] getRequiredTokens() {
115        return new int[] {TokenTypes.SLIST};
116    }
117
118    @Override
119    public void visitToken(DetailAST ast) {
120        final DetailAST parent = ast.getParent();
121        if (parent.getType() == TokenTypes.SLIST
122                && (!allowInSwitchCase
123                    || parent.getParent().getType() != TokenTypes.CASE_GROUP
124                    || parent.getNumberOfChildren() != 1)) {
125            log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY_BLOCK_NESTED);
126        }
127    }
128
129    /**
130     * Setter for allowInSwitchCase property.
131     * @param allowInSwitchCase whether nested blocks are allowed
132     *                 if they are the only child of a switch case.
133     */
134    public void setAllowInSwitchCase(boolean allowInSwitchCase) {
135        this.allowInSwitchCase = allowInSwitchCase;
136    }
137
138}