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.coding; 021 022import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 023import com.puppycrawl.tools.checkstyle.api.DetailAST; 024import com.puppycrawl.tools.checkstyle.api.TokenTypes; 025 026/** 027 * Check the number of nested {@code for} -statements. The maximum 028 * number of nested layers can be configured. The default value is 1. 029 * The code for the class is copied from the NestedIfDepthCheck-class. 030 * The only difference is the intercepted token (for instead of if). 031 * Example: 032 * <pre> 033 * <!-- Restricts nested for blocks to a specified depth (default = 1). 034 * --> 035 * <module name="com.puppycrawl.tools.checkstyle.checks.coding 036 * .CatchWithLostStackCheck"> 037 * <property name="severity" value="info"/> 038 * <property name="max" value="1"/> 039 * </module> 040 * </pre> 041 * @author Alexander Jesse 042 * @see NestedIfDepthCheck 043 */ 044public final class NestedForDepthCheck extends AbstractCheck { 045 046 /** 047 * A key is pointing to the warning message text in "messages.properties" 048 * file. 049 */ 050 public static final String MSG_KEY = "nested.for.depth"; 051 052 /** Maximum allowed nesting depth. */ 053 private int max = 1; 054 /** Current nesting depth. */ 055 private int depth; 056 057 /** 058 * Setter for maximum allowed nesting depth. 059 * @param max maximum allowed nesting depth. 060 */ 061 public void setMax(int max) { 062 this.max = max; 063 } 064 065 @Override 066 public int[] getDefaultTokens() { 067 return getAcceptableTokens(); 068 } 069 070 @Override 071 public int[] getAcceptableTokens() { 072 return new int[] {TokenTypes.LITERAL_FOR}; 073 } 074 075 @Override 076 public int[] getRequiredTokens() { 077 return getAcceptableTokens(); 078 } 079 080 @Override 081 public void beginTree(DetailAST rootAST) { 082 depth = 0; 083 } 084 085 @Override 086 public void visitToken(DetailAST ast) { 087 if (depth > max) { 088 log(ast, MSG_KEY, depth, max); 089 } 090 ++depth; 091 } 092 093 @Override 094 public void leaveToken(DetailAST ast) { 095 --depth; 096 } 097}