diff --git a/config/suppressions.xml b/config/suppressions.xml index fe18472f2..7461156bc 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/coding/ReturnCountCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java index 46be5f2b9..9e65e14c4 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/ReturnCountCheck.java @@ -21,10 +21,12 @@ package com.puppycrawl.tools.checkstyle.checks.coding; 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; /** *

@@ -38,7 +40,7 @@ import com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck; * * @author Simon Harris */ -public final class ReturnCountCheck extends AbstractFormatCheck { +public final class ReturnCountCheck extends Check { /** * A key is pointing to the warning message text in "messages.properties" @@ -46,22 +48,18 @@ public final class ReturnCountCheck extends AbstractFormatCheck { */ public static final String MSG_KEY = "return.count"; - /** Default allowed number of return statements. */ - private static final int DEFAULT_MAX = 2; + /** The format string of the regexp. */ + private String format = "^equals$"; + /** The regexp to match against. */ + private Pattern regexp = Pattern.compile(format); /** Stack of method contexts. */ private final Deque contextStack = new ArrayDeque<>(); - /** Maximum allowed number of return stmts. */ - private int max; + /** Maximum allowed number of return statements. */ + private int max = 2; /** Current method context. */ private Context context; - /** Creates new instance of the checks. */ - public ReturnCountCheck() { - super("^equals$"); - max = DEFAULT_MAX; - } - @Override public int[] getDefaultTokens() { return new int[] { @@ -89,6 +87,16 @@ public final class ReturnCountCheck extends AbstractFormatCheck { }; } + /** + * 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); + } + /** * Getter for max property. * @return maximum allowed number of return statements. @@ -152,7 +160,7 @@ public final class ReturnCountCheck extends AbstractFormatCheck { private void visitMethodDef(DetailAST ast) { contextStack.push(context); final DetailAST methodNameAST = ast.findFirstToken(TokenTypes.IDENT); - final boolean check = !getRegexp().matcher(methodNameAST.getText()).find(); + final boolean check = !regexp.matcher(methodNameAST.getText()).find(); context = new Context(check); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/XDocsPagesTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/XDocsPagesTest.java index 0ffb433da..8239ca271 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", - "ReturnCountCheck.compileFlags", "MutableExceptionCheck.compileFlags", "AbstractClassNameCheck.compileFlags", "ClassTypeParameterNameCheck.compileFlags",