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.coding; 020 021import com.puppycrawl.tools.checkstyle.api.DetailAST; 022import com.puppycrawl.tools.checkstyle.api.TokenTypes; 023 024/** 025 * Check the number of nested <code>for</code> -statements. The maximum 026 * number of nested layers can be configured. The default value is 1. Most of 027 * the logic is implemented in the parent class. The code for the class is 028 * copied from the NestedIfDepthCheck-class. The only difference is the 029 * intercepted token (for instead of if). Example: 030 * <pre> 031 * <!-- Restricts nested for blocks to a specified depth (default = 1). 032 * --> 033 * <module name="com.puppycrawl.tools.checkstyle.checks.coding 034 * .CatchWithLostStackCheck"> 035 * <property name="severity" value="info"/> 036 * <property name="max" value="1"/> 037 * </module> 038 * </pre> 039 * @author Alexander Jesse 040 * @see com.puppycrawl.tools.checkstyle.checks.coding.AbstractNestedDepthCheck 041 * @see com.puppycrawl.tools.checkstyle.checks.coding.NestedIfDepthCheck 042 */ 043public final class NestedForDepthCheck extends AbstractNestedDepthCheck 044{ 045 /** default allowed nesting depth. */ 046 private static final int DEFAULT_MAX = 1; 047 048 /** Creates new check instance with default allowed nesting depth. */ 049 public NestedForDepthCheck() 050 { 051 super(DEFAULT_MAX); 052 } 053 054 @Override 055 public int[] getDefaultTokens() 056 { 057 return new int[] {TokenTypes.LITERAL_FOR}; 058 } 059 060 @Override 061 public void visitToken(DetailAST ast) 062 { 063 if (TokenTypes.LITERAL_FOR == ast.getType()) { 064 nestIn(ast, "nested.for.depth"); 065 } 066 } 067 068 @Override 069 public void leaveToken(DetailAST ast) 070 { 071 if (TokenTypes.LITERAL_FOR == ast.getType()) { 072 nestOut(); 073 } 074 } 075}