Remove useless violation factories

This commit is contained in:
Clément Fournier
2019-10-05 15:26:29 +02:00
parent 9431ce4d9f
commit d0a3174f20
16 changed files with 41 additions and 123 deletions

View File

@ -16,9 +16,9 @@ import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.ViolationSuppressor;
import net.sourceforge.pmd.lang.apex.ast.CanSuppressWarnings;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.DefaultRuleViolationFactory;
public final class ApexRuleViolationFactory extends AbstractRuleViolationFactory {
public final class ApexRuleViolationFactory extends DefaultRuleViolationFactory {
public static final ApexRuleViolationFactory INSTANCE = new ApexRuleViolationFactory();
private static final ViolationSuppressor APEX_ANNOT_SUPPRESSOR = new ViolationSuppressor() {

View File

@ -9,9 +9,10 @@ import java.util.List;
import net.sourceforge.pmd.annotation.Experimental;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.lang.ast.AstProcessingStage;
import net.sourceforge.pmd.lang.ast.xpath.DefaultASTXPathHandler;
import net.sourceforge.pmd.lang.dfa.DFAGraphRule;
import net.sourceforge.pmd.lang.metrics.LanguageMetricsProvider;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.DefaultRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
@ -31,7 +32,9 @@ public interface LanguageVersionHandler {
/**
* Get the XPathHandler.
*/
XPathHandler getXPathHandler();
default XPathHandler getXPathHandler() {
return new DefaultASTXPathHandler();
}
/**
@ -63,7 +66,7 @@ public interface LanguageVersionHandler {
* Get the RuleViolationFactory.
*/
default RuleViolationFactory getRuleViolationFactory() {
return new AbstractRuleViolationFactory() {};
return DefaultRuleViolationFactory.defaultInstance();
}

View File

@ -26,13 +26,13 @@ import net.sourceforge.pmd.lang.ast.Node;
* 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 {
public class DefaultRuleViolationFactory implements RuleViolationFactory {
private static final Object[] NO_ARGS = new Object[0];
private static final DefaultRuleViolationFactory DEFAULT = new DefaultRuleViolationFactory();
private String cleanup(String message, Object[] args) {
if (message != null) {
@ -45,7 +45,6 @@ public abstract class AbstractRuleViolationFactory implements RuleViolationFacto
}
}
// TODO why do we need those two overloads??
@Override
public void addViolation(RuleContext ruleContext, Rule rule, Node node, String message, Object[] args) {
@ -101,4 +100,9 @@ public abstract class AbstractRuleViolationFactory implements RuleViolationFacto
rv.setLines(beginLine, endLine);
return rv;
}
/** Returns the default instance (no additional suppressors, creates a ParametricRuleViolation). */
public static RuleViolationFactory defaultInstance() {
return DEFAULT;
}
}

View File

@ -20,9 +20,9 @@ import net.sourceforge.pmd.lang.DummyLanguageModule;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.ast.DummyNode;
import net.sourceforge.pmd.lang.ast.DummyRoot;
import net.sourceforge.pmd.lang.ast.DummyRuleViolationFactory;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.AbstractRule;
import net.sourceforge.pmd.lang.rule.DefaultRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
@ -128,7 +128,7 @@ public class AbstractRuleTest {
ctx.setSourceCodeFile(new File("filename"));
DummyRoot n = new DummyRoot(m);
n.setCoords(5, 1, 6, 0);
DummyRuleViolationFactory.INSTANCE.addViolation(ctx, r, n, "specificdescription", new Object[0]);
DefaultRuleViolationFactory.defaultInstance().addViolation(ctx, r, n, "specificdescription", new Object[0]);
assertTrue(ctx.getReport().isEmpty());
}

View File

@ -19,7 +19,7 @@ import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.ParseException;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.rule.AbstractRuleChainVisitor;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.DefaultRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
/**
@ -93,7 +93,7 @@ public class DummyLanguageModule extends BaseLanguageModule {
}
}
public static class RuleViolationFactory extends AbstractRuleViolationFactory {
public static class RuleViolationFactory extends DefaultRuleViolationFactory {
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
return createRuleViolation(rule, ruleContext, node, message, 0, 0);
@ -103,6 +103,7 @@ public class DummyLanguageModule extends BaseLanguageModule {
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message,
int beginLine, int endLine) {
ParametricRuleViolation<Node> rv = new ParametricRuleViolation<Node>(rule, ruleContext, node, message) {
@Override
public String getPackageName() {
this.packageName = "foo"; // just for testing variable expansion
return super.getPackageName();

View File

@ -1,12 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ast;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
public class DummyRuleViolationFactory extends AbstractRuleViolationFactory {
public static final DummyRuleViolationFactory INSTANCE = new DummyRuleViolationFactory();
}

View File

@ -14,9 +14,9 @@ import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.ast.Node;
public class AbstractRuleViolationFactoryTest {
public class DefaultRuleViolationFactoryTest {
private RuleContext ruleContext;
private RuleViolationFactory factory;
private RuleViolationFactory factory = DefaultRuleViolationFactory.defaultInstance();
private static class TestRule extends AbstractRule {
@Override
@ -28,9 +28,8 @@ public class AbstractRuleViolationFactoryTest {
@Before
public void setup() {
ruleContext = new RuleContext();
factory = new AbstractRuleViolationFactory() {};
}
@Test
public void testMessage() {
factory.addViolation(ruleContext, new TestRule(), null, "message with \"'{'\"", null);

View File

@ -19,7 +19,7 @@ import net.sourceforge.pmd.Report.ProcessingError;
import net.sourceforge.pmd.ReportTest;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.ast.DummyRoot;
import net.sourceforge.pmd.lang.ast.DummyRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.DefaultRuleViolationFactory;
public class SummaryHTMLRendererTest extends AbstractRendererTst {
@ -93,7 +93,7 @@ public class SummaryHTMLRendererTest extends AbstractRendererTst {
+ "<td>file</td>" + PMD.EOL + "<td><pre>" + error.getDetail() + "</pre></td>" + PMD.EOL + "</tr>" + PMD.EOL
+ "</table></tr></table></body></html>" + PMD.EOL;
}
@Override
public String getExpectedError(ConfigurationError error) {
return "<html><head><title>PMD</title></head><body>" + PMD.EOL + "<center><h2>Summary</h2></center>" + PMD.EOL
@ -146,7 +146,7 @@ public class SummaryHTMLRendererTest extends AbstractRendererTst {
RuleContext ctx = new RuleContext();
DummyRoot root = new DummyRoot(suppressions);
root.setCoords(1, 10, 4, 5);
DummyRuleViolationFactory.INSTANCE.addViolation(ctx, new FooRule(), root, "suppress test", 1, 1, new Object[0]);
DefaultRuleViolationFactory.defaultInstance().addViolation(ctx, new FooRule(), root, "suppress test", 1, 1, new Object[0]);
return ctx.getReport();
}
}

View File

@ -17,10 +17,10 @@ import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.ViolationSuppressor;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.DefaultRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
public final class JavaRuleViolationFactory extends AbstractRuleViolationFactory {
public final class JavaRuleViolationFactory extends DefaultRuleViolationFactory {
public static final RuleViolationFactory INSTANCE = new JavaRuleViolationFactory();
private static final ViolationSuppressor JAVA_ANNOT_SUPPRESSOR = new ViolationSuppressor() {

View File

@ -7,26 +7,12 @@ package net.sourceforge.pmd.lang.ecmascript;
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.xpath.DefaultASTXPathHandler;
import net.sourceforge.pmd.lang.ecmascript.rule.EcmascriptRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
/**
* Implementation of LanguageVersionHandler for the ECMAScript Version 3.
*/
public class Ecmascript3Handler extends AbstractPmdLanguageVersionHandler {
@Override
public XPathHandler getXPathHandler() {
return new DefaultASTXPathHandler();
}
@Override
public RuleViolationFactory getRuleViolationFactory() {
return EcmascriptRuleViolationFactory.INSTANCE;
}
@Override
public ParserOptions getDefaultParserOptions() {
return new EcmascriptParserOptions();

View File

@ -5,6 +5,7 @@
package net.sourceforge.pmd.lang.ecmascript;
import net.sourceforge.pmd.lang.BaseLanguageModule;
import net.sourceforge.pmd.lang.LanguageVersionHandler;
import net.sourceforge.pmd.lang.ecmascript.rule.EcmascriptRuleChainVisitor;
/**
@ -12,12 +13,17 @@ import net.sourceforge.pmd.lang.ecmascript.rule.EcmascriptRuleChainVisitor;
*/
public class EcmascriptLanguageModule extends BaseLanguageModule {
private static final Ecmascript3Handler DEFAULT = new Ecmascript3Handler();
public static final String NAME = "Ecmascript";
public static final String TERSE_NAME = "ecmascript";
public EcmascriptLanguageModule() {
super(NAME, null, TERSE_NAME, EcmascriptRuleChainVisitor.class, "js");
addVersion("3", new Ecmascript3Handler(), true);
addVersion("3", DEFAULT, true);
}
public static LanguageVersionHandler defaultHandler() {
return DEFAULT;
}
}

View File

@ -1,15 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ecmascript.rule;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
public final class EcmascriptRuleViolationFactory extends AbstractRuleViolationFactory {
public static final EcmascriptRuleViolationFactory INSTANCE = new EcmascriptRuleViolationFactory();
private EcmascriptRuleViolationFactory() {
}
}

View File

@ -13,7 +13,6 @@ import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.ecmascript.EcmascriptLanguageModule;
import net.sourceforge.pmd.lang.ecmascript.ast.ASTFunctionNode;
import net.sourceforge.pmd.lang.ecmascript.rule.AbstractEcmascriptRule;
import net.sourceforge.pmd.lang.ecmascript.rule.EcmascriptRuleViolationFactory;
import net.sourceforge.pmd.testframework.RuleTst;
public class ReportTest extends RuleTst {
@ -24,7 +23,7 @@ public class ReportTest extends RuleTst {
Rule rule = new AbstractEcmascriptRule() {
@Override
public Object visit(ASTFunctionNode node, Object data) {
EcmascriptRuleViolationFactory.INSTANCE.addViolation((RuleContext) data, this, node, "Test", null);
EcmascriptLanguageModule.defaultHandler().getRuleViolationFactory().addViolation((RuleContext) data, this, node, "Test", null);
return super.visit(node, data);
}
};

View File

@ -6,8 +6,6 @@ package net.sourceforge.pmd.lang.scala;
import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler;
import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.scala.rule.ScalaRuleViolationFactory;
import scala.meta.Dialect;
@ -20,7 +18,7 @@ public class ScalaLanguageHandler extends AbstractPmdLanguageVersionHandler {
/**
* Create the Language Handler using the given Scala Dialect.
*
*
* @param scalaDialect
* the language version to use while parsing etc
*/
@ -30,17 +28,13 @@ public class ScalaLanguageHandler extends AbstractPmdLanguageVersionHandler {
/**
* Get the Scala Dialect used in this language version choice.
*
*
* @return the Scala Dialect for this handler
*/
public Dialect getDialect() {
return this.dialect;
}
@Override
public RuleViolationFactory getRuleViolationFactory() {
return ScalaRuleViolationFactory.INSTANCE;
}
@Override
public ScalaParser getParser(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.scala.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;
/**
* A RuleViolationFactory for Scala.
*/
public class ScalaRuleViolationFactory extends AbstractRuleViolationFactory {
/**
* The shared singleton of this RuleViolationFactory.
*/
public static final RuleViolationFactory INSTANCE = new ScalaRuleViolationFactory();
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
return new ParametricRuleViolation<Node>(rule, ruleContext, node, message);
}
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message,
int beginLine, int endLine) {
ParametricRuleViolation<Node> rv = new ParametricRuleViolation<>(rule, ruleContext, node, message);
rv.setLines(beginLine, endLine);
return rv;
}
}

View File

@ -7,9 +7,7 @@ package net.sourceforge.pmd.test.lang;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
@ -24,7 +22,7 @@ import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.ParseException;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.rule.AbstractRuleChainVisitor;
import net.sourceforge.pmd.lang.rule.AbstractRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.DefaultRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.test.lang.ast.DummyNode;
@ -94,15 +92,7 @@ public class DummyLanguageModule extends BaseLanguageModule {
}
}
private static class DummyRootNode extends DummyNode implements RootNode {
DummyRootNode(int id) {
super(id);
}
}
public static class RuleViolationFactory extends AbstractRuleViolationFactory {
public static class RuleViolationFactory extends DefaultRuleViolationFactory {
@Override
protected RuleViolation createRuleViolation(Rule rule, RuleContext ruleContext, Node node, String message) {
return createRuleViolation(rule, ruleContext, node, message, 0, 0);