Remove RuleViolationFactory

This commit is contained in:
Clément Fournier
2022-04-09 17:09:24 +02:00
parent ebbb6b8770
commit 40824e4fc9
18 changed files with 78 additions and 391 deletions

View File

@ -5,6 +5,7 @@
package net.sourceforge.pmd.lang.apex;
import net.sourceforge.pmd.lang.BaseLanguageModule;
import net.sourceforge.pmd.lang.apex.internal.ApexHandler;
import net.sourceforge.pmd.util.CollectionUtil;
import apex.jorje.services.Version;

View File

@ -1,35 +1,32 @@
/**
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.apex;
package net.sourceforge.pmd.lang.apex.internal;
import static net.sourceforge.pmd.util.CollectionUtil.setOf;
import java.util.List;
import java.util.Set;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.ViolationSuppressor;
import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler;
import net.sourceforge.pmd.lang.apex.ApexLanguageModule;
import net.sourceforge.pmd.lang.apex.ast.ApexParser;
import net.sourceforge.pmd.lang.apex.internal.ApexDesignerBindings;
import net.sourceforge.pmd.lang.apex.metrics.ApexMetrics;
import net.sourceforge.pmd.lang.apex.rule.internal.ApexRuleViolationFactory;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.metrics.LanguageMetricsProvider;
import net.sourceforge.pmd.lang.metrics.Metric;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.properties.PropertySource;
import net.sourceforge.pmd.util.designerbindings.DesignerBindings;
@InternalApi
public class ApexHandler extends AbstractPmdLanguageVersionHandler {
private final ApexMetricsProvider myMetricsProvider = new ApexMetricsProvider();
@Override
public RuleViolationFactory getRuleViolationFactory() {
return ApexRuleViolationFactory.INSTANCE;
public List<ViolationSuppressor> getExtraViolationSuppressor() {
return ApexViolationSuppressors.ALL_APEX_SUPPRESSORS;
}
@Override

View File

@ -2,10 +2,11 @@
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.apex.rule.internal;
package net.sourceforge.pmd.lang.apex.internal;
import static net.sourceforge.pmd.util.CollectionUtil.listOf;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
@ -29,11 +30,9 @@ import net.sourceforge.pmd.lang.apex.ast.ASTUserEnum;
import net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclarationStatements;
import net.sourceforge.pmd.lang.apex.ast.ApexNode;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.impl.DefaultRuleViolationFactory;
public final class ApexRuleViolationFactory extends DefaultRuleViolationFactory {
public final class ApexViolationSuppressors {
public static final ApexRuleViolationFactory INSTANCE = new ApexRuleViolationFactory();
private static final ViolationSuppressor APEX_ANNOT_SUPPRESSOR = new ViolationSuppressor() {
@Override
public String getId() {
@ -49,12 +48,9 @@ public final class ApexRuleViolationFactory extends DefaultRuleViolationFactory
}
};
private ApexRuleViolationFactory() {
}
static final List<ViolationSuppressor> ALL_APEX_SUPPRESSORS = listOf(APEX_ANNOT_SUPPRESSOR);
@Override
protected List<ViolationSuppressor> getSuppressors() {
return Collections.singletonList(APEX_ANNOT_SUPPRESSOR);
private ApexViolationSuppressors() {
}
/**

View File

@ -21,7 +21,7 @@ import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.document.FileLocation;
import net.sourceforge.pmd.lang.document.TextRange2d;
import net.sourceforge.pmd.lang.rule.AbstractRule;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.processor.AbstractPMDProcessor;
import net.sourceforge.pmd.reporting.FileAnalysisListener;
import net.sourceforge.pmd.reporting.ViolationDecorator;
@ -127,7 +127,7 @@ public final class RuleContext {
* as a format string for a {@link MessageFormat} and should hence use
* appropriate escapes. The given formatting arguments are used.
*
* @param location Location of the violation
* @param node Location of the violation
* @param message Violation message
* @param formatArgs Format arguments for the message
*/
@ -137,16 +137,14 @@ public final class RuleContext {
Objects.requireNonNull(formatArgs, "Format arguments were null, use an empty array");
LanguageVersionHandler handler = node.getTextDocument().getLanguageVersion().getLanguageVersionHandler();
RuleViolationFactory fact = handler.getRuleViolationFactory();
FileLocation location = node.getReportLocation();
if (beginLine != -1 && endLine != -1) {
location = FileLocation.location(location.getFileName(), TextRange2d.range2d(beginLine, 1, endLine, 1));
}
violation = ViolationDecorator.apply(handler.getViolationDecorator(), violation, location);
RuleViolation violation = fact.createViolation(rule, node, location, makeMessage(message, formatArgs));
RuleViolation violation = new ParametricRuleViolation(rule, location, makeMessage(message, formatArgs));
violation = ViolationDecorator.apply(handler.getViolationDecorator(), violation, node);
SuppressedViolation suppressed = suppressOrNull(node, violation, handler);

View File

@ -14,14 +14,13 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.Report.SuppressedViolation;
import net.sourceforge.pmd.lang.ast.AstInfo;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.rule.xpath.XPathVersion;
import net.sourceforge.pmd.lang.rule.xpath.internal.DeprecatedAttrLogger;
import net.sourceforge.pmd.lang.rule.xpath.internal.SaxonXPathRuleQuery;
/**
* An object that suppresses rule violations. Suppressors are used by
* {@link RuleViolationFactory} to filter out violations. In PMD 6.0.x,
* {@link RuleContext} 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.
*/

View File

@ -11,8 +11,6 @@ import net.sourceforge.pmd.ViolationSuppressor;
import net.sourceforge.pmd.annotation.Experimental;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.metrics.LanguageMetricsProvider;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.rule.impl.DefaultRuleViolationFactory;
import net.sourceforge.pmd.lang.rule.xpath.impl.XPathHandler;
import net.sourceforge.pmd.properties.PropertySource;
import net.sourceforge.pmd.reporting.ViolationDecorator;
@ -62,13 +60,6 @@ public interface LanguageVersionHandler {
return Collections.emptyList();
}
/**
* Get the RuleViolationFactory.
*/
default RuleViolationFactory getRuleViolationFactory() {
return DefaultRuleViolationFactory.defaultInstance();
}
/**
* Returns the metrics provider for this language version,

View File

@ -14,8 +14,6 @@ import net.sourceforge.pmd.internal.util.AssertionUtil;
import net.sourceforge.pmd.lang.document.FileLocation;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.reporting.Reportable;
import net.sourceforge.pmd.util.DataMap;
import net.sourceforge.pmd.util.DataMap.TransparentDataMap;
/**
* @deprecated This is internal. Clients should exclusively use {@link RuleViolation}.
@ -99,26 +97,6 @@ public class ParametricRuleViolation implements RuleViolation {
return location;
}
@Override
public String getPackageName() {
return packageName;
}
@Override
public String getClassName() {
return className;
}
@Override
public String getMethodName() {
return methodName;
}
@Override
public String getVariableName() {
return variableName;
}
@Override
public Map<String, String> getAdditionalInfo() {
return Collections.emptyMap();

View File

@ -1,36 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.rule;
import net.sourceforge.pmd.Report.SuppressedViolation;
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.document.FileLocation;
/**
* Creates violations and controls suppression behavior for a language.
*
* TODO split this into violation decorators + violation suppressors.
* There is no need to have language-specific violation classes.
*
* <p>Since PMD 6.43.0, {@link RuleContext} has been enriched with methods that should
* be strongly preferred to using this interface directly. The interface will change a
* lot in PMD 7.
*/
public interface RuleViolationFactory {
// todo move to package reporting
default RuleViolation createViolation(Rule rule, Node node, FileLocation location, String formattedMessage) {
return new ParametricRuleViolation(rule, location, formattedMessage);
}
SuppressedViolation suppressOrNull(Node location, RuleViolation violation);
}

View File

@ -1,67 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.rule.impl;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import net.sourceforge.pmd.Report.SuppressedViolation;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.ViolationSuppressor;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
/**
* 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.
*/
public class DefaultRuleViolationFactory implements RuleViolationFactory {
// todo move to package reporting
private static final DefaultRuleViolationFactory DEFAULT = new DefaultRuleViolationFactory();
private Set<ViolationSuppressor> allSuppressors;
@Override
public SuppressedViolation suppressOrNull(Node location, RuleViolation violation) {
for (ViolationSuppressor suppressor : getAllSuppressors()) {
SuppressedViolation suppressed = suppressor.suppressOrNull(violation, location);
if (suppressed != null) {
return suppressed;
}
}
return null;
}
/**
* Returns a list of additional suppressors for this language. These
* are added to regular //NOPMD, regex and XPath suppression.
*/
protected List<ViolationSuppressor> getSuppressors() {
return Collections.emptyList();
}
private Set<ViolationSuppressor> getAllSuppressors() {
if (allSuppressors == null) {
// lazy loaded because calling getSuppressors in constructor
// is not safe wrt initialization of static constants
// (order dependent otherwise)
this.allSuppressors = new LinkedHashSet<>(getSuppressors());
allSuppressors.add(ViolationSuppressor.NOPMD_COMMENT_SUPPRESSOR);
allSuppressors.add(ViolationSuppressor.REGEX_SUPPRESSOR);
allSuppressors.add(ViolationSuppressor.XPATH_SUPPRESSOR);
}
return allSuppressors;
}
/** Returns the default instance (no additional suppressors, creates a ParametricRuleViolation). */
public static RuleViolationFactory defaultInstance() {
return DEFAULT;
}
}

View File

@ -6,7 +6,7 @@
* Logic about reporting: violations, suppression etc.
*
* <p>TODO move {@link net.sourceforge.pmd.Report}, {@link net.sourceforge.pmd.RuleViolation},
* {@link net.sourceforge.pmd.RuleContext}, {@link net.sourceforge.pmd.ViolationSuppressor},
* {@link net.sourceforge.pmd.lang.rule.RuleViolationFactory} into this package
* {@link net.sourceforge.pmd.RuleContext}, {@link net.sourceforge.pmd.ViolationSuppressor}
* into this package
*/
package net.sourceforge.pmd.reporting;

View File

@ -7,20 +7,23 @@ package net.sourceforge.pmd;
import static net.sourceforge.pmd.properties.constraints.NumericConstraints.inRange;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.Collections;
import org.junit.Test;
import net.sourceforge.pmd.Report.SuppressedViolation;
import net.sourceforge.pmd.lang.ast.DummyRoot;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.AbstractRule;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.lang.rule.impl.DefaultRuleViolationFactory;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import net.sourceforge.pmd.reporting.FileAnalysisListener;
public class AbstractRuleTest {
@ -114,10 +117,13 @@ public class AbstractRuleTest {
public void testRuleSuppress() {
DummyRoot n = new DummyRoot().withNoPmdComments(Collections.singletonMap(5, ""));
n.setCoordsReplaceText(5, 1, 6, 1);
RuleViolation violation = DefaultRuleViolationFactory.defaultInstance().createViolation(new MyRule(), n, n.getReportLocation(), "specificdescription");
SuppressedViolation suppressed = DefaultRuleViolationFactory.defaultInstance().suppressOrNull(n, violation);
assertNotNull(suppressed);
FileAnalysisListener listener = mock(FileAnalysisListener.class);
RuleContext ctx = RuleContext.create(listener, new MyRule());
ctx.addViolationWithMessage(n, "message");
verify(listener, never()).onRuleViolation(any());
verify(listener, times(1)).onSuppressedRuleViolation(any());
}
@Test

View File

@ -4,16 +4,10 @@
package net.sourceforge.pmd.lang;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.ast.DummyRoot;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.document.FileLocation;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
import net.sourceforge.pmd.lang.rule.impl.DefaultRuleViolationFactory;
import net.sourceforge.pmd.reporting.ViolationDecorator;
/**
* Dummy language used for testing PMD.
@ -39,11 +33,6 @@ public class DummyLanguageModule extends BaseLanguageModule {
public static class Handler extends AbstractPmdLanguageVersionHandler {
@Override
public RuleViolationFactory getRuleViolationFactory() {
return new RuleViolationFactory();
}
@Override
public Parser getParser() {
@ -57,6 +46,11 @@ public class DummyLanguageModule extends BaseLanguageModule {
return node;
};
}
@Override
public ViolationDecorator getViolationDecorator() {
return (rv, node, data) -> data.put(RuleViolation.PACKAGE_NAME, "foo");
}
}
public static class HandlerWithParserThatThrows extends Handler {
@ -67,17 +61,4 @@ public class DummyLanguageModule extends BaseLanguageModule {
};
}
}
public static class RuleViolationFactory extends DefaultRuleViolationFactory {
@Override
public RuleViolation createViolation(Rule rule, @NonNull Node node, FileLocation location, @NonNull String formattedMessage) {
return new ParametricRuleViolation(rule, location, formattedMessage) {
{
this.packageName = "foo"; // just for testing variable expansion
}
};
}
}
}

View File

@ -2,15 +2,23 @@
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.internal;
package net.sourceforge.pmd.lang.java.internal;
import static net.sourceforge.pmd.util.CollectionUtil.listOf;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.Report.SuppressedViolation;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.ViolationSuppressor;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAnnotation;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
@ -48,10 +56,27 @@ final class AnnotationSuppressionUtil {
private static final Set<String> UNUSED_RULES
= new HashSet<>(Arrays.asList("UnusedPrivateField", "UnusedLocalVariable", "UnusedPrivateMethod",
"UnusedFormalParameter", "UnusedAssignment", "SingularField"));
"UnusedFormalParameter", "UnusedAssignment", "SingularField"));
private static final Set<String> SERIAL_RULES =
new HashSet<>(Arrays.asList("BeanMembersShouldSerialize", "MissingSerialVersionUID"));
static final ViolationSuppressor JAVA_ANNOT_SUPPRESSOR = new ViolationSuppressor() {
@Override
public String getId() {
return "@SuppressWarnings";
}
@Override
public Report.SuppressedViolation suppressOrNull(RuleViolation rv, @NonNull Node node) {
if (contextSuppresses(node, rv.getRule())) {
return new SuppressedViolation(rv, this, null);
}
return null;
}
};
static final List<ViolationSuppressor> ALL_JAVA_SUPPRESSORS = listOf(JAVA_ANNOT_SUPPRESSOR);
private AnnotationSuppressionUtil() {
}

View File

@ -6,15 +6,16 @@ package net.sourceforge.pmd.lang.java.internal;
import static net.sourceforge.pmd.util.CollectionUtil.setOf;
import java.util.List;
import java.util.Set;
import net.sourceforge.pmd.ViolationSuppressor;
import net.sourceforge.pmd.lang.AbstractPmdLanguageVersionHandler;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.java.ast.JavaParser;
import net.sourceforge.pmd.lang.java.ast.internal.LanguageLevelChecker;
import net.sourceforge.pmd.lang.java.ast.internal.ReportingStrategy;
import net.sourceforge.pmd.lang.java.metrics.JavaMetrics;
import net.sourceforge.pmd.lang.java.rule.internal.JavaRuleViolationFactory;
import net.sourceforge.pmd.lang.java.rule.xpath.internal.BaseContextNodeTestFun;
import net.sourceforge.pmd.lang.java.rule.xpath.internal.GetCommentOnFunction;
import net.sourceforge.pmd.lang.java.rule.xpath.internal.GetModifiersFun;
@ -23,7 +24,6 @@ import net.sourceforge.pmd.lang.java.rule.xpath.internal.MetricFunction;
import net.sourceforge.pmd.lang.java.rule.xpath.internal.NodeIsFunction;
import net.sourceforge.pmd.lang.metrics.LanguageMetricsProvider;
import net.sourceforge.pmd.lang.metrics.Metric;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.rule.xpath.impl.XPathHandler;
import net.sourceforge.pmd.reporting.ViolationDecorator;
import net.sourceforge.pmd.util.designerbindings.DesignerBindings;
@ -78,8 +78,8 @@ public class JavaLanguageHandler extends AbstractPmdLanguageVersionHandler {
}
@Override
public RuleViolationFactory getRuleViolationFactory() {
return JavaRuleViolationFactory.INSTANCE;
public List<ViolationSuppressor> getExtraViolationSuppressor() {
return AnnotationSuppressionUtil.ALL_JAVA_SUPPRESSORS;
}
@Override

View File

@ -1,118 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule;
import java.util.Iterator;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.NodeStream;
import net.sourceforge.pmd.lang.document.FileLocation;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTFormalParameter;
import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.AccessNode;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.rule.ParametricRuleViolation;
/**
* This is a Java RuleViolation. It knows how to try to extract the following
* extra information from the violation node:
* <ul>
* <li>Package name</li>
* <li>Class name</li>
* <li>Method name</li>
* <li>Variable name</li>
* <li>Suppression indicator</li>
* </ul>
* @deprecated See {@link RuleViolation}
*/
@Deprecated
public class JavaRuleViolation extends ParametricRuleViolation {
public JavaRuleViolation(Rule rule, @NonNull JavaNode node, FileLocation location, String message) {
super(rule, location, message);
ASTCompilationUnit root = node.getRoot();
packageName = root.getPackageName();
className = getClassName(node);
methodName = getMethodName(node);
variableName = getVariableNameIfExists(node);
}
@Nullable
private static String getClassName(JavaNode node) {
ASTAnyTypeDeclaration enclosing = node instanceof ASTAnyTypeDeclaration ? (ASTAnyTypeDeclaration) node
: node.getEnclosingType();
ASTCompilationUnit file = node.getRoot();
if (enclosing == null) {
NodeStream<ASTAnyTypeDeclaration> tds = file.getTypeDeclarations();
enclosing = tds.first(AccessNode::isPublic);
if (enclosing == null) {
enclosing = tds.first();
}
}
if (enclosing == null) {
return null;
} else {
String binaryName = enclosing.getBinaryName();
String packageName = enclosing.getPackageName();
return packageName.isEmpty()
? binaryName
// plus 1 for the '.'
: binaryName.substring(packageName.length() + 1);
}
}
@Nullable
private static String getMethodName(JavaNode node) {
// ancestorsOrSelf..........
ASTMethodOrConstructorDeclaration enclosing =
node instanceof ASTMethodOrConstructorDeclaration ? (ASTMethodOrConstructorDeclaration) node
: node.getFirstParentOfType(ASTMethodOrConstructorDeclaration.class);
return enclosing == null ? null : enclosing.getName();
}
private static String getVariableNames(Iterable<ASTVariableDeclaratorId> iterable) {
Iterator<ASTVariableDeclaratorId> it = iterable.iterator();
StringBuilder builder = new StringBuilder();
builder.append(it.next());
while (it.hasNext()) {
builder.append(", ").append(it.next());
}
return builder.toString();
}
private static String getVariableNameIfExists(Node node) {
if (node instanceof ASTFieldDeclaration) {
return getVariableNames((ASTFieldDeclaration) node);
} else if (node instanceof ASTLocalVariableDeclaration) {
return getVariableNames((ASTLocalVariableDeclaration) node);
} else if (node instanceof ASTVariableDeclarator) {
return ((ASTVariableDeclarator) node).getVarId().getVariableName();
} else if (node instanceof ASTVariableDeclaratorId) {
return ((ASTVariableDeclaratorId) node).getVariableName();
} else if (node instanceof ASTFormalParameter) {
return ((ASTFormalParameter) node).getVarId().getVariableName();
} else {
return "";
}
}
}

View File

@ -1,56 +0,0 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.internal;
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;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.ViolationSuppressor;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.document.FileLocation;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.rule.JavaRuleViolation;
import net.sourceforge.pmd.lang.rule.RuleViolationFactory;
import net.sourceforge.pmd.lang.rule.impl.DefaultRuleViolationFactory;
public final class JavaRuleViolationFactory extends DefaultRuleViolationFactory {
public static final RuleViolationFactory INSTANCE = new JavaRuleViolationFactory();
private static final ViolationSuppressor JAVA_ANNOT_SUPPRESSOR = new ViolationSuppressor() {
@Override
public String getId() {
return "@SuppressWarnings";
}
@Override
public Report.SuppressedViolation suppressOrNull(RuleViolation rv, @NonNull Node node) {
if (AnnotationSuppressionUtil.contextSuppresses(node, rv.getRule())) {
return new SuppressedViolation(rv, this, null);
}
return null;
}
};
private JavaRuleViolationFactory() {
// singleton
}
@Override
protected List<ViolationSuppressor> getSuppressors() {
return Collections.singletonList(JAVA_ANNOT_SUPPRESSOR);
}
@Override
public RuleViolation createViolation(Rule rule, @NonNull Node node, FileLocation location, @NonNull String formattedMessage) {
return new JavaRuleViolation(rule, (JavaNode) node, location, formattedMessage);
}
}

View File

@ -7,11 +7,14 @@ package net.sourceforge.pmd.lang.java.rule;
import static org.junit.Assert.assertEquals;
import java.util.List;
import java.util.Objects;
import org.apache.commons.lang3.mutable.MutableObject;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.junit.Test;
import net.sourceforge.pmd.FooRule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.lang.java.JavaParsingHelper;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
@ -29,10 +32,6 @@ public class JavaRuleViolationTest {
// TODO there are no tests for anon or local classes
/**
* Verifies that {@link JavaRuleViolation} sets the variable name for an
* {@link ASTFormalParameter} node.
*/
@Test
public void testASTFormalParameterVariableName() {
ASTCompilationUnit ast = parse("class Foo { void bar(int x) {} }");
@ -57,9 +56,11 @@ public class JavaRuleViolationTest {
assertEquals("bar", violationAt(md).getMethodName());
}
@NonNull
public RuleViolation violationAt(JavaNode md) {
return new JavaRuleViolation(new FooRule(), md, md.getReportLocation(), "");
public @NonNull RuleViolation violationAt(JavaNode md) {
MutableObject<RuleViolation> rv = new MutableObject<>();
RuleContext rctx = RuleContext.create(rv::setValue, new FooRule());
rctx.addViolation(md);
return Objects.requireNonNull(rv.getValue());
}
/**

View File

@ -14,7 +14,6 @@ import net.sourceforge.pmd.lang.ast.AstInfo;
import net.sourceforge.pmd.lang.ast.Parser;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.document.TextDocument;
import net.sourceforge.pmd.lang.rule.impl.DefaultRuleViolationFactory;
import net.sourceforge.pmd.test.lang.ast.DummyNode;
/**
@ -39,10 +38,6 @@ public class DummyLanguageModule extends BaseLanguageModule {
}
public static class Handler extends AbstractPmdLanguageVersionHandler {
@Override
public RuleViolationFactory getRuleViolationFactory() {
return new RuleViolationFactory();
}
@Override
public Parser getParser() {
@ -81,8 +76,4 @@ public class DummyLanguageModule extends BaseLanguageModule {
}
}
public static class RuleViolationFactory extends DefaultRuleViolationFactory {
}
}