Skip to content

Commit

Permalink
Merge pull request #5015 from oowekyala/issue2368
Browse files Browse the repository at this point in the history
[java]  Fix #2368 - UnsynchronizedStaticFormatter FP in static initializer
  • Loading branch information
jsotuyod committed May 14, 2024
2 parents 3f61843 + 88b8adb commit 9acb774
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/pages/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Since this release, PMD will also expose any getter returning a collection of an
* [#4930](https://github.com/pmd/pmd/issues/4930): \[java] EmptyControlStatement should not allow empty try with concise resources
* java-errorprone
* [#4042](https://github.com/pmd/pmd/issues/4042): \[java] A false negative about the rule StringBufferInstantiationWithChar
* java-multithreading
* [#2368](https://github.com/pmd/pmd/issues/2368): \[java] False positive UnsynchronizedStaticFormatter in static initializer

### 🚨 API Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import java.util.Arrays;
import java.util.List;

import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTAssignableExpr.ASTNamedReferenceExpr;
import net.sourceforge.pmd.lang.java.ast.ASTClassType;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTInitializer;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement;
import net.sourceforge.pmd.lang.java.ast.ASTType;
import net.sourceforge.pmd.lang.java.ast.ASTVariableId;
import net.sourceforge.pmd.lang.java.ast.JModifier;
import net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils;
Expand Down Expand Up @@ -62,12 +63,13 @@ public Object visit(ASTFieldDeclaration node, Object data) {
if (!node.hasModifiers(JModifier.STATIC)) {
return data;
}
ASTClassType cit = node.descendants(ASTClassType.class).first();
if (cit == null || !TypeTestUtil.isA(formatterClassToCheck, cit)) {
ASTType cit = node.getTypeNode();
if (!(cit instanceof ASTClassType)
|| !TypeTestUtil.isA(formatterClassToCheck, cit)) {
return data;
}

ASTVariableId var = node.descendants(ASTVariableId.class).first();
ASTVariableId var = node.getVarIds().firstOrThrow();
for (String formatter: THREAD_SAFE_FORMATTER) {
if (TypeTestUtil.isA(formatter, var)) {
return data;
Expand All @@ -83,8 +85,6 @@ public Object visit(ASTFieldDeclaration node, Object data) {
continue;
}

Node n = ref;

// is there a block-level synch?
ASTSynchronizedStatement syncStatement = ref.ancestors(ASTSynchronizedStatement.class).first();
if (syncStatement != null) {
Expand All @@ -102,7 +102,12 @@ public Object visit(ASTFieldDeclaration node, Object data) {
}
}

asCtx(data).addViolation(n);
boolean hasStaticInit = ref.ancestors(ASTInitializer.class).any(ASTInitializer::isStatic);
if (hasStaticInit) {
continue;
}

asCtx(data).addViolation(ref);
}
return data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,21 @@ public class Foo {
}
]]></code>
</test-code>
<test-code>
<description> [java] False positive UnsynchronizedStaticFormatter in static initializer #2368 </description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.text.NumberFormat;
class WithFormatter {
private static final NumberFormat formatter;
static {
formatter = NumberFormat.getInstance();
formatter.setMaximumFractionDigits(2); // FALSE POSITIVE
}
// …
}
]]></code>
</test-code>
</test-data>

0 comments on commit 9acb774

Please sign in to comment.