Skip to content

Commit

Permalink
Issue checkstyle#5879: Suppression xpath single filter
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunghanjacktsai committed Nov 20, 2018
1 parent e2f7bb6 commit afd6b0c
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/checkstyle_checks.xml
Expand Up @@ -350,6 +350,7 @@
<module name="SuppressionXpathFilter">
<property name="file" value="${checkstyle.suppressions-xpath.file}"/>
</module>
<module name="SuppressionXpathSingleFilter"/>
<module name="SuppressWithNearbyCommentFilter">
<property name="commentFormat"
value="-@cs\[(\w{8,}(\|\w{8,})*)\] \w[\(\)\-\.\'\`\,\:\;\w ]{10,}"/>
Expand Down
@@ -0,0 +1,60 @@
package com.puppycrawl.tools.checkstyle.filters;

import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;

/**
* This filter processes {@link TreeWalkerAuditEvent}
* objects based on the criteria of files, checks, message, module id, xpathQuery.
*/
public class SuppressionXpathSingleFilter extends AutomaticBean implements
TreeWalkerFilter {

/** The pattern for file names. */
private String files;

/** The pattern for check class names. */
private String checks;

/** The pattern for message names. */
private String message;

/** Module id filter. */
private String id;

/** Xpath query. */
private String query;

public void setFiles(String files) {
this.files = files;
}

public void setChecks(String checks) {
this.checks = checks;
}

public void setMessage(String message) {
this.message = message;
}

public void setId(String id) {
this.id = id;
}

public void setQuery(String query) {
this.query = query;
}

@Override
public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) {
XpathFilter xpathFilter = new XpathFilter(files, checks, message, id, query);
return xpathFilter.accept(treeWalkerAuditEvent);
}

@Override
protected void finishLocalSetup() throws CheckstyleException {
// No code by default
}
}
@@ -0,0 +1,74 @@
package com.puppycrawl.tools.checkstyle.filters;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.charset.StandardCharsets;

import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.JavaParser;
import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.api.*;
import org.junit.Before;
import org.junit.Test;

public class SuppressionXpathSingleFilterTest
extends AbstractModuleTestSupport {
private File file;
private FileContents fileContents;

@Before
public void setUp() throws Exception {
file = new File(getPath("InputSuppressionXpathSingleFilter.java"));
fileContents = new FileContents(new FileText(file,
StandardCharsets.UTF_8.name()));
}

@Override
protected String getPackageLocation() {
return "com/puppycrawl/tools/checkstyle/filters/suppressionxpathsinglefilter";
}

@Test
public void testDefault() throws Exception {
final String xpath = "/CLASS_DEF[@text='InputTest']" +
"//METHOD_DEF[@text='changeAge']//ASSIGN[@text='age']/IDENT";
final SuppressionXpathSingleFilter filter = createSuppressionXpathSingleFilter(
"InputSuppressionXpathSingleFilter", null, null, null, null);
final TreeWalkerAuditEvent ev = getEvent(3, 8, TokenTypes.VARIABLE_DEF);
assertFalse("Event should be rejected", filter.accept(ev));
}

@Test
public void testRequireThis() throws Exception {
final String xpath = "/CLASS_DEF[@text='InputTest']" +
"//METHOD_DEF[@text='changeAge']//ASSIGN[@text='age']/IDENT";
final SuppressionXpathSingleFilter filter = createSuppressionXpathSingleFilter(
"InputSuppressionXpathSingleFilter", "RequireThis", null, null, xpath);
final TreeWalkerAuditEvent ev = getEvent(3, 8, TokenTypes.VARIABLE_DEF);
assertTrue("Event should be accepted", filter.accept(ev));
}

private static SuppressionXpathSingleFilter createSuppressionXpathSingleFilter(
String files, String checks, String message, String id, String query)
throws CheckstyleException {
final SuppressionXpathSingleFilter filter = new SuppressionXpathSingleFilter();
filter.setFiles(files);
filter.setChecks(checks);
filter.setMessage(message);
filter.setId(id);
filter.setQuery(query);
filter.finishLocalSetup();
return filter;
}

private TreeWalkerAuditEvent getEvent(int line, int column, int tokenType)
throws Exception {
final LocalizedMessage message =
new LocalizedMessage(line, column, tokenType, "", "", null, null, null,
getClass(), null);
return new TreeWalkerAuditEvent(fileContents, file.getName(), message,
JavaParser.parseFile(file, JavaParser.Options.WITHOUT_COMMENTS));
}
}
@@ -0,0 +1,9 @@
package com.puppycrawl.tools.checkstyle.filters.suppressionxpathsinglefilter;

public class InputSuppressionXpathSingleFilter {
private int age = 23;

public void changeAge() {
age = 24;
}
}
81 changes: 81 additions & 0 deletions src/xdocs/config_filters.xml
Expand Up @@ -985,6 +985,87 @@ public class InputTest {
</subsection>
</section>

<section name="SuppressionXpathSingleFilter">
<subsection name="Description" id="SuppressionXpathSingleFilter_Description">
<p>Since Checkstyle 8.15</p>
</subsection>
<subsection name="Properties" id="SuppressionXpathSingleFilter_Properties">
<table>
<tr>
<th>name</th>
<th>description</th>
<th>type</th>
<th>default value</th>
<th>since</th>
</tr>
<tr>
<td>files</td>
<td>a Regular Expression matched against the file name associated
with an audit event. It is optional.</td>
<td><a href="property_types.html#regexp">Regular Expression</a></td>
<td><code>".*"</code></td>
<td>8.15</td>
</tr>
<tr>
<td>checks</td>
<td>a Regular Expression matched against the name of the check
associated with an audit event. Optional as long as id or message
is specified.</td>
<td><a href="property_types.html#regexp">Regular Expression</a></td>
<td><code>".*"</code></td>
<td>8.15</td>
</tr>
<tr>
<td>message</td>
<td>a Regular Expression matched against the message of the check associated
with an audit event. Optional as long as checks or id is specified.</td>
<td><a href="property_types.html#regexp">Regular Expression</a></td>
<td><code>".*"</code></td>
<td>8.15</td>
</tr>
<tr>
<td>id</td>
<td>a string matched against the ID of the check associated with an audit event.
Optional as long as checks or message is specified.</td>
<td><a href="property_types.html#string">String</a></td>
<td><code>null</code></td>
<td>3.5</td>
</tr>
<tr>
<td>query</td>
<td>a string xpath query. It is optional.</td>
<td><a href="property_types.html#string">String</a></td>
<td><code>null</code></td>
<td>8.15</td>
</tr>
</table>
</subsection>
<subsection name="Examples" id="SuppressionXpathSingleFilter_Examples">
<p>
The following suppressions XML document directs a SuppressionXpathFilter to
reject CyclomaticComplexity errors for all methods with name sayHelloWorld
inside FileOne classes:
</p>
<pre>

</pre>
</subsection>
<subsection name="Example of Usage" id="SuppressionXpathSingleFilter_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+SuppressionXpathFilter">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Package" id="SuppressionXpathSingleFilter_Package">
<p> com.puppycrawl.tools.checkstyle.filters </p>
</subsection>
<subsection name="Parent Module" id="SuppressionXpathSingleFilter_Parent_Module">
<p> <a href="config.html#TreeWalker">TreeWalker</a> </p>
</subsection>
</section>

<section name="SuppressWarningsFilter">
<subsection name="Description" id="SuppressWarningsFilter_Description">
<p>Since Checkstyle 5.7</p>
Expand Down

0 comments on commit afd6b0c

Please sign in to comment.