From fde23333a0eaa89c05408aa17399889dca46d670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Tue, 25 Aug 2020 16:25:01 +0200 Subject: [PATCH 1/3] Deprecate ruleviolation comparator --- .../src/main/java/net/sourceforge/pmd/Report.java | 6 +++--- .../main/java/net/sourceforge/pmd/RuleViolation.java | 10 ++++++++++ .../net/sourceforge/pmd/RuleViolationComparator.java | 12 ++++++++++++ .../sourceforge/pmd/RuleViolationComparatorTest.java | 2 +- .../java/net/sourceforge/pmd/RuleViolationTest.java | 7 ++++--- 5 files changed, 30 insertions(+), 7 deletions(-) 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 3d6cd2d45d..f0a75a7028 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/Report.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/Report.java @@ -345,7 +345,7 @@ public class Report implements Iterable { return; } - int index = Collections.binarySearch(violations, violation, RuleViolationComparator.INSTANCE); + int index = Collections.binarySearch(violations, violation, RuleViolation.DEFAULT_COMPARATOR); violations.add(index < 0 ? -index - 1 : index, violation); violationTree.addRuleViolation(violation); for (ThreadSafeReportListener listener : listeners) { @@ -405,7 +405,7 @@ public class Report implements Iterable { suppressedRuleViolations.addAll(r.suppressedRuleViolations); for (RuleViolation violation : r.getViolations()) { - int index = Collections.binarySearch(violations, violation, RuleViolationComparator.INSTANCE); + int index = Collections.binarySearch(violations, violation, RuleViolation.DEFAULT_COMPARATOR); violations.add(index < 0 ? -index - 1 : index, violation); violationTree.addRuleViolation(violation); } @@ -519,7 +519,7 @@ public class Report implements Iterable { * Returns an unmodifiable list of violations that have been * recorded until now. None of those violations were suppressed. * - *

The violations list is sorted with {@link RuleViolationComparator#INSTANCE}. + *

The violations list is sorted with {@link RuleViolation#DEFAULT_COMPARATOR}. */ public final List getViolations() { return Collections.unmodifiableList(violations); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleViolation.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleViolation.java index 5873d6e4db..b8359f6a7c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleViolation.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleViolation.java @@ -4,6 +4,8 @@ package net.sourceforge.pmd; +import java.util.Comparator; + /** * A RuleViolation is created by a Rule when it identifies a violation of the * Rule constraints. RuleViolations are simple data holders that are collected @@ -16,6 +18,14 @@ package net.sourceforge.pmd; */ public interface RuleViolation { + /** + * A comparator for rule violations. This compares all exposed attributes + * of a violation, filename first. The remaining parameters are compared + * in an unspecified order. + */ + // TODO in java 8 this can be a chained Comparator.comparing call + Comparator DEFAULT_COMPARATOR = RuleViolationComparator.INSTANCE; + /** * Get the Rule which identified this violation. * diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/RuleViolationComparator.java b/pmd-core/src/main/java/net/sourceforge/pmd/RuleViolationComparator.java index bbfa48ed07..bc1b1adf39 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/RuleViolationComparator.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/RuleViolationComparator.java @@ -17,7 +17,19 @@ import java.util.Comparator; *

  • End column
  • *
  • Rule name
  • * + * + * TODO why is begin line/begin column split?? would make more sense to use + * - filename + * - begin line + * - begin column + * - description + * - rule name + * - end line + * - end column + * + * @deprecated Use {@link RuleViolation#DEFAULT_COMPARATOR} */ +@Deprecated public final class RuleViolationComparator implements Comparator { public static final RuleViolationComparator INSTANCE = new RuleViolationComparator(); diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationComparatorTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationComparatorTest.java index e0643c0e01..c1f74cdf66 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationComparatorTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationComparatorTest.java @@ -58,7 +58,7 @@ public class RuleViolationComparatorTest { Collections.shuffle(ruleViolations, random); // Sort - Collections.sort(ruleViolations, RuleViolationComparator.INSTANCE); + Collections.sort(ruleViolations, RuleViolation.DEFAULT_COMPARATOR); // Check int count = 0; diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationTest.java index 9fd8554203..c6125da54c 100644 --- a/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationTest.java +++ b/pmd-core/src/test/java/net/sourceforge/pmd/RuleViolationTest.java @@ -8,6 +8,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import java.io.File; +import java.util.Comparator; import org.junit.Ignore; import org.junit.Test; @@ -53,7 +54,7 @@ public class RuleViolationTest { @Test public void testComparatorWithDifferentFilenames() { Rule rule = new MockRule("name", "desc", "msg", "rulesetname"); - RuleViolationComparator comp = RuleViolationComparator.INSTANCE; + Comparator comp = RuleViolation.DEFAULT_COMPARATOR; RuleContext ctx = new RuleContext(); ctx.setSourceCodeFile(new File("filename1")); DummyNode s = new DummyNode(1); @@ -72,7 +73,7 @@ public class RuleViolationTest { @Test public void testComparatorWithSameFileDifferentLines() { Rule rule = new MockRule("name", "desc", "msg", "rulesetname"); - RuleViolationComparator comp = RuleViolationComparator.INSTANCE; + Comparator comp = RuleViolation.DEFAULT_COMPARATOR; RuleContext ctx = new RuleContext(); ctx.setSourceCodeFile(new File("filename")); DummyNode s = new DummyNode(1); @@ -91,7 +92,7 @@ public class RuleViolationTest { @Test public void testComparatorWithSameFileSameLines() { Rule rule = new MockRule("name", "desc", "msg", "rulesetname"); - RuleViolationComparator comp = RuleViolationComparator.INSTANCE; + Comparator comp = RuleViolation.DEFAULT_COMPARATOR; RuleContext ctx = new RuleContext(); ctx.setSourceCodeFile(new File("filename")); DummyNode s = new DummyNode(1); From 31b75d3acbf872dcc04964e4d5b3dafd3c0903b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Tue, 25 Aug 2020 16:48:16 +0200 Subject: [PATCH 2/3] Update release notes --- docs/pages/release_notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index cc0ca22491..089377fc94 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -120,6 +120,7 @@ For the changes, see [PMD Designer Changelog](https://github.com/pmd/pmd-designe ##### For removal +* {% jdoc !!core::RuleViolationComparator %} * {% jdoc !!core::Rule#getParserOptions() %} * {% jdoc !!core::lang.Parser#getParserOptions() %} * {% jdoc core::lang.AbstractParser %} From d03960574721dac667da1adfb940b583638ddec8 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 12 Sep 2020 11:51:11 +0200 Subject: [PATCH 3/3] [doc] Mention replacement for deprecated RuleViolationComparator --- docs/pages/release_notes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 708c858408..14e1adb41d 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -22,7 +22,7 @@ This is a {{ site.pmd.release_type }} release. ##### For removal -* {% jdoc !!core::RuleViolationComparator %} +* {% jdoc !!core::RuleViolationComparator %}. Use {% jdoc !!core::RuleViolation#DEFAULT_COMPARATOR %} instead. ### External Contributions