diff options
author | rnveach <rveach02@gmail.com> | 2015-10-31 19:12:30 -0400 |
---|---|---|
committer | Roman Ivanov <ivanov-jr@mail.ru> | 2015-10-31 21:49:23 -0700 |
commit | 53ad5f4a6bf3a073b7ccaea51ce4ac85151ca672 (patch) | |
tree | b1baffb35362a4e49b89a5c4b0292e8cd8b85001 /src | |
parent | 3514294e655ea9e2e35f21ec1c94cef8f4c6d945 (diff) | |
download | checkstyle-53ad5f4a6bf3a073b7ccaea51ce4ac85151ca672.tar.gz |
Issue #2451: removed excess hierarchy from IllegalCatchCheck
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheck.java | 33 | ||||
-rw-r--r-- | src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheckTest.java | 18 |
2 files changed, 45 insertions, 6 deletions
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheck.java index 553a730b3..5843e3faa 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheck.java @@ -21,7 +21,10 @@ package com.puppycrawl.tools.checkstyle.checks.coding; import java.util.LinkedList; import java.util.List; +import java.util.Set; +import com.google.common.collect.Sets; +import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.FullIdent; import com.puppycrawl.tools.checkstyle.api.TokenTypes; @@ -32,7 +35,7 @@ import com.puppycrawl.tools.checkstyle.api.TokenTypes; * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a> * @author <a href="mailto:IliaDubinin91@gmail.com">Ilja Dubinin</a> */ -public final class IllegalCatchCheck extends AbstractIllegalCheck { +public final class IllegalCatchCheck extends Check { /** * A key is pointing to the warning message text in "messages.properties" @@ -40,10 +43,28 @@ public final class IllegalCatchCheck extends AbstractIllegalCheck { */ public static final String MSG_KEY = "illegal.catch"; - /** Creates new instance of the check. */ - public IllegalCatchCheck() { - super("Exception", "Error", "RuntimeException", "Throwable", "java.lang.Error", - "java.lang.Exception", "java.lang.RuntimeException", "java.lang.Throwable"); + /** Illegal class names. */ + private final Set<String> illegalClassNames = Sets.newHashSet("Exception", "Error", + "RuntimeException", "Throwable", "java.lang.Error", "java.lang.Exception", + "java.lang.RuntimeException", "java.lang.Throwable"); + + /** + * Set the list of illegal classes. + * + * @param classNames + * array of illegal exception classes + */ + public void setIllegalClassNames(final String... classNames) { + illegalClassNames.clear(); + for (final String name : classNames) { + illegalClassNames.add(name); + final int lastDot = name.lastIndexOf('.'); + if (lastDot > 0 && lastDot < name.length() - 1) { + final String shortName = name + .substring(name.lastIndexOf('.') + 1); + illegalClassNames.add(shortName); + } + } } @Override @@ -72,7 +93,7 @@ public final class IllegalCatchCheck extends AbstractIllegalCheck { for (DetailAST excType : excTypes) { final FullIdent ident = FullIdent.createFullIdent(excType); - if (isIllegalClassName(ident.getText())) { + if (illegalClassNames.contains(ident.getText())) { log(detailAST, MSG_KEY, ident.getText()); } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheckTest.java index 30213b2b0..3231399a2 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/IllegalCatchCheckTest.java @@ -70,6 +70,24 @@ public class IllegalCatchCheckTest extends BaseCheckTestSupport { } @Test + public void testIllegalClassNamesBad() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(IllegalCatchCheck.class); + checkConfig.addAttribute("illegalClassNames", + "java.lang.Error, java.lang.Exception, NullPointerException"); + + // check that incorrect names don't break the Check + checkConfig.addAttribute("illegalClassNames", + "java.lang.IOException."); + + final String[] expected = { + "7:11: " + getCheckMessage(MSG_KEY, "Exception"), + "15:11: " + getCheckMessage(MSG_KEY, "java.lang.Exception"), + }; + + verify(checkConfig, getPath("InputIllegalCatch.java"), expected); + } + + @Test public void testMultipleTypes() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(IllegalCatchCheck.class); |