diff options
author | rnveach <rveach02@gmail.com> | 2015-11-08 23:06:07 -0500 |
---|---|---|
committer | Roman Ivanov <ivanov-jr@mail.ru> | 2015-11-09 10:06:32 -0800 |
commit | 284c72cc83dfdc780545e667f5eb13982bf502e0 (patch) | |
tree | 773bfccf0394bdab955b48c613b1523c42b6fcbf /src/main | |
parent | 7503098309bf341eaea789595fa71a7ab740b49b (diff) | |
download | checkstyle-284c72cc83dfdc780545e667f5eb13982bf502e0.tar.gz |
Issue #2451: removed excess hierarchy from NestedTryDepthCheckTest
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/NestedTryDepthCheck.java | 37 |
1 files changed, 28 insertions, 9 deletions
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 <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> */ -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 @@ -53,13 +59,26 @@ public final class NestedTryDepthCheck extends AbstractNestedDepthCheck { } @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; } } |