forked from checkstyle/checkstyle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#5879: Suppression xpath single filter
- Loading branch information
1 parent
7ea6a08
commit a450c60
Showing
8 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathSingleFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
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; | ||
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 { | ||
/** | ||
* Default check format to suppress. By default the filter suppress all checks. | ||
*/ | ||
private static final String DEFAULT_CHECK_FORMAT = ".*"; | ||
/** | ||
* The pattern for file names. | ||
*/ | ||
private Pattern files = Pattern.compile(DEFAULT_CHECK_FORMAT); | ||
/** | ||
* The pattern for check class names. | ||
*/ | ||
private Pattern checks = Pattern.compile(DEFAULT_CHECK_FORMAT); | ||
/** | ||
* The pattern for message names. | ||
*/ | ||
private Pattern message = Pattern.compile(DEFAULT_CHECK_FORMAT); | ||
/** | ||
* Module id filter. | ||
*/ | ||
private String id; | ||
/** | ||
* Xpath query. | ||
*/ | ||
private String query; | ||
|
||
public void setFiles(String files) { | ||
this.files = Pattern.compile(files); | ||
} | ||
|
||
public void setChecks(String checks) { | ||
this.checks = Pattern.compile(checks); | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = Pattern.compile(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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionXpathSingleFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
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, 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)); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ls/checkstyle/filters/suppressionxpathsinglefilter/InputSuppressionXpathSingleFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.puppycrawl.tools.checkstyle.filters.suppressionxpathsinglefilter; | ||
|
||
public class InputSuppressionXpathSingleFilter { | ||
private int age = 23; | ||
|
||
public void changeAge() { | ||
age = 24; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters