From 5ffb9531917fbc83344c98a9921b3fb340c633de Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 18 Oct 2024 09:40:02 +0200 Subject: [PATCH 1/2] [java] CouplingBetweenObjects: improve violation message When we add the actual count to the message, we can more easily verify that the rule works as expected. --- .../lang/java/rule/design/CouplingBetweenObjectsRule.java | 6 +++--- pmd-java/src/main/resources/category/java/design.xml | 2 +- .../lang/java/rule/design/xml/CouplingBetweenObjects.xml | 6 ++++++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CouplingBetweenObjectsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CouplingBetweenObjectsRule.java index 527477529c..230e0861c7 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CouplingBetweenObjectsRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/CouplingBetweenObjectsRule.java @@ -52,9 +52,9 @@ public class CouplingBetweenObjectsRule extends AbstractJavaRule { public Object visit(ASTCompilationUnit cu, Object data) { super.visit(cu, data); - if (couplingCount > getProperty(THRESHOLD_DESCRIPTOR)) { - asCtx(data).addViolation(cu, - "A value of " + couplingCount + " may denote a high amount of coupling within the class"); + Integer threshold = getProperty(THRESHOLD_DESCRIPTOR); + if (couplingCount > threshold) { + asCtx(data).addViolation(cu, couplingCount, threshold); } couplingCount = 0; diff --git a/pmd-java/src/main/resources/category/java/design.xml b/pmd-java/src/main/resources/category/java/design.xml index 37ca5dc3cd..d235d4239b 100644 --- a/pmd-java/src/main/resources/category/java/design.xml +++ b/pmd-java/src/main/resources/category/java/design.xml @@ -377,7 +377,7 @@ class Foo { diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/CouplingBetweenObjects.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/CouplingBetweenObjects.xml index 28b2e0abaf..49a39332f1 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/CouplingBetweenObjects.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/CouplingBetweenObjects.xml @@ -9,6 +9,9 @@ 2 1 1 + + A value of 3 may denote a high amount of coupling within the class (threshold: 2) + 2 1 1 + + A value of 3 may denote a high amount of coupling within the class (threshold: 2) + Date: Fri, 18 Oct 2024 15:39:52 +0200 Subject: [PATCH 2/2] [doc] Update release notes (#5278) --- 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 c80d7c0cc2..a160b83a88 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -55,6 +55,7 @@ The old rule names still work but are deprecated. * [#5258](https://github.com/pmd/pmd/pull/5258): Ignore generated antlr classes in coverage reports - [Juan Martín Sotuyo Dodero](https://github.com/jsotuyod) (@jsotuyod) * [#5264](https://github.com/pmd/pmd/pull/5264): \[java] Fix NPE with empty pattern list - [Clément Fournier](https://github.com/oowekyala) (@oowekyala) * [#5269](https://github.com/pmd/pmd/pull/5269): \[java] Fix #5253: Support Boolean wrapper class for BooleanGetMethodName rule - [Aryant Tripathi](https://github.com/Aryant-Tripathi) (@Aryant-Tripathi) +* [#5278](https://github.com/pmd/pmd/pull/5278): \[java] CouplingBetweenObjects: improve violation message - [Andreas Dangel](https://github.com/adangel) (@adangel) {% endtocmaker %}