From ca34d4fa1c15e292b6f6f5dd695c838e00d6d2ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Thu, 18 Jul 2019 19:34:51 -0300 Subject: [PATCH 1/3] Add failing test case for #1903 --- .../xml/UnsynchronizedStaticFormatter.xml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/multithreading/xml/UnsynchronizedStaticFormatter.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/multithreading/xml/UnsynchronizedStaticFormatter.xml index a97fa56f96..e8a0677f0c 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/multithreading/xml/UnsynchronizedStaticFormatter.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/multithreading/xml/UnsynchronizedStaticFormatter.xml @@ -258,6 +258,24 @@ public class Foo { sdf.format(); } } +} + ]]> + + + + #1903 - Block synch is valid even with allowMethodLevelSynchronization + true + 0 + From cb62b7e472580fff6fd940e305aded678e2a7999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Thu, 18 Jul 2019 19:35:09 -0300 Subject: [PATCH 2/3] Fix false positive on UnsynchronizedStaticFormatterRule --- .../UnsynchronizedStaticFormatterRule.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/multithreading/UnsynchronizedStaticFormatterRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/multithreading/UnsynchronizedStaticFormatterRule.java index 4ea482bf6f..af9e04c26b 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/multithreading/UnsynchronizedStaticFormatterRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/multithreading/UnsynchronizedStaticFormatterRule.java @@ -77,14 +77,7 @@ public class UnsynchronizedStaticFormatterRule extends AbstractJavaRule { continue; } - if (getProperty(ALLOW_METHOD_LEVEL_SYNC)) { - ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class); - if (method != null && (!method.isSynchronized() || !method.isStatic())) { - addViolation(data, node); - } - continue; - } - + // is there a block-level synch? ASTSynchronizedStatement syncStatement = n.getFirstParentOfType(ASTSynchronizedStatement.class); if (syncStatement != null) { ASTExpression expression = syncStatement.getFirstChildOfType(ASTExpression.class); @@ -95,6 +88,15 @@ public class UnsynchronizedStaticFormatterRule extends AbstractJavaRule { } } } + + // method level synch enabled and used? + if (getProperty(ALLOW_METHOD_LEVEL_SYNC)) { + ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class); + if (method != null && method.isSynchronized() && method.isStatic()) { + continue; + } + } + addViolation(data, n); } return data; From c39a342b9230eb87dcdda249cf15c8de9ea76dea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Sotuyo=20Dodero?= Date: Thu, 18 Jul 2019 19:36:21 -0300 Subject: [PATCH 3/3] Update changelog, refs #1903 --- docs/pages/release_notes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index fe3d2aa4c7..06417b1a9d 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -29,6 +29,8 @@ Being based on a proper Antlr grammar, CPD can: * [#1896](https://github.com/pmd/pmd/issues/1896): \[doc] Error in changelog 6.16.0 due to not properly closed rule tag * [#1906](https://github.com/pmd/pmd/issues/1906): \[doc] Broken link for adding own CPD languages * [#1909](https://github.com/pmd/pmd/issues/1909): \[doc] Sample usage example refers to deprecated ruleset "basic.xml" instead of "quickstart.xml" +* java-multithreading + * [#1903](https://github.com/pmd/pmd/issues/1903): \[java] UnsynchronizedStaticFormatter doesn't allow block-level synchronization when using allowMethodLevelSynchronization=true * xml * [#1666](https://github.com/pmd/pmd/issues/1666): \[xml] wrong cdata rule description and examples