#1633 [java] UnsynchronizedStaticFormatter reports commons lang FastDateFormat

Thread safe formatter org.apache.commons.lang3.time.FastDateFormat can not avoid UnsynchronizedStaticFormatterRule.
This commit is contained in:
Shubham
2019-01-30 14:02:17 +05:30
parent dab1fb24c6
commit 149f85a7e4
2 changed files with 21 additions and 0 deletions

View File

@ -5,6 +5,7 @@
package net.sourceforge.pmd.lang.java.rule.multithreading;
import java.text.Format;
import java.util.Arrays;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
@ -27,6 +28,9 @@ import net.sourceforge.pmd.lang.symboltable.NameOccurrence;
*/
public class UnsynchronizedStaticFormatterRule extends AbstractJavaRule {
private Class<?> formatterClassToCheck = Format.class;
private String[] threadSafeFormatter = {
"org.apache.commons.lang3.time.FastDateFormat",
};
public UnsynchronizedStaticFormatterRule() {
addRuleChainVisit(ASTFieldDeclaration.class);
@ -48,6 +52,9 @@ public class UnsynchronizedStaticFormatterRule extends AbstractJavaRule {
}
ASTVariableDeclaratorId var = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
if (Arrays.asList(threadSafeFormatter).contains(var.getType().getName())) {
return data;
}
for (NameOccurrence occ : var.getUsages()) {
Node n = occ.getLocation();
if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) {

View File

@ -132,6 +132,20 @@ public class Foo {
void bar() {
messageFormat.format();
}
}
]]></code>
</test-code>
<test-code>
<description>#1633 [java] UnsynchronizedStaticFormatter reports commons lang FastDateFormat</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.apache.commons.lang3.time.FastDateFormat;
public class Foo {
private static final FastDateFormat FDF = FastDateFormat.getInstance("dd.MM.yyyy HH:mm:ss");
public void format() {
FDF.format(new Date());
}
}
]]></code>
</test-code>