From fbd55d01edfb957b8ba57efafd17b658c94a498b Mon Sep 17 00:00:00 2001
From: Timur
Date: Tue, 18 Jul 2017 14:07:19 +0600
Subject: [PATCH] Issue #4714: Make SuppressionCommentFilter and
SuppressWithNearbyCommentFilter children of TreeWalker
---
config/checkstyle_checks.xml | 28 ++---
.../tools/checkstyle/TreeWalker.java | 74 +++++++++--
.../tools/checkstyle/api/AstFilter.java | 34 ++++++
.../checkstyle/api/TreeWalkerAuditEvent.java | 115 ++++++++++++++++++
.../SuppressWithNearbyCommentFilter.java | 15 ++-
.../filters/SuppressionCommentFilter.java | 16 ++-
.../SuppressWithNearbyCommentFilterTest.java | 15 +--
.../filters/SuppressionCommentFilterTest.java | 21 ++--
.../checkstyle/internal/XdocsPagesTest.java | 2 -
src/xdocs/config_filters.xml | 6 +-
10 files changed, 260 insertions(+), 66 deletions(-)
create mode 100644 src/main/java/com/puppycrawl/tools/checkstyle/api/AstFilter.java
create mode 100644 src/main/java/com/puppycrawl/tools/checkstyle/api/TreeWalkerAuditEvent.java
diff --git a/config/checkstyle_checks.xml b/config/checkstyle_checks.xml
index 24a0850003ec..d33cef488d9a 100644
--- a/config/checkstyle_checks.xml
+++ b/config/checkstyle_checks.xml
@@ -31,24 +31,10 @@
-
-
-
-
-
-
-
-
-
-
-
@@ -155,6 +141,20 @@
value="The warning ''{0}'' cannot be suppressed at this location. Only few javac warnings are allowed to suppress. If try to suppress checkstyle/pmd/..... violation please do this in their config file. If you try to suppress IntelliJ IDEA inspection, please use javadoc block tag @noinspection"
/>
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
index 7fef3bac2747..abdf5a2a2a06 100644
--- a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
@@ -29,6 +29,8 @@
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
import antlr.CommonHiddenStreamToken;
import antlr.RecognitionException;
@@ -40,6 +42,8 @@
import com.google.common.collect.Multimap;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
+import com.puppycrawl.tools.checkstyle.api.AstFilter;
+import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.Context;
@@ -47,7 +51,9 @@
import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.FileText;
+import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
+import com.puppycrawl.tools.checkstyle.api.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaLexer;
import com.puppycrawl.tools.checkstyle.grammars.GeneratedJavaRecognizer;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
@@ -59,6 +65,7 @@
*
* @author Oliver Burn
*/
+// -@cs[ClassFanOutComplexity] Number of classes current class relies on exceeds 25.
public final class TreeWalker extends AbstractFileSetCheck implements ExternalResourceHolder {
/** Default distance between tab stops. */
@@ -78,6 +85,12 @@ public final class TreeWalker extends AbstractFileSetCheck implements ExternalRe
/** Registered comment checks. */
private final Set commentChecks = new HashSet<>();
+ /** The ast filters. */
+ private final Set filters = new HashSet<>();
+
+ /** The sorted set of messages. */
+ private final SortedSet messages = new TreeSet<>();
+
/** The distance between tab stops. */
private int tabWidth = DEFAULT_TAB_WIDTH;
@@ -149,18 +162,26 @@ public void setupChild(Configuration childConf)
throws CheckstyleException {
final String name = childConf.getName();
final Object module = moduleFactory.createModule(name);
- if (!(module instanceof AbstractCheck)) {
+ if (module instanceof AbstractCheck || module instanceof AstFilter) {
+ final AutomaticBean bean = (AutomaticBean) module;
+ bean.contextualize(childContext);
+ bean.configure(childConf);
+ if (module instanceof AbstractCheck) {
+ final AbstractCheck check = (AbstractCheck) module;
+ check.init();
+ registerCheck(check);
+ }
+ else {
+ final AstFilter filter = (AstFilter) module;
+ filters.add(filter);
+ }
+ }
+ else {
throw new CheckstyleException(
- "TreeWalker is not allowed as a parent of " + name
- + " Please review 'Parent Module' section for this Check in web"
- + " documentation if Check is standard.");
+ "TreeWalker is not allowed as a parent of " + name
+ + " Please review 'Parent Module' section for this Check in web"
+ + " documentation if Check is standard.");
}
- final AbstractCheck check = (AbstractCheck) module;
- check.contextualize(childContext);
- check.configure(childConf);
- check.init();
-
- registerCheck(check);
}
@Override
@@ -169,10 +190,11 @@ protected void processFiltered(File file, FileText fileText) throws CheckstyleEx
if (CommonUtils.matchesFileExtension(file, getFileExtensions())) {
final String msg = "%s occurred during the analysis of file %s.";
final String fileName = file.getPath();
+ final FileContents contents = new FileContents(fileText);
+
try {
if (!ordinaryChecks.isEmpty()
|| !commentChecks.isEmpty()) {
- final FileContents contents = new FileContents(fileText);
final DetailAST rootAST = parse(contents);
if (!ordinaryChecks.isEmpty()) {
@@ -180,10 +202,14 @@ protected void processFiltered(File file, FileText fileText) throws CheckstyleEx
}
if (!commentChecks.isEmpty()) {
final DetailAST astWithComments = appendHiddenCommentNodes(rootAST);
-
walk(astWithComments, contents, AstState.WITH_COMMENTS);
}
}
+
+ final SortedSet filteredMessages = getFilteredMessages(fileName,
+ contents);
+ addMessages(filteredMessages);
+ messages.clear();
}
catch (final TokenStreamRecognitionException tre) {
final String exceptionMsg = String.format(Locale.ROOT, msg,
@@ -198,6 +224,28 @@ protected void processFiltered(File file, FileText fileText) throws CheckstyleEx
}
}
+ /**
+ * Returns filtered set of {@link LocalizedMessage}.
+ * @param fileName path to the file
+ * @param fileContents the contents of the file
+ * @return filtered set of messages
+ */
+ private SortedSet getFilteredMessages(String fileName,
+ FileContents fileContents) {
+ final SortedSet result = new TreeSet<>(messages);
+ for (final LocalizedMessage element : messages) {
+ final TreeWalkerAuditEvent event = new TreeWalkerAuditEvent(fileContents, fileName,
+ element);
+ for (final AstFilter filter : filters) {
+ if (!filter.accept(event)) {
+ result.remove(element);
+ break;
+ }
+ }
+ }
+ return result;
+ }
+
/**
* Register a check for a given configuration.
* @param check the check to register
@@ -350,7 +398,7 @@ private void notifyEnd(DetailAST rootAST, AstState astState) {
for (AbstractCheck check : checks) {
check.finishTree(rootAST);
- addMessages(check.getMessages());
+ messages.addAll(check.getMessages());
}
}
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/api/AstFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/api/AstFilter.java
new file mode 100644
index 000000000000..44712bdc21bb
--- /dev/null
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/api/AstFilter.java
@@ -0,0 +1,34 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2017 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.api;
+
+/**
+ * An interface for filtering {@code TreeWalkerAuditEvent}.
+ *
+ * @author Timur Tibeyev.
+ */
+public interface AstFilter {
+ /**
+ * Determines whether or not a filtered {@code TreeWalkerAuditEvent} is accepted.
+ * @param auditEvent the AuditEvent to filter.
+ * @return true if the event is accepted.
+ */
+ boolean accept(TreeWalkerAuditEvent auditEvent);
+}
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/api/TreeWalkerAuditEvent.java b/src/main/java/com/puppycrawl/tools/checkstyle/api/TreeWalkerAuditEvent.java
new file mode 100644
index 000000000000..2181cb5c9bb5
--- /dev/null
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/api/TreeWalkerAuditEvent.java
@@ -0,0 +1,115 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2017 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.api;
+
+/**
+ * Raw {@code TreeWalker} event for audit.
+ *
+ * @author Timur Tibeyev
+ */
+public class TreeWalkerAuditEvent {
+ /** Filename event associated with. **/
+ private final String fileName;
+ /** The file contents. */
+ private final FileContents fileContents;
+ /** Message associated with the event. **/
+ private final LocalizedMessage localizedMessage;
+
+ /**
+ * Creates a new {@code AuditEvent} instance.
+ *
+ * @param fileContents contents of the file associated with the event
+ * @param fileName file associated with the event
+ * @param localizedMessage the actual message
+ */
+ public TreeWalkerAuditEvent(FileContents fileContents, String fileName,
+ LocalizedMessage localizedMessage) {
+ this.fileContents = fileContents;
+ this.fileName = fileName;
+ this.localizedMessage = localizedMessage;
+ }
+
+ /**
+ * Returns name of file being audited.
+ * @return the file name currently being audited or null if there is
+ * no relation to a file.
+ */
+ public String getFileName() {
+ return fileName;
+ }
+
+ /**
+ * Returns contents of the file.
+ * @return contents of the file.
+ */
+ public FileContents getFileContents() {
+ return fileContents;
+ }
+
+ /**
+ * Gets the localized message.
+ * @return the localized message
+ */
+ public LocalizedMessage getLocalizedMessage() {
+ return localizedMessage;
+ }
+
+ /**
+ * Return the line number on the source file where the event occurred.
+ * This may be 0 if there is no relation to a file content.
+ * @return an integer representing the line number in the file source code.
+ */
+ public int getLine() {
+ return localizedMessage.getLineNo();
+ }
+
+ /**
+ * Return the message associated to the event.
+ * @return the event message
+ */
+ public String getMessage() {
+ return localizedMessage.getMessage();
+ }
+
+ /**
+ * Gets the column associated with the message.
+ * @return the column associated with the message
+ */
+ public int getColumn() {
+ return localizedMessage.getColumnNo();
+ }
+
+ /**
+ * Returns id of module.
+ * @return the identifier of the module that generated the event. Can return
+ * null.
+ */
+ public String getModuleId() {
+ return localizedMessage.getModuleId();
+ }
+
+ /**
+ * Gets the name of the source for the message.
+ * @return the name of the source for the message
+ */
+ public String getSourceName() {
+ return localizedMessage.getSourceName();
+ }
+}
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilter.java
index fa1b1fdc4dda..1a9d5c960779 100644
--- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilter.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilter.java
@@ -28,12 +28,11 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
-import com.puppycrawl.tools.checkstyle.api.AuditEvent;
+import com.puppycrawl.tools.checkstyle.api.AstFilter;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.FileContents;
-import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.api.TextBlock;
-import com.puppycrawl.tools.checkstyle.checks.FileContentsHolder;
+import com.puppycrawl.tools.checkstyle.api.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
/**
@@ -72,7 +71,7 @@
*/
public class SuppressWithNearbyCommentFilter
extends AutomaticBean
- implements Filter {
+ implements AstFilter {
/** Format to turns checkstyle reporting off. */
private static final String DEFAULT_COMMENT_FORMAT =
@@ -183,13 +182,13 @@ public void setCheckC(boolean checkC) {
}
@Override
- public boolean accept(AuditEvent event) {
+ public boolean accept(TreeWalkerAuditEvent event) {
boolean accepted = true;
if (event.getLocalizedMessage() != null) {
// Lazy update. If the first event for the current file, update file
// contents and tag suppressions
- final FileContents currentContents = FileContentsHolder.getCurrentFileContents();
+ final FileContents currentContents = event.getFileContents();
if (getFileContents() != currentContents) {
setFileContents(currentContents);
@@ -207,7 +206,7 @@ public boolean accept(AuditEvent event) {
* @param event AuditEvent to test match on {@link #tags}.
* @return true if event matches any tag from {@link #tags}, false otherwise.
*/
- private boolean matchesTag(AuditEvent event) {
+ private boolean matchesTag(TreeWalkerAuditEvent event) {
boolean result = false;
for (final Tag tag : tags) {
if (tag.isMatch(event)) {
@@ -373,7 +372,7 @@ public int hashCode() {
* @param event the {@code AuditEvent} to check.
* @return true if the source of event matches the text of this tag.
*/
- public boolean isMatch(AuditEvent event) {
+ public boolean isMatch(TreeWalkerAuditEvent event) {
final int line = event.getLine();
boolean match = false;
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilter.java b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilter.java
index 50e0e542e4c9..07fce6d2239c 100644
--- a/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilter.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilter.java
@@ -29,12 +29,11 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
-import com.puppycrawl.tools.checkstyle.api.AuditEvent;
+import com.puppycrawl.tools.checkstyle.api.AstFilter;
import com.puppycrawl.tools.checkstyle.api.AutomaticBean;
import com.puppycrawl.tools.checkstyle.api.FileContents;
-import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.api.TextBlock;
-import com.puppycrawl.tools.checkstyle.checks.FileContentsHolder;
+import com.puppycrawl.tools.checkstyle.api.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
/**
@@ -58,11 +57,10 @@
*
* @author Mike McMahon
* @author Rick Giles
- * @see FileContentsHolder
*/
public class SuppressionCommentFilter
extends AutomaticBean
- implements Filter {
+ implements AstFilter {
/** Turns checkstyle reporting off. */
private static final String DEFAULT_OFF_FORMAT = "CHECKSTYLE:OFF";
@@ -172,13 +170,13 @@ public void setCheckC(boolean checkC) {
}
@Override
- public boolean accept(AuditEvent event) {
+ public boolean accept(TreeWalkerAuditEvent event) {
boolean accepted = true;
if (event.getLocalizedMessage() != null) {
// Lazy update. If the first event for the current file, update file
// contents and tag suppressions
- final FileContents currentContents = FileContentsHolder.getCurrentFileContents();
+ final FileContents currentContents = event.getFileContents();
if (getFileContents() != currentContents) {
setFileContents(currentContents);
@@ -196,7 +194,7 @@ public boolean accept(AuditEvent event) {
* @param event the {@code AuditEvent} to match.
* @return The {@code Tag} nearest event.
*/
- private Tag findNearestMatch(AuditEvent event) {
+ private Tag findNearestMatch(TreeWalkerAuditEvent event) {
Tag result = null;
for (Tag tag : tags) {
if (tag.getLine() > event.getLine()
@@ -430,7 +428,7 @@ public int hashCode() {
* @param event the {@code AuditEvent} to check.
* @return true if the source of event matches the text of this tag.
*/
- public boolean isMatch(AuditEvent event) {
+ public boolean isMatch(TreeWalkerAuditEvent event) {
boolean match = false;
final Matcher tagMatcher = tagCheckRegexp.matcher(event.getSourceName());
if (tagMatcher.find()) {
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilterTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilterTest.java
index fb78a4c8271d..2f37dfcadd77 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilterTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressWithNearbyCommentFilterTest.java
@@ -38,11 +38,11 @@
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.TreeWalker;
-import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
+import com.puppycrawl.tools.checkstyle.api.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.checks.FileContentsHolder;
import com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
@@ -227,7 +227,7 @@ public Checker createChecker(Configuration checkConfig)
checksConfig.addChild(createCheckConfig(IllegalCatchCheck.class));
checkerConfig.addChild(checksConfig);
if (checkConfig != null) {
- checkerConfig.addChild(checkConfig);
+ checksConfig.addChild(checkConfig);
}
final Checker checker = new Checker();
final Locale locale = Locale.ROOT;
@@ -301,7 +301,7 @@ public void testInvalidCheckFormat() throws Exception {
@Test
public void testAcceptNullLocalizedMessage() {
final SuppressWithNearbyCommentFilter filter = new SuppressWithNearbyCommentFilter();
- final AuditEvent auditEvent = new AuditEvent(this);
+ final TreeWalkerAuditEvent auditEvent = new TreeWalkerAuditEvent(null, null, null);
assertTrue(filter.accept(auditEvent));
}
@@ -352,22 +352,23 @@ public void testSuppressById() throws Exception {
public void testTagsAreClearedEachRun() {
final SuppressWithNearbyCommentFilter suppressionCommentFilter =
new SuppressWithNearbyCommentFilter();
- final AuditEvent dummyEvent = new AuditEvent(new Object(), "filename",
- new LocalizedMessage(1, null, null, null, null, Object.class, null));
-
final FileContentsHolder fileContentsHolder = new FileContentsHolder();
final FileContents contents =
new FileContents("filename", "//SUPPRESS CHECKSTYLE ignore", "line2");
contents.reportSingleLineComment(1, 0);
fileContentsHolder.setFileContents(contents);
fileContentsHolder.beginTree(null);
+ final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, "filename",
+ new LocalizedMessage(1, null, null, null, null, Object.class, null));
suppressionCommentFilter.accept(dummyEvent);
final FileContents contents2 =
new FileContents("filename2", "some line", "//SUPPRESS CHECKSTYLE ignore");
contents2.reportSingleLineComment(2, 0);
fileContentsHolder.setFileContents(contents2);
fileContentsHolder.beginTree(null);
- suppressionCommentFilter.accept(dummyEvent);
+ final TreeWalkerAuditEvent dummyEvent2 = new TreeWalkerAuditEvent(contents2, "filename",
+ new LocalizedMessage(1, null, null, null, null, Object.class, null));
+ suppressionCommentFilter.accept(dummyEvent2);
final List tags =
Whitebox.getInternalState(suppressionCommentFilter, "tags");
assertEquals(1, tags.size());
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilterTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilterTest.java
index 3d37f237d170..7e4550d05e4d 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilterTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/filters/SuppressionCommentFilterTest.java
@@ -39,11 +39,11 @@
import com.puppycrawl.tools.checkstyle.Checker;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.TreeWalker;
-import com.puppycrawl.tools.checkstyle.api.AuditEvent;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
+import com.puppycrawl.tools.checkstyle.api.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.checks.FileContentsHolder;
import com.puppycrawl.tools.checkstyle.checks.coding.IllegalCatchCheck;
import com.puppycrawl.tools.checkstyle.checks.naming.ConstantNameCheck;
@@ -228,7 +228,7 @@ public Checker createChecker(Configuration checkConfig)
checksConfig.addChild(createCheckConfig(IllegalCatchCheck.class));
checkerConfig.addChild(checksConfig);
if (checkConfig != null) {
- checkerConfig.addChild(checkConfig);
+ checksConfig.addChild(checkConfig);
}
final Checker checker = new Checker();
final Locale locale = Locale.ROOT;
@@ -299,8 +299,9 @@ public void testInvalidMessageFormat() throws Exception {
@Test
public void testAcceptNullLocalizedMessage() {
final SuppressionCommentFilter filter = new SuppressionCommentFilter();
- final AuditEvent auditEvent = new AuditEvent(this);
+ final TreeWalkerAuditEvent auditEvent = new TreeWalkerAuditEvent(null, null, null);
Assert.assertTrue(filter.accept(auditEvent));
+ Assert.assertNull(auditEvent.getFileName());
}
@Test
@@ -329,15 +330,14 @@ public void testSuppressById() throws Exception {
@Test
public void testFindNearestMatchDontAllowSameColumn() {
final SuppressionCommentFilter suppressionCommentFilter = new SuppressionCommentFilter();
- final AuditEvent dummyEvent = new AuditEvent(new Object(), "filename",
- new LocalizedMessage(1, null, null, null, null, Object.class, null));
-
final FileContentsHolder fileContentsHolder = new FileContentsHolder();
final FileContents contents =
new FileContents("filename", "//CHECKSTYLE:OFF: ConstantNameCheck", "line2");
contents.reportSingleLineComment(1, 0);
fileContentsHolder.setFileContents(contents);
fileContentsHolder.beginTree(null);
+ final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, "filename",
+ new LocalizedMessage(1, null, null, null, null, Object.class, null));
final boolean result = suppressionCommentFilter.accept(dummyEvent);
assertFalse(result);
}
@@ -345,22 +345,23 @@ public void testFindNearestMatchDontAllowSameColumn() {
@Test
public void testTagsAreClearedEachRun() {
final SuppressionCommentFilter suppressionCommentFilter = new SuppressionCommentFilter();
- final AuditEvent dummyEvent = new AuditEvent(new Object(), "filename",
- new LocalizedMessage(1, null, null, null, null, Object.class, null));
-
final FileContentsHolder fileContentsHolder = new FileContentsHolder();
final FileContents contents =
new FileContents("filename", "//CHECKSTYLE:OFF", "line2");
contents.reportSingleLineComment(1, 0);
fileContentsHolder.setFileContents(contents);
fileContentsHolder.beginTree(null);
+ final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, "filename",
+ new LocalizedMessage(1, null, null, null, null, Object.class, null));
suppressionCommentFilter.accept(dummyEvent);
final FileContents contents2 =
new FileContents("filename2", "some line", "//CHECKSTYLE:OFF");
contents2.reportSingleLineComment(2, 0);
fileContentsHolder.setFileContents(contents2);
fileContentsHolder.beginTree(null);
- suppressionCommentFilter.accept(dummyEvent);
+ final TreeWalkerAuditEvent dummyEvent2 = new TreeWalkerAuditEvent(contents2, "filename",
+ new LocalizedMessage(1, null, null, null, null, Object.class, null));
+ suppressionCommentFilter.accept(dummyEvent2);
final List tags =
Whitebox.getInternalState(suppressionCommentFilter, "tags");
assertEquals(1, tags.size());
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsPagesTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsPagesTest.java
index c4b3597eb51e..9f95b45b01b0 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsPagesTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsPagesTest.java
@@ -83,8 +83,6 @@ public class XdocsPagesTest {
"name=\"Translation\"",
"name=\"SeverityMatchFilter\"",
"name=\"SuppressionFilter\"",
- "name=\"SuppressionCommentFilter\"",
- "name=\"SuppressWithNearbyCommentFilter\"",
"name=\"SuppressWarningsFilter\"",
"name=\"BeforeExecutionExclusionFileFilter\"",
"name=\"RegexpHeader\"",
diff --git a/src/xdocs/config_filters.xml b/src/xdocs/config_filters.xml
index d55ffd704d1c..d8bd4c74702a 100644
--- a/src/xdocs/config_filters.xml
+++ b/src/xdocs/config_filters.xml
@@ -191,7 +191,7 @@
CHECKSTYLE:ON: