forked from phoedos/pmd
#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:
@ -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) {
|
||||
|
@ -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>
|
||||
|
Reference in New Issue
Block a user