#1398 False positive for GuardLogStatementJavaUtil with Log4j

This commit is contained in:
Andreas Dangel
2015-09-06 11:06:48 +02:00
parent a1ab0d6657
commit 100dd09b28
3 changed files with 20 additions and 3 deletions

View File

@ -26,7 +26,7 @@ public class GuardLogStatementJavaUtilRule extends GuardLogStatementRule {
@Override
public Object visit(ASTCompilationUnit unit, Object data) {
if (isSlf4jImported(unit)) {
if (isSlf4jOrLog4jImported(unit)) {
return data;
}
@ -43,10 +43,10 @@ public class GuardLogStatementJavaUtilRule extends GuardLogStatementRule {
return super.visit(unit,data);
}
private boolean isSlf4jImported(ASTCompilationUnit unit) {
private boolean isSlf4jOrLog4jImported(ASTCompilationUnit unit) {
List<ASTImportDeclaration> imports = unit.findChildrenOfType(ASTImportDeclaration.class);
for (ASTImportDeclaration i : imports) {
if (i.getImportedName().startsWith("org.slf4j")) {
if (i.getImportedName().startsWith("org.slf4j") || i.getImportedName().startsWith("org.apache.log4j")) {
return true;
}
}

View File

@ -100,4 +100,20 @@ public class GuardLogTest {
}
]]></code>
</test-code>
<test-code>
<description>#1398 False positive for GuardLogStatementJavaUtil with Log4j</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.apache.log4j.Logger;
public class GuardLogTest {
Logger LOG;
public void foo() {
if (LOG.isInfoEnabled()) {
LOG.info("update: After spool map size: " + map.size());
}
}
}
]]></code>
</test-code>
</test-data>

View File

@ -15,6 +15,7 @@
* [#1370](https://sourceforge.net/p/pmd/bugs/1370/): ConsecutiveAppendsShouldReuse not detected properly on StringBuffer
* [#1371](https://sourceforge.net/p/pmd/bugs/1371/): InsufficientStringBufferDeclaration not detected properly on StringBuffer
* [#1384](https://sourceforge.net/p/pmd/bugs/1384/): NullPointerException in ConsecutiveLiteralAppendsRule
* [#1398](https://sourceforge.net/p/pmd/bugs/1398/): False positive for GuardLogStatementJavaUtil with Log4j
* [#1399](https://sourceforge.net/p/pmd/bugs/1399/): False positive for VariableNamingConventions with annotation @interface
* [#1400](https://sourceforge.net/p/pmd/bugs/1400/): False positive with JUnit4TestShouldUseBeforeAnnotation
* [#1401](https://sourceforge.net/p/pmd/bugs/1401/): False positive for StringBuilder.append called with constructor