diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index c094f04a50..0068f262c7 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -32,6 +32,8 @@ Being based on a proper Antlr grammar, CPD can: * [#1898](https://github.com/pmd/pmd/issues/1898): \[doc] Incorrect code example for DoubleBraceInitialization in documentation on website * [#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 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; 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 +