001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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.FileStatefulCheck; 023import com.puppycrawl.tools.checkstyle.api.AbstractCheck; 024import com.puppycrawl.tools.checkstyle.api.DetailAST; 025import com.puppycrawl.tools.checkstyle.api.TokenTypes; 026 027/** 028 * <p> 029 * Restricts nested try-catch-finally blocks to a specified depth. 030 * </p> 031 * <ul> 032 * <li> 033 * Property {@code max} - Specify maximum allowed nesting depth. 034 * Default value is {@code 1}. 035 * </li> 036 * </ul> 037 * <p> 038 * To configure the check: 039 * </p> 040 * <pre> 041 * <module name="NestedTryDepth"/> 042 * </pre> 043 * <p> 044 * To configure the check to allow nesting depth 3: 045 * </p> 046 * <pre> 047 * <module name="NestedTryDepth"> 048 * <property name="max" value="3"/> 049 * </module> 050 * </pre> 051 * 052 * @since 3.2 053 */ 054@FileStatefulCheck 055public final class NestedTryDepthCheck extends AbstractCheck { 056 057 /** 058 * A key is pointing to the warning message text in "messages.properties" 059 * file. 060 */ 061 public static final String MSG_KEY = "nested.try.depth"; 062 063 /** Specify maximum allowed nesting depth. */ 064 private int max = 1; 065 /** Current nesting depth. */ 066 private int depth; 067 068 /** 069 * Setter to specify maximum allowed nesting depth. 070 * @param max maximum allowed nesting depth. 071 */ 072 public void setMax(int max) { 073 this.max = max; 074 } 075 076 @Override 077 public int[] getDefaultTokens() { 078 return getRequiredTokens(); 079 } 080 081 @Override 082 public int[] getAcceptableTokens() { 083 return getRequiredTokens(); 084 } 085 086 @Override 087 public int[] getRequiredTokens() { 088 return new int[] {TokenTypes.LITERAL_TRY}; 089 } 090 091 @Override 092 public void beginTree(DetailAST rootAST) { 093 depth = 0; 094 } 095 096 @Override 097 public void visitToken(DetailAST literalTry) { 098 if (depth > max) { 099 log(literalTry, MSG_KEY, depth, max); 100 } 101 ++depth; 102 } 103 104 @Override 105 public void leaveToken(DetailAST literalTry) { 106 --depth; 107 } 108 109}