Skip to content

Commit

Permalink
Issue checkstyle#4421: Support suppression-xpath element in Suppressi…
Browse files Browse the repository at this point in the history
…onLoader
  • Loading branch information
timurt committed Sep 4, 2017
1 parent 546bae1 commit fe333d0
Show file tree
Hide file tree
Showing 20 changed files with 891 additions and 91 deletions.
3 changes: 3 additions & 0 deletions config/checkstyle_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@
<module name="VariableDeclarationUsageDistance"/>

<!-- Filters-->
<module name="SuppressionXpathFilter">
<property name="file" value="${checkstyle.suppressions.file}"/>
</module>
<module name="SuppressionCommentFilter">
<!--
Use suppressions.xml for suppressions, this is only example.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,8 @@ private static void fillModulesFromFiltersPackage() {
BASE_PACKAGE + ".filters.SuppressionCommentFilter");
NAME_TO_FULL_MODULE_NAME.put("SuppressionFilter",
BASE_PACKAGE + ".filters.SuppressionFilter");
NAME_TO_FULL_MODULE_NAME.put("SuppressionXpathFilter",
BASE_PACKAGE + ".filters.SuppressionXpathFilter");
NAME_TO_FULL_MODULE_NAME.put("SuppressWarningsFilter",
BASE_PACKAGE + ".filters.SuppressWarningsFilter");
NAME_TO_FULL_MODULE_NAME.put("SuppressWithNearbyCommentFilter",
Expand Down
30 changes: 26 additions & 4 deletions src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,21 +523,43 @@ public void destroy() {

@Override
public Set<String> getExternalResourceLocations() {
final Set<String> ordinaryChecksResources = getExternalResourceLocations(ordinaryChecks);
final Set<String> commentChecksResources = getExternalResourceLocations(commentChecks);
final int resultListSize = commentChecksResources.size() + ordinaryChecksResources.size();
final Set<String> ordinaryChecksResources =
getExternalResourceLocationsOfChecks(ordinaryChecks);
final Set<String> commentChecksResources =
getExternalResourceLocationsOfChecks(commentChecks);
final Set<String> filtersResources =
getExternalResourceLocationsOfFilters();
final int resultListSize = commentChecksResources.size()
+ ordinaryChecksResources.size()
+ filtersResources.size();
final Set<String> resourceLocations = new HashSet<>(resultListSize);
resourceLocations.addAll(ordinaryChecksResources);
resourceLocations.addAll(commentChecksResources);
resourceLocations.addAll(filtersResources);
return resourceLocations;
}

/**
* Returns a set of external configuation resource locations which are used by the filters set.
* @return a set of external configration resource locations which are used by the filters set.
*/
private Set<String> getExternalResourceLocationsOfFilters() {
final Set<String> externalConfigurationResources = new HashSet<>();
filters.stream().filter(filter -> filter instanceof ExternalResourceHolder)
.forEach(filter -> {
final Set<String> checkExternalResources =
((ExternalResourceHolder) filter).getExternalResourceLocations();
externalConfigurationResources.addAll(checkExternalResources);
});
return externalConfigurationResources;
}

/**
* Returns a set of external configuration resource locations which are used by the checks set.
* @param checks a set of checks.
* @return a set of external configuration resource locations which are used by the checks set.
*/
private static Set<String> getExternalResourceLocations(Set<AbstractCheck> checks) {
private static Set<String> getExternalResourceLocationsOfChecks(Set<AbstractCheck> checks) {
final Set<String> externalConfigurationResources = new HashSet<>();
checks.stream().filter(check -> check instanceof ExternalResourceHolder).forEach(check -> {
final Set<String> checkExternalResources =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@

package com.puppycrawl.tools.checkstyle.filters;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
Expand All @@ -33,7 +29,7 @@
import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder;
import com.puppycrawl.tools.checkstyle.api.Filter;
import com.puppycrawl.tools.checkstyle.api.FilterSet;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
import com.puppycrawl.tools.checkstyle.utils.FilterUtils;

/**
* <p>
Expand Down Expand Up @@ -95,7 +91,7 @@ public int hashCode() {
protected void finishLocalSetup() throws CheckstyleException {
if (file != null) {
if (optional) {
if (suppressionSourceExists(file)) {
if (FilterUtils.isFileExists(file)) {
filters = SuppressionsLoader.loadSuppressions(file);
}
else {
Expand All @@ -112,33 +108,4 @@ protected void finishLocalSetup() throws CheckstyleException {
public Set<String> getExternalResourceLocations() {
return Collections.singleton(file);
}

/**
* Checks if suppression source with given fileName exists.
* @param fileName name of the suppressions file.
* @return true if suppression file exists, otherwise false
*/
private static boolean suppressionSourceExists(String fileName) {
boolean suppressionSourceExists = true;
InputStream sourceInput = null;
try {
final URI uriByFilename = CommonUtils.getUriByFilename(fileName);
final URL url = uriByFilename.toURL();
sourceInput = url.openStream();
}
catch (CheckstyleException | IOException ignored) {
suppressionSourceExists = false;
}
finally {
if (sourceInput != null) {
try {
sourceInput.close();
}
catch (IOException ignored) {
suppressionSourceExists = false;
}
}
}
return suppressionSourceExists;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
////////////////////////////////////////////////////////////////////////////////
// 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.filters;

import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

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;
import com.puppycrawl.tools.checkstyle.api.ExternalResourceHolder;
import com.puppycrawl.tools.checkstyle.utils.FilterUtils;

/**
* This filter accepts TreeWalkerAuditEvents according to file, check and xpath query,
* as specified in a suppression file.
*
* @author Timur Tibeyev.
* @noinspection NonFinalFieldReferenceInEquals, NonFinalFieldReferencedInHashCode
*/
public class SuppressionXpathFilter extends AutomaticBean implements
TreeWalkerFilter, ExternalResourceHolder {

/** Filename of supression file. */
private String file;
/** Tells whether config file existence is optional. */
private boolean optional;
/** Set of individual xpath suppresses. */
private Set<TreeWalkerFilter> filters = new HashSet<>();

/**
* Sets name of the supression file.
* @param fileName name of the suppressions file.
*/
public void setFile(String fileName) {
file = fileName;
}

/**
* Sets whether config file existence is optional.
* @param optional tells if config file existence is optional.
*/
public void setOptional(boolean optional) {
this.optional = optional;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final SuppressionXpathFilter suppressionXpathFilter = (SuppressionXpathFilter) obj;
return Objects.equals(filters, suppressionXpathFilter.filters);
}

@Override
public int hashCode() {
return Objects.hash(filters);
}

@Override
public boolean accept(TreeWalkerAuditEvent treeWalkerAuditEvent) {
boolean result = true;
for (TreeWalkerFilter filter : filters) {
if (!filter.accept(treeWalkerAuditEvent)) {
result = false;
break;
}
}
return result;
}

@Override
public Set<String> getExternalResourceLocations() {
return Collections.singleton(file);
}

@Override
protected void finishLocalSetup() throws CheckstyleException {
if (file != null) {
if (optional) {
if (FilterUtils.isFileExists(file)) {
filters = SuppressionsLoader.loadXpathSuppressions(file);
}
else {
filters = new HashSet<>();
}
}
else {
filters = SuppressionsLoader.loadXpathSuppressions(file);
}
}
}
}
Loading

0 comments on commit fe333d0

Please sign in to comment.