From 3514294e655ea9e2e35f21ec1c94cef8f4c6d945 Mon Sep 17 00:00:00 2001 From: rnveach Date: Sat, 31 Oct 2015 10:33:12 -0400 Subject: [PATCH] Issue #2451: removed excess hierarchy from MutableExceptionCheck --- config/suppressions.xml | 2 +- .../checks/design/MutableExceptionCheck.java | 30 ++++++++++++------- .../tools/checkstyle/XDocsPagesTest.java | 1 - 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/config/suppressions.xml b/config/suppressions.xml index 7461156bc..4b82755e8 100644 --- a/config/suppressions.xml +++ b/config/suppressions.xml @@ -37,7 +37,7 @@ files="AbstractClassNameCheckTest.java|AbstractTypeAwareCheckTest.java|AbstractJavadocCheckTest.java|AbstractViolationReporterTest.java"/> - + diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/MutableExceptionCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/MutableExceptionCheck.java index 6564d8f6a..d5f62075b 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/MutableExceptionCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/design/MutableExceptionCheck.java @@ -21,10 +21,12 @@ package com.puppycrawl.tools.checkstyle.checks.design; import java.util.ArrayDeque; import java.util.Deque; +import java.util.regex.Pattern; +import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.TokenTypes; -import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck; +import com.puppycrawl.tools.checkstyle.utils.CommonUtils; /** *

Ensures that exceptions (classes with names conforming to some regular @@ -39,7 +41,7 @@ import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck; * * @author Simon Harris */ -public final class MutableExceptionCheck extends AbstractFormatCheck { +public final class MutableExceptionCheck extends Check { /** * A key is pointing to the warning message text in "messages.properties" @@ -50,17 +52,15 @@ public final class MutableExceptionCheck extends AbstractFormatCheck { /** Default value for format and extendedClassNameFormat properties. */ private static final String DEFAULT_FORMAT = "^.*Exception$|^.*Error$|^.*Throwable$"; /** Pattern for class name that is being extended. */ - private String extendedClassNameFormat; + private String extendedClassNameFormat = DEFAULT_FORMAT; /** Stack of checking information for classes. */ private final Deque checkingStack = new ArrayDeque<>(); /** Should we check current class or not. */ private boolean checking; - - /** Creates new instance of the check. */ - public MutableExceptionCheck() { - super(DEFAULT_FORMAT); - extendedClassNameFormat = DEFAULT_FORMAT; - } + /** The format string of the regexp. */ + private String format = DEFAULT_FORMAT; + /** The regexp to match against. */ + private Pattern regexp = Pattern.compile(format); /** * Sets the format of extended class name to the specified regular expression. @@ -70,6 +70,16 @@ public final class MutableExceptionCheck extends AbstractFormatCheck { this.extendedClassNameFormat = extendedClassNameFormat; } + /** + * Set the format to the specified regular expression. + * @param format a {@code String} value + * @throws org.apache.commons.beanutils.ConversionException unable to parse format + */ + public void setFormat(String format) { + this.format = format; + regexp = CommonUtils.createPattern(format); + } + @Override public int[] getDefaultTokens() { return new int[] {TokenTypes.CLASS_DEF, TokenTypes.VARIABLE_DEF}; @@ -143,7 +153,7 @@ public final class MutableExceptionCheck extends AbstractFormatCheck { */ private boolean isNamedAsException(DetailAST ast) { final String className = ast.findFirstToken(TokenTypes.IDENT).getText(); - return getRegexp().matcher(className).find(); + return regexp.matcher(className).find(); } /** diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/XDocsPagesTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/XDocsPagesTest.java index 8239ca271..744f3c253 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/XDocsPagesTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/XDocsPagesTest.java @@ -111,7 +111,6 @@ public class XDocsPagesTest { private static final List UNDOCUMENTED_PROPERTIES = Arrays.asList( "SuppressWithNearbyCommentFilter.fileContents", "IllegalTokenTextCheck.compileFlags", - "MutableExceptionCheck.compileFlags", "AbstractClassNameCheck.compileFlags", "ClassTypeParameterNameCheck.compileFlags", "ConstantNameCheck.compileFlags",