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 Jan 9, 2019
1 parent 6a315a1 commit c8b66e5
Show file tree
Hide file tree
Showing 13 changed files with 973 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .ci/jsoref-spellchecker/whitelist.words
Expand Up @@ -520,7 +520,7 @@ gzip
hadoop
hamcrest
Haml
Hardcoded
hardcoded
hashcode
hashset
hashtable
Expand Down Expand Up @@ -1192,6 +1192,7 @@ suppressionfilter
suppressionsloader
suppressionsstringprinter
suppressionxpathfilter
suppressionxpathsinglefilter
suppresswarnings
suppresswarningsfilter
suppresswarningsholder
Expand Down
7 changes: 7 additions & 0 deletions config/checkstyle_checks.xml
Expand Up @@ -350,6 +350,13 @@
<module name="SuppressionXpathFilter">
<property name="file" value="${checkstyle.suppressions-xpath.file}"/>
</module>
<module name="SuppressionXpathSingleFilter">
<property name="files" value="IGNORETHIS"/>
<property name="checks" value="IGNORETHIS"/>
<property name="message" value="IGNORETHIS"/>
<property name="id" value="IGNORETHIS"/>
<property name="query" value="IGNORETHIS"/>
</module>
<module name="SuppressWithNearbyCommentFilter">
<property name="commentFormat"
value="-@cs\[(\w{8,}(\|\w{8,})*)\] \w[\(\)\-\.\'\`\,\:\;\w ]{10,}"/>
Expand Down
13 changes: 13 additions & 0 deletions config/pmd-test.xml
Expand Up @@ -193,4 +193,17 @@
</properties>
</rule>

<rule ref="category/java/design.xml/UseObjectForClearerAPI">
<properties>
<!-- This is checking for amount of arguments more that 3 (hardcoded).
But in make sense to start making violations on items where
amount of arguments more that 7. Extra abstraction(object)
for single method does not make code more readable or
easy to maintain -->
<property name="violationSuppressXPath"
value="//ClassOrInterfaceDeclaration[
@Image='SuppressionXpathSingleFilterTest']
//MethodDeclarator[@Image='createSuppressionXpathSingleFilter']"/>
</properties>
</rule>
</ruleset>
3 changes: 3 additions & 0 deletions config/spotbugs-exclude.xml
Expand Up @@ -78,6 +78,9 @@
<Class name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineCheck" />
<!--Uses setters to set fields values-->
<Class name="com.puppycrawl.tools.checkstyle.api.AbstractCheck" />
<!-- We cannot modify fields since we won't have the values
to create the instance until the setters are called -->
<Class name="com.puppycrawl.tools.checkstyle.filters.SuppressionXpathSingleFilter"/>
</Or>
<Bug pattern="UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" />
</Match>
Expand Down
Expand Up @@ -843,6 +843,8 @@ private static void fillModulesFromFiltersPackage() {
BASE_PACKAGE + ".filters.SuppressionFilter");
NAME_TO_FULL_MODULE_NAME.put("SuppressionXpathFilter",
BASE_PACKAGE + ".filters.SuppressionXpathFilter");
NAME_TO_FULL_MODULE_NAME.put("SuppressionXpathSingleFilter",
BASE_PACKAGE + ".filters.SuppressionXpathSingleFilter");
NAME_TO_FULL_MODULE_NAME.put("SuppressWarningsFilter",
BASE_PACKAGE + ".filters.SuppressWarningsFilter");
NAME_TO_FULL_MODULE_NAME.put("SuppressWithNearbyCommentFilter",
Expand Down
Expand Up @@ -230,17 +230,9 @@
* &lt;property name=&quot;option&quot; value=&quot;bottom&quot;/&gt;
* &lt;property name=&quot;sortStaticImportsAlphabetically&quot; value=&quot;true&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <pre>
* &lt;?xml version=&quot;1.0&quot;?&gt;
* &lt;!DOCTYPE suppressions PUBLIC
* &quot;-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN&quot;
* &quot;https://checkstyle.org/dtds/suppressions_1_2.dtd&quot;&gt;
*
* &lt;suppressions&gt;
* &lt;!-- message contains no message text to work well in multi-language environments --&gt;
* &lt;suppress checks=&quot;ImportOrder&quot; message=&quot;^'java\..*'.*&quot;/&gt;
* &lt;/suppressions&gt;
* &lt;module name="SuppressionXpathSingleFilter"&gt;
* &lt;property name="checks" value="ImportOrder"/&gt;
* &lt;/module&gt;
* </pre>
* <p>
* To configure the check so that it matches default NetBeans formatter configuration
Expand Down
@@ -0,0 +1,127 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2018 the original author or authors.
//
// 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.filters;

import java.util.regex.Pattern;

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

/**
* Filter {@code SuppressionXpathSingleFilter} suppresses audit events for
* Checks violations in the specified file, class, checks, message, module id,
* and xpath.
* Attention: This filter only supports single suppression, and will need
* multiple instances if users wants to suppress multiple violations.
*/
public class SuppressionXpathSingleFilter extends AutomaticBean implements
TreeWalkerFilter {
/**
* XpathFilter instance.
*/
private XpathFilter xpathFilter;
/**
* The pattern for file names.
*/
private Pattern files;
/**
* The pattern for check class names.
*/
private Pattern checks;
/**
* The pattern for message names.
*/
private Pattern message;
/**
* Module id of filter.
*/
private String id;
/**
* Xpath query.
*/
private String query;

/**
* Set the regular expression to specify names of files to suppress.
* @param files the name of the file
*/
public void setFiles(String files) {
if (files == null) {
this.files = null;
}
else {
this.files = Pattern.compile(files);
}
}

/**
* Set the regular expression to specify the name of the check to suppress.
* @param checks the name of the check
*/
public void setChecks(String checks) {
if (checks == null) {
this.checks = null;
}
else {
this.checks = Pattern.compile(checks);
}
}

/**
* Set the regular expression to specify the message of the check to suppress.
* @param message the message of the check
*/
public void setMessage(String message) {
if (message == null) {
this.message = null;
}
else {
this.message = Pattern.compile(message);
}
}

/**
* Set the ID of the check to suppress.
* @param id the ID of the check
*/
public void setId(String id) {
this.id = id;
}

/**
* Set the xpath query.
* @param query the xpath query
*/
public void setQuery(String query) {
this.query = query;
}

@Override
protected void finishLocalSetup() {
xpathFilter = new XpathFilter(files, checks, message, id, query);
}

@Override
public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) {
return xpathFilter.accept(treeWalkerAuditEvent);
}

}
Expand Up @@ -111,7 +111,57 @@ public XpathFilter(String files, String checks,
xpathExpression = xpathEvaluator.createExpression(xpathQuery);
}
catch (XPathException ex) {
throw new IllegalStateException("Unexpected xpath query: " + xpathQuery, ex);
throw new IllegalArgumentException("Unexpected xpath query: " + xpathQuery, ex);
}
}
}

/**
* Creates a {@code XpathElement} instance.
* @param files regular expression for names of filtered files
* @param checks regular expression for filtered check classes
* @param message regular expression for messages.
* @param moduleId the module id
* @param query the xpath query
*/
public XpathFilter(Pattern files, Pattern checks, Pattern message,
String moduleId, String query) {
if (files == null) {
filePattern = null;
fileRegexp = null;
}
else {
filePattern = files.pattern();
fileRegexp = files;
}
if (checks == null) {
checkPattern = null;
checkRegexp = null;
}
else {
checkPattern = checks.pattern();
checkRegexp = checks;
}
if (message == null) {
messagePattern = null;
messageRegexp = null;
}
else {
messagePattern = message.pattern();
messageRegexp = message;
}
this.moduleId = moduleId;
xpathQuery = query;
if (xpathQuery == null) {
xpathExpression = null;
}
else {
final XPathEvaluator xpathEvaluator = new XPathEvaluator();
try {
xpathExpression = xpathEvaluator.createExpression(xpathQuery);
}
catch (XPathException ex) {
throw new IllegalArgumentException("Incorrect xpath query: " + xpathQuery, ex);
}
}
}
Expand Down

0 comments on commit c8b66e5

Please sign in to comment.