Added check that checks for a required regexp, contributed by Daniel Grenner
request 606115, patch 902189
This commit is contained in:
parent
e578713af0
commit
65603924fd
|
|
@ -40,6 +40,9 @@
|
|||
<li>
|
||||
<a href="#NewlineAtEndOfFile">NewlineAtEndOfFile</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="#RequiredRegexp">RequiredRegexp</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="#TodoComment">TodoComment</a>
|
||||
</li>
|
||||
|
|
@ -874,6 +877,46 @@ like <span class="code">1</span>.
|
|||
<a href="config.html#treewalker">TreeWalker</a>
|
||||
</p>
|
||||
|
||||
<!-- --> <a name="RequiredRegexp"></a> <h2>RequiredRegexp</h2> <h4>Description</h4>
|
||||
<p class="body">
|
||||
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.
|
||||
</p>
|
||||
<h4>Properties</h4>
|
||||
<table width="100%" border="1" cellpadding="5" class="body">
|
||||
<tr class="header">
|
||||
<th>name</th>
|
||||
<th>description</th>
|
||||
<th>type</th>
|
||||
<th>default value</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>format</td>
|
||||
<td>required pattern</td>
|
||||
<td><a href="property_types.html#regexp">regular expression</a></td>
|
||||
<td><span class="default">^$</span> (empty)</td>
|
||||
</tr>
|
||||
</table>
|
||||
<h4>Examples</h4>
|
||||
<p class="body">
|
||||
An example of how to configure the check to make sure a copyright statement
|
||||
is included in the file:
|
||||
</p>
|
||||
<pre class="body">
|
||||
<module name="RequiredRegexp">
|
||||
<property name="format" value="This code is copyrighted"/>
|
||||
</module>
|
||||
</pre>
|
||||
|
||||
<h4>Package</h4>
|
||||
<p class="body">
|
||||
com.puppycrawl.tools.checkstyle.checks
|
||||
</p>
|
||||
<h4>Parent Module</h4>
|
||||
<p class="body">
|
||||
<a href="config.html#treewalker">TreeWalker</a>
|
||||
</p>
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
|
|
|||
|
|
@ -62,6 +62,10 @@
|
|||
to Ant task to allow finetuning of failure behaviour
|
||||
(request 783538).</li>
|
||||
|
||||
<li class="body">Added check that checks for a required regexp,
|
||||
contributed by Daniel Grenner
|
||||
(module RequiredRegexp, request 606115, patch 902189).</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<p class="body">
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* A check that makes sure that a specified pattern exists in the code.
|
||||
* </p>
|
||||
* <p>
|
||||
* 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):
|
||||
* </p>
|
||||
* <pre>
|
||||
* <module name="RequiredRegexp">
|
||||
* <property name="format" value="This code is copyrighted"/>
|
||||
* </module>
|
||||
* </pre>
|
||||
* @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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue