diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/NestedTryDepthCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/NestedTryDepthCheck.java index 8d8f55549..7a766d8b1 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/NestedTryDepthCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/NestedTryDepthCheck.java @@ -19,6 +19,7 @@ package com.puppycrawl.tools.checkstyle.checks.coding; +import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.TokenTypes; @@ -26,7 +27,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes; * Restricts nested try-catch-finally blocks to a specified depth (default = 1). * @author Simon Harris */ -public final class NestedTryDepthCheck extends AbstractNestedDepthCheck { +public final class NestedTryDepthCheck extends Check { /** * A key is pointing to the warning message text in "messages.properties" @@ -34,17 +35,22 @@ public final class NestedTryDepthCheck extends AbstractNestedDepthCheck { */ public static final String MSG_KEY = "nested.try.depth"; - /** Default allowed nesting depth. */ - private static final int DEFAULT_MAX = 1; + /** Maximum allowed nesting depth. */ + private int max = 1; + /** Current nesting depth. */ + private int depth; - /** Creates new check instance with default allowed nesting depth. */ - public NestedTryDepthCheck() { - super(DEFAULT_MAX); + /** + * Setter for maximum allowed nesting depth. + * @param max maximum allowed nesting depth. + */ + public void setMax(int max) { + this.max = max; } @Override public int[] getDefaultTokens() { - return new int[] {TokenTypes.LITERAL_TRY}; + return getAcceptableTokens(); } @Override @@ -52,14 +58,27 @@ public final class NestedTryDepthCheck extends AbstractNestedDepthCheck { return new int[] {TokenTypes.LITERAL_TRY}; } + @Override + public int[] getRequiredTokens() { + return getAcceptableTokens(); + } + + @Override + public void beginTree(DetailAST rootAST) { + depth = 0; + } + @Override public void visitToken(DetailAST literalTry) { - nestIn(literalTry, MSG_KEY); + if (depth > max) { + log(literalTry, MSG_KEY, depth, max); + } + ++depth; } @Override public void leaveToken(DetailAST literalTry) { - nestOut(); + --depth; } }