| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| NestedForDepthCheck |
|
| 1.5;1.5 |
| 1 | //////////////////////////////////////////////////////////////////////////////// | |
| 2 | // checkstyle: Checks Java source code for adherence to a set of rules. | |
| 3 | // Copyright (C) 2001-2014 Oliver Burn | |
| 4 | // | |
| 5 | // This library is free software; you can redistribute it and/or | |
| 6 | // modify it under the terms of the GNU Lesser General Public | |
| 7 | // License as published by the Free Software Foundation; either | |
| 8 | // version 2.1 of the License, or (at your option) any later version. | |
| 9 | // | |
| 10 | // This library is distributed in the hope that it will be useful, | |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 13 | // Lesser General Public License for more details. | |
| 14 | // | |
| 15 | // You should have received a copy of the GNU Lesser General Public | |
| 16 | // License along with this library; if not, write to the Free Software | |
| 17 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 18 | //////////////////////////////////////////////////////////////////////////////// | |
| 19 | package com.puppycrawl.tools.checkstyle.checks.coding; | |
| 20 | ||
| 21 | import com.puppycrawl.tools.checkstyle.api.DetailAST; | |
| 22 | import com.puppycrawl.tools.checkstyle.api.TokenTypes; | |
| 23 | ||
| 24 | /** | |
| 25 | * Check the number of nested <code>for</code> -statements. The maximum | |
| 26 | * number of nested layers can be configured. The default value is 1. Most of | |
| 27 | * the logic is implemented in the parent class. The code for the class is | |
| 28 | * copied from the NestedIfDepthCheck-class. The only difference is the | |
| 29 | * intercepted token (for instead of if). Example: | |
| 30 | * <pre> | |
| 31 | * <!-- Restricts nested for blocks to a specified depth (default = 1). | |
| 32 | * --> | |
| 33 | * <module name="com.puppycrawl.tools.checkstyle.checks.coding | |
| 34 | * .CatchWithLostStackCheck"> | |
| 35 | * <property name="severity" value="info"/> | |
| 36 | * <property name="max" value="1"/> | |
| 37 | * </module> | |
| 38 | * </pre> | |
| 39 | * @author Alexander Jesse | |
| 40 | * @see com.puppycrawl.tools.checkstyle.checks.coding.AbstractNestedDepthCheck | |
| 41 | * @see com.puppycrawl.tools.checkstyle.checks.coding.NestedIfDepthCheck | |
| 42 | */ | |
| 43 | public final class NestedForDepthCheck extends AbstractNestedDepthCheck | |
| 44 | { | |
| 45 | /** default allowed nesting depth. */ | |
| 46 | private static final int DEFAULT_MAX = 1; | |
| 47 | ||
| 48 | /** Creates new check instance with default allowed nesting depth. */ | |
| 49 | public NestedForDepthCheck() | |
| 50 | { | |
| 51 | 2 | super(DEFAULT_MAX); |
| 52 | 2 | } |
| 53 | ||
| 54 | @Override | |
| 55 | public int[] getDefaultTokens() | |
| 56 | { | |
| 57 | 2 | return new int[] {TokenTypes.LITERAL_FOR}; |
| 58 | } | |
| 59 | ||
| 60 | @Override | |
| 61 | public void visitToken(DetailAST aAST) | |
| 62 | { | |
| 63 | 10 | if (TokenTypes.LITERAL_FOR == aAST.getType()) { |
| 64 | 10 | nestIn(aAST, "nested.for.depth"); |
| 65 | } | |
| 66 | 10 | } |
| 67 | ||
| 68 | @Override | |
| 69 | public void leaveToken(DetailAST aAST) | |
| 70 | { | |
| 71 | 10 | if (TokenTypes.LITERAL_FOR == aAST.getType()) { |
| 72 | 10 | nestOut(); |
| 73 | } | |
| 74 | 10 | } |
| 75 | } |