diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/ThrowsCountCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/ThrowsCountCheck.java index c4818f812..94ef42ba9 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/ThrowsCountCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/ThrowsCountCheck.java @@ -41,6 +41,10 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes; * a caller but allows any sub-classes to be caught * specifically if necessary. *
+ *+ * ignorePrivateMethods - allows to skip private methods as they do + * not cause problems for other classes. + *
* @author Simon Harris */ public final class ThrowsCountCheck extends Check { @@ -54,6 +58,9 @@ public final class ThrowsCountCheck extends Check { /** default value of max property */ private static final int DEFAULT_MAX = 1; + /** whether private methods must be ignored **/ + private boolean ignorePrivateMethods = true; + /** maximum allowed throws statements */ private int max; @@ -89,6 +96,14 @@ public final class ThrowsCountCheck extends Check { return max; } + /** + * Sets whether private methods must be ignored. + * @param ignorePrivateMethods whether private methods must be ignored. + */ + public void setIgnorePrivateMethods(boolean ignorePrivateMethods) { + this.ignorePrivateMethods = ignorePrivateMethods; + } + /** * Setter for max property. * @param max maximum allowed throws statements. @@ -112,7 +127,8 @@ public final class ThrowsCountCheck extends Check { * @param ast throws for check. */ private void visitLiteralThrows(DetailAST ast) { - if (!isOverriding(ast)) { + if ((!ignorePrivateMethods || !isInPrivateMethod(ast)) + && !isOverriding(ast)) { // Account for all the commas! final int count = (ast.getChildCount() + 1) / 2; if (count > getMax()) { @@ -159,4 +175,14 @@ public final class ThrowsCountCheck extends Check { } return name; } + + /** + * Checks if method, which throws an exception is private. + * @param ast throws, which is being checked. + * @return true, if method, which throws an exception is private. + */ + private static boolean isInPrivateMethod(DetailAST ast) { + final DetailAST methodModifiers = ast.getParent().findFirstToken(TokenTypes.MODIFIERS); + return methodModifiers.findFirstToken(TokenTypes.LITERAL_PRIVATE) != null; + } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/ThrowsCountCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/ThrowsCountCheckTest.java index 96c494c7c..4783dbe3c 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/ThrowsCountCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/design/ThrowsCountCheckTest.java @@ -45,7 +45,7 @@ public class ThrowsCountCheckTest extends BaseCheckTestSupport { "14:20: " + getCheckMessage(MSG_KEY, 2, 1), "18:20: " + getCheckMessage(MSG_KEY, 2, 1), "22:20: " + getCheckMessage(MSG_KEY, 3, 1), - "45:43: " + getCheckMessage(MSG_KEY, 2, 1), + "48:43: " + getCheckMessage(MSG_KEY, 2, 1), }; verify(checkConfig, getPath("design" + File.separator + "InputThrowsCount.java"), expected); @@ -90,4 +90,18 @@ public class ThrowsCountCheckTest extends BaseCheckTestSupport { assertEquals(ast.toString(), e.getMessage()); } } + + @Test + public void testNotIgnorePrivateMethod() throws Exception { + DefaultConfiguration checkConfig = createCheckConfig(ThrowsCountCheck.class); + checkConfig.addAttribute("ignorePrivateMethods", "false"); + String[] expected = { + "14:20: " + getCheckMessage(MSG_KEY, 2, 1), + "18:20: " + getCheckMessage(MSG_KEY, 2, 1), + "22:20: " + getCheckMessage(MSG_KEY, 3, 1), + "29:28: " + getCheckMessage(MSG_KEY, 3, 1), + "48:43: " + getCheckMessage(MSG_KEY, 2, 1), + }; + verify(checkConfig, getPath("design" + File.separator + "InputThrowsCount.java"), expected); + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/design/InputThrowsCount.java b/src/test/resources/com/puppycrawl/tools/checkstyle/design/InputThrowsCount.java index dfb57c15a..5024e710b 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/design/InputThrowsCount.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/design/InputThrowsCount.java @@ -25,6 +25,9 @@ public class InputThrowsCount { void method6() { } + + private void method7() throws Exception, AWTException, Throwable { + } } class SubClass extends InputThrowsCount { diff --git a/src/xdocs/config_design.xml b/src/xdocs/config_design.xml index 51053a221..ac6401f8c 100644 --- a/src/xdocs/config_design.xml +++ b/src/xdocs/config_design.xml @@ -591,6 +591,11 @@ public class StringUtils // not final to allow subclassing type of exception need be checked for by a caller but any subclasses can be caught specifically if necessary. + ++ ignorePrivateMethods - allows to skip private methods as they do + not cause problems for other classes. +
1true+ To configure the check so that it doesn't skip private methods: +
+