diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/Report.java b/pmd-core/src/main/java/net/sourceforge/pmd/Report.java index 4e50199537..731885f9b4 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/Report.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/Report.java @@ -156,7 +156,7 @@ public class Report implements Iterable { public String getMsg() { return error.getClass().getSimpleName() + ": " + error.getMessage(); } - + public String getDetail() { try (StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter)) { @@ -247,8 +247,8 @@ public class Report implements Iterable { /** * Creates a suppressed violation. * - * @param rv The violation, that has been suppressed - * @param suppressor The suppressor which suppressed the violation + * @param rv The violation, that has been suppressed + * @param suppressor The suppressor which suppressed the violation * @param userMessage Any relevant info given by the suppressor */ public SuppressedViolation(RuleViolation rv, ViolationSuppressor suppressor, String userMessage) { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/DefaultRuleViolationFactory.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/DefaultRuleViolationFactory.java index 12fd5b9a1c..d207e1daba 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/DefaultRuleViolationFactory.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/rule/DefaultRuleViolationFactory.java @@ -11,6 +11,7 @@ import java.util.List; import java.util.Set; import org.apache.commons.lang3.StringUtils; +import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import net.sourceforge.pmd.Report.SuppressedViolation; @@ -32,6 +33,15 @@ public class DefaultRuleViolationFactory implements RuleViolationFactory { private static final Object[] NO_ARGS = new Object[0]; private static final DefaultRuleViolationFactory DEFAULT = new DefaultRuleViolationFactory(); + private @NonNull Set allSuppressors; + + + public DefaultRuleViolationFactory() { + this.allSuppressors = new LinkedHashSet<>(getSuppressors()); + allSuppressors.add(ViolationSuppressor.NOPMD_COMMENT_SUPPRESSOR); + allSuppressors.add(ViolationSuppressor.REGEX_SUPPRESSOR); + allSuppressors.add(ViolationSuppressor.XPATH_SUPPRESSOR); + } private String cleanup(String message, Object[] args) { @@ -65,13 +75,12 @@ public class DefaultRuleViolationFactory implements RuleViolationFactory { } private void maybeSuppress(RuleContext ruleContext, @Nullable Node node, RuleViolation rv) { - Set suppressors = new LinkedHashSet<>(getSuppressors()); - suppressors.add(ViolationSuppressor.NOPMD_COMMENT_SUPPRESSOR); - suppressors.add(ViolationSuppressor.REGEX_SUPPRESSOR); - suppressors.add(ViolationSuppressor.XPATH_SUPPRESSOR); if (node != null) { - for (ViolationSuppressor suppressor : suppressors) { + // note: no suppression when node is null. + // Node should in fact never be null, this is todo for later + + for (ViolationSuppressor suppressor : allSuppressors) { SuppressedViolation suppressed = suppressor.suppressOrNull(rv, node); if (suppressed != null) { ruleContext.getReport().addSuppressedViolation(suppressed); @@ -79,6 +88,7 @@ public class DefaultRuleViolationFactory implements RuleViolationFactory { } } } + ruleContext.getReport().addRuleViolation(rv); }