Remove useless violation factories

This commit is contained in:
Clément Fournier
2019-08-17 11:09:11 +02:00
parent eb88113ed1
commit b270076e13
17 changed files with 64 additions and 252 deletions

View File

@ -7,6 +7,8 @@ package net.sourceforge.pmd.lang.apex.rule;
import java.util.Collections;
import java.util.List;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Report.SuppressedViolation;
import net.sourceforge.pmd.Rule;
@ -26,8 +28,8 @@ public final class ApexRuleViolationFactory extends AbstractRuleViolationFactory
}
@Override
public Report.SuppressedViolation suppressOrNull(RuleViolation rv, Node node, Rule rule) {
if (isSuppressed(node, rule)) {
public Report.SuppressedViolation suppressOrNull(RuleViolation rv, @NonNull Node node) {
if (isSuppressed(node, rv.getRule())) {
return new SuppressedViolation(rv, this, null);
}
return null;

View File

@ -3,19 +3,25 @@ package net.sourceforge.pmd;
import java.util.Map;
import java.util.regex.Pattern;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.Report.SuppressedViolation;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
/**
* Generic rule violation suppressor.
* An object that suppresses rule violations. Suppressors are used by
* {@link RuleViolationFactory} to filter out violations. In PMD 6.0.x,
* the {@link Report} object filtered violations itself - but it has
* no knowledge of language-specific suppressors.
*/
public interface ViolationSuppressor {
String NOPMD_COMMENT_ID = "//NOPMD";
/**
* Suppressor for the violationSuppressRegex property.
*/
ViolationSuppressor REGEX_SUPPRESSOR = new ViolationSuppressor() {
@Override
public String id() {
@ -23,8 +29,8 @@ public interface ViolationSuppressor {
}
@Override
public @Nullable SuppressedViolation suppressOrNull(RuleViolation rv, Node node, Rule rule) {
String regex = rule.getProperty(Rule.VIOLATION_SUPPRESS_REGEX_DESCRIPTOR); // Regex
public @Nullable SuppressedViolation suppressOrNull(RuleViolation rv, @NonNull Node node) {
String regex = rv.getRule().getProperty(Rule.VIOLATION_SUPPRESS_REGEX_DESCRIPTOR); // Regex
if (regex != null && rv.getDescription() != null) {
if (Pattern.matches(regex, rv.getDescription())) {
return new SuppressedViolation(rv, this, regex);
@ -34,6 +40,9 @@ public interface ViolationSuppressor {
}
};
/**
* Suppressor for the violationSuppressXPath property.
*/
ViolationSuppressor XPATH_SUPPRESSOR = new ViolationSuppressor() {
@Override
public String id() {
@ -41,8 +50,8 @@ public interface ViolationSuppressor {
}
@Override
public @Nullable SuppressedViolation suppressOrNull(RuleViolation rv, Node node, Rule rule) {
String xpath = rule.getProperty(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR);
public @Nullable SuppressedViolation suppressOrNull(RuleViolation rv, @NonNull Node node) {
String xpath = rv.getRule().getProperty(Rule.VIOLATION_SUPPRESS_XPATH_DESCRIPTOR);
if (xpath != null && node.hasDescendantMatchingXPath(xpath)) {
return new SuppressedViolation(rv, this, xpath);
}
@ -50,14 +59,20 @@ public interface ViolationSuppressor {
}
};
/**
* Suppressor for regular NOPMD comments.
*
* @implNote This requires special support from the language, namely
* an implementation of {@link RootNode#getNoPmdComments()}.
*/
ViolationSuppressor NOPMD_COMMENT_SUPPRESSOR = new ViolationSuppressor() {
@Override
public String id() {
return NOPMD_COMMENT_ID;
return "//NOPMD";
}
@Override
public @Nullable SuppressedViolation suppressOrNull(RuleViolation rv, Node node, Rule rule) {
public @Nullable SuppressedViolation suppressOrNull(RuleViolation rv, @NonNull Node node) {
Map<Integer, String> noPmd = node.getRoot().getNoPmdComments();
if (noPmd.containsKey(rv.getBeginLine())) {
return new SuppressedViolation(rv, this, noPmd.get(rv.getBeginLine()));
@ -66,10 +81,20 @@ public interface ViolationSuppressor {
}
};
/**
* A name, for reporting and documentation purposes.
*/
String id();
/**
* Returns a {@link SuppressedViolation} if the given violation is
* suppressed by this object. The node and the rule are provided
* for context.
*/
@Nullable
SuppressedViolation suppressOrNull(RuleViolation rv, Node node, Rule rule);
SuppressedViolation suppressOrNull(RuleViolation rv, @NonNull Node node);
}

View File

@ -10,6 +10,7 @@ import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.Report.SuppressedViolation;
import net.sourceforge.pmd.Rule;
@ -18,6 +19,15 @@ import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.ViolationSuppressor;
import net.sourceforge.pmd.lang.ast.Node;
/**
* This is a functional implementation of {@link RuleViolationFactory}.
* It uses only the standard {@link ViolationSuppressor}s (constants in the interface).
* It may be extended to add more suppression options.
*
* <p>Implementations should be internal. Only the interface should be exposed.
*
* TODO this should not be an abstract class anymore
*/
public abstract class AbstractRuleViolationFactory implements RuleViolationFactory {
private static final Object[] NO_ARGS = new Object[0];
@ -42,7 +52,7 @@ public abstract class AbstractRuleViolationFactory implements RuleViolationFacto
String formattedMessage = cleanup(message, args);
RuleViolation rv = createRuleViolation(rule, ruleContext, node, formattedMessage);
maybeSuppress(ruleContext, node, rv, rule);
maybeSuppress(ruleContext, node, rv);
}
@Override
@ -51,10 +61,10 @@ public abstract class AbstractRuleViolationFactory implements RuleViolationFacto
String formattedMessage = cleanup(message, args);
RuleViolation rv = createRuleViolation(rule, ruleContext, node, formattedMessage, beginLine, endLine);
maybeSuppress(ruleContext, node, rv, rule);
maybeSuppress(ruleContext, node, rv);
}
private void maybeSuppress(RuleContext ruleContext, Node node, RuleViolation rv, Rule rule) {
private void maybeSuppress(RuleContext ruleContext, @Nullable Node node, RuleViolation rv) {
List<ViolationSuppressor> suppressors = new ArrayList<>(getSuppressors());
suppressors.add(ViolationSuppressor.NOPMD_COMMENT_SUPPRESSOR);
suppressors.add(ViolationSuppressor.REGEX_SUPPRESSOR);
@ -62,7 +72,7 @@ public abstract class AbstractRuleViolationFactory implements RuleViolationFacto
if (node != null) {
for (ViolationSuppressor suppressor : suppressors) {
SuppressedViolation suppressed = suppressor.suppressOrNull(rv, node, rule);
SuppressedViolation suppressed = suppressor.suppressOrNull(rv, node);
if (suppressed != null) {
ruleContext.getReport().addSuppressedViolation(suppressed);
return;

View File

@ -4,6 +4,8 @@
package net.sourceforge.pmd.lang.rule;
import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.ast.Node;
@ -27,8 +29,8 @@ public interface RuleViolationFactory {
* @param args
* arguments to embed in the rule violation message
*/
void addViolation(RuleContext ruleContext, Rule rule, Node node, String message, Object[] args);
void addViolation(RuleContext ruleContext, Rule rule, @Nullable Node node, String message, Object[] args);
void addViolation(RuleContext ruleContext, Rule rule, Node node, String message, int beginLine, int endLine,
Object[] args);
void addViolation(RuleContext ruleContext, Rule rule, @Nullable Node node, String message, int beginLine, int endLine, Object[] args);
}

View File

@ -10,7 +10,6 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.ast.Node;
@ -19,21 +18,6 @@ public class AbstractRuleViolationFactoryTest {
private RuleContext ruleContext;
private RuleViolationFactory factory;
private static class TestRuleViolationFactory extends AbstractRuleViolationFactory {
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
return new ParametricRuleViolation<>(rule, ruleContext, node, message);
}
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message,
int beginLine, int endLine) {
ParametricRuleViolation<Node> violation = new ParametricRuleViolation<>(rule, ruleContext, node, message);
violation.setLines(beginLine, endLine);
return violation;
}
}
private static class TestRule extends AbstractRule {
@Override
public void apply(List<? extends Node> nodes, RuleContext ctx) {
@ -44,7 +28,7 @@ public class AbstractRuleViolationFactoryTest {
@Before
public void setup() {
ruleContext = new RuleContext();
factory = new TestRuleViolationFactory();
factory = new AbstractRuleViolationFactory() {};
}
@Test

View File

@ -7,6 +7,8 @@ package net.sourceforge.pmd.lang.java.rule;
import java.util.Collections;
import java.util.List;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Report.SuppressedViolation;
import net.sourceforge.pmd.Rule;
@ -28,8 +30,8 @@ public final class JavaRuleViolationFactory extends AbstractRuleViolationFactory
}
@Override
public Report.SuppressedViolation suppressOrNull(RuleViolation rv, Node node, Rule rule) {
if (AnnotationSuppressionUtil.contextSuppresses(node, rule)) {
public Report.SuppressedViolation suppressOrNull(RuleViolation rv, @NonNull Node node) {
if (AnnotationSuppressionUtil.contextSuppresses(node, rv.getRule())) {
return new SuppressedViolation(rv, this, null);
}
return null;

View File

@ -9,8 +9,6 @@ import net.sourceforge.pmd.lang.Parser;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.XPathHandler;
import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler;
import net.sourceforge.pmd.lang.jsp.rule.JspRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
/**
* Implementation of LanguageVersionHandler for the JSP parser.
@ -24,10 +22,6 @@ public class JspHandler extends AbstractPmdLanguageVersionHandler {
return new DefaultASTXPathHandler();
}
@Override
public RuleViolationFactory getRuleViolationFactory() {
return JspRuleViolationFactory.INSTANCE;
}
@Override
public Parser getParser(ParserOptions parserOptions) {

View File

@ -1,33 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.jsp.rule;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.jsp.ast.JspNode;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
public final class JspRuleViolationFactory extends AbstractRuleViolationFactory {
public static final RuleViolationFactory INSTANCE = new JspRuleViolationFactory();
private JspRuleViolationFactory() {
}
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
return new ParametricRuleViolation<>(rule, ruleContext, (JspNode) node, message);
}
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message,
int beginLine, int endLine) {
return null; // FIXME
}
}

View File

@ -16,9 +16,7 @@ import net.sourceforge.pmd.lang.dfa.DFAGraphRule;
import net.sourceforge.pmd.lang.plsql.ast.ASTInput;
import net.sourceforge.pmd.lang.plsql.dfa.DFAPLSQLGraphRule;
import net.sourceforge.pmd.lang.plsql.dfa.DataFlowFacade;
import net.sourceforge.pmd.lang.plsql.rule.PLSQLRuleViolationFactory;
import net.sourceforge.pmd.lang.plsql.symboltable.SymbolFacade;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
/**
* Implementation of LanguageVersionHandler for the PLSQL AST. It uses anonymous
@ -38,11 +36,6 @@ public class PLSQLHandler extends AbstractPmdLanguageVersionHandler {
return new PLSQLParser(parserOptions);
}
@Override
public RuleViolationFactory getRuleViolationFactory() {
return PLSQLRuleViolationFactory.INSTANCE;
}
@Override
public DFAGraphRule getDFAGraphRule() {
return new DFAPLSQLGraphRule();

View File

@ -1,34 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.plsql.rule;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
public final class PLSQLRuleViolationFactory extends AbstractRuleViolationFactory {
public static final RuleViolationFactory INSTANCE = new PLSQLRuleViolationFactory();
private PLSQLRuleViolationFactory() {
}
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
return new ParametricRuleViolation<>(rule, ruleContext, node, message);
}
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message,
int beginLine, int endLine) {
ParametricRuleViolation<Node> violation = new ParametricRuleViolation<>(rule, ruleContext, node, message);
violation.setLines(beginLine, endLine);
return violation;
}
}

View File

@ -8,9 +8,7 @@ import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler;
import net.sourceforge.pmd.lang.Parser;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.XPathHandler;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrRuleViolationFactory;
import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
public class SwiftHandler extends AbstractPmdLanguageVersionHandler {
@ -19,11 +17,6 @@ public class SwiftHandler extends AbstractPmdLanguageVersionHandler {
return new DefaultASTXPathHandler();
}
@Override
public RuleViolationFactory getRuleViolationFactory() {
return AntlrRuleViolationFactory.INSTANCE;
}
@Override
public Parser getParser(final ParserOptions parserOptions) {
return new SwiftParserAdapter(parserOptions);

View File

@ -9,8 +9,6 @@ import net.sourceforge.pmd.lang.Parser;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.XPathHandler;
import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.vf.rule.VfRuleViolationFactory;
public class VfHandler extends AbstractPmdLanguageVersionHandler {
@ -19,11 +17,6 @@ public class VfHandler extends AbstractPmdLanguageVersionHandler {
return new DefaultASTXPathHandler();
}
@Override
public RuleViolationFactory getRuleViolationFactory() {
return VfRuleViolationFactory.INSTANCE;
}
@Override
public Parser getParser(ParserOptions parserOptions) {
return new VfParser(parserOptions);

View File

@ -1,36 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.vf.rule;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.vf.ast.VfNode;
public final class VfRuleViolationFactory extends AbstractRuleViolationFactory {
public static final RuleViolationFactory INSTANCE = new VfRuleViolationFactory();
private VfRuleViolationFactory() {
}
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
return new ParametricRuleViolation<>(rule, ruleContext, (VfNode) node, message);
}
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message,
int beginLine, int endLine) {
ParametricRuleViolation<VfNode> rViolation = new ParametricRuleViolation<>(rule, ruleContext, (VfNode) node, message);
rViolation.setLines(beginLine, endLine);
return rViolation;
}
}

View File

@ -9,8 +9,6 @@ import net.sourceforge.pmd.lang.Parser;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.XPathHandler;
import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.vm.rule.VmRuleViolationFactory;
/**
* Implementation of LanguageVersionHandler for the VM parser.
@ -23,10 +21,6 @@ public class VmHandler extends AbstractPmdLanguageVersionHandler {
return new DefaultASTXPathHandler();
}
@Override
public RuleViolationFactory getRuleViolationFactory() {
return VmRuleViolationFactory.INSTANCE;
}
@Override
public Parser getParser(final ParserOptions parserOptions) {

View File

@ -1,37 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.vm.rule;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.vm.ast.AbstractVmNode;
public final class VmRuleViolationFactory extends AbstractRuleViolationFactory {
public static final RuleViolationFactory INSTANCE = new VmRuleViolationFactory();
private VmRuleViolationFactory() {
}
@Override
protected RuleViolation createRuleViolation(final Rule rule, final RuleContext ruleContext, final Node node,
final String message) {
return new ParametricRuleViolation<>(rule, ruleContext, (AbstractVmNode) node, message);
}
@Override
protected RuleViolation createRuleViolation(final Rule rule, final RuleContext ruleContext, final Node node,
final String message, final int beginLine, final int endLine) {
final ParametricRuleViolation<AbstractVmNode> violation = new ParametricRuleViolation<>(rule, ruleContext,
(AbstractVmNode) node, message);
violation.setLines(beginLine, endLine);
return violation;
}
}

View File

@ -9,8 +9,6 @@ import net.sourceforge.pmd.lang.Parser;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.XPathHandler;
import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.xml.rule.XmlRuleViolationFactory;
/**
* Implementation of LanguageVersionHandler for the XML.
@ -22,11 +20,6 @@ public class XmlHandler extends AbstractPmdLanguageVersionHandler {
return new DefaultASTXPathHandler();
}
@Override
public RuleViolationFactory getRuleViolationFactory() {
return XmlRuleViolationFactory.INSTANCE;
}
@Override
public ParserOptions getDefaultParserOptions() {
return new XmlParserOptions();

View File

@ -1,33 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.xml.rule;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.xml.ast.XmlNode;
public final class XmlRuleViolationFactory extends AbstractRuleViolationFactory {
public static final RuleViolationFactory INSTANCE = new XmlRuleViolationFactory();
private XmlRuleViolationFactory() {
}
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
return new ParametricRuleViolation<>(rule, ruleContext, (XmlNode) node, message);
}
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message,
int beginLine, int endLine) {
return null; // FIXME
}
}