From 65603924fdf47c194fc2ef9e0bc60bfc81ee4166 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lars=20K=C3=BChne?=
+ A check that makes sure that a specified pattern exists in the code, e.g. a required + legal text. It does not care about where in the file the pattern is. +
+| name | +description | +type | +default value | +
|---|---|---|---|
| format | +required pattern | +regular expression | +^$ (empty) | +
+ An example of how to configure the check to make sure a copyright statement + is included in the file: +
++ <module name="RequiredRegexp"> + <property name="format" value="This code is copyrighted"/> + </module> ++ +
+ com.puppycrawl.tools.checkstyle.checks +
++ TreeWalker +
+ diff --git a/docs/releasenotes.html b/docs/releasenotes.html index ad1bb563a..934765d34 100644 --- a/docs/releasenotes.html +++ b/docs/releasenotes.html @@ -62,6 +62,10 @@ to Ant task to allow finetuning of failure behaviour (request 783538). +diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RequiredRegexpCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RequiredRegexpCheck.java new file mode 100644 index 000000000..b45a0ef9a --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/RequiredRegexpCheck.java @@ -0,0 +1,72 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2004 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.checks; + +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import org.apache.regexp.RE; + + +/** + *
+ * A check that makes sure that a specified pattern exists in the code. + *
+ *+ * An example of how to configure the check to make sure a copyright statement + * is included in the file (but without requirements on where in the file + * it should be): + *
+ *+ * <module name="RequiredRegexp"> + * <property name="format" value="This code is copyrighted"/> + * </module> + *+ * @author Daniel Grenner + */ +public class RequiredRegexpCheck extends AbstractFormatCheck +{ + /** + * Instantiates an new GenericIllegalRegexpCheck. + */ + public RequiredRegexpCheck() + { + super("$^"); // the empty language + } + + /** @see com.puppycrawl.tools.checkstyle.api.Check */ + public int[] getDefaultTokens() + { + return new int[0]; + } + + /** @see com.puppycrawl.tools.checkstyle.api.Check */ + public void beginTree(DetailAST aRootAST) + { + final RE regexp = getRegexp(); + final String[] lines = getLines(); + for (int i = 0; i < lines.length; i++) { + + final String line = lines[i]; + if (regexp.match(line)) { + return; + } + } + log(0, "required.regexp", getFormat()); + } +} + diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties index 1930e0e58..7e21e662c 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties @@ -13,6 +13,7 @@ mod.order=''{0}'' modifier out of order with the JLS suggestions. illegal.regexp=Line matches the illegal pattern ''{0}''. +required.regexp=Required pattern ''{0}'' missing in file. translation.missingKey=Key ''{0}'' missing. diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages_de.properties b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages_de.properties index 43718ad1d..1cde3b2e9 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages_de.properties +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages_de.properties @@ -14,6 +14,7 @@ mod.order=Modifier ''{0}'' weicht von der empfohlenen Modifier-Reihenfolge aus d illegal.regexp=Die Zeile entspricht dem verbotenen Muster ''{0}''. +required.regexp=Keine Zeile entspricht dem Muster ''{0}''. translation.missingKey=Übersetzung für Schlüssel ''{0}'' fehlt. diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/AllTests.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/AllTests.java index cbd8c961d..cca08fabd 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/AllTests.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/AllTests.java @@ -23,6 +23,7 @@ public class AllTests { suite.addTest(new TestSuite(ModifierOrderCheckTest.class)); suite.addTest(new TestSuite(NewlineAtEndOfFileCheckTest.class)); suite.addTest(new TestSuite(RedundantModifierTest.class)); + suite.addTest(new TestSuite(RequiredRegexpCheckTest.class)); suite.addTest(new TestSuite(TodoCommentCheckTest.class)); suite.addTest(new TestSuite(TrailingCommentCheckTest.class)); suite.addTest(new TestSuite(TranslationCheckTest.class)); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/RequiredRegexpCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/RequiredRegexpCheckTest.java new file mode 100644 index 000000000..7aebb2876 --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/RequiredRegexpCheckTest.java @@ -0,0 +1,45 @@ +package com.puppycrawl.tools.checkstyle.checks; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; + +public class RequiredRegexpCheckTest + extends BaseCheckTestCase +{ + public void testExistingInDoc() + throws Exception + { + final String required = "Test case file"; + final DefaultConfiguration checkConfig = + createCheckConfig(RequiredRegexpCheck.class); + checkConfig.addAttribute("format", required); + final String[] expected = { + }; + verify(checkConfig, getPath("InputSemantic.java"), expected); + } + + public void testExistingInCode() + throws Exception + { + final String required = "package"; + final DefaultConfiguration checkConfig = + createCheckConfig(RequiredRegexpCheck.class); + checkConfig.addAttribute("format", required); + final String[] expected = { + }; + verify(checkConfig, getPath("InputSemantic.java"), expected); + } + + public void testMissing() + throws Exception + { + final String required = "This text is not in the file"; + final DefaultConfiguration checkConfig = + createCheckConfig(RequiredRegexpCheck.class); + checkConfig.addAttribute("format", required); + final String[] expected = { + "0: Required pattern '" + required + "' missing in file." + }; + verify(checkConfig, getPath("InputSemantic.java"), expected); + } +}