forked from phoedos/pmd
#1335 GuardLogStatementJavaUtil should not apply to SLF4J Logger
This commit is contained in:
@ -3,9 +3,11 @@
|
||||
*/
|
||||
package net.sourceforge.pmd.lang.java.rule.logging;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
|
||||
|
||||
public class GuardLogStatementJavaUtilRule extends GuardLogStatementRule {
|
||||
|
||||
@ -24,6 +26,10 @@ public class GuardLogStatementJavaUtilRule extends GuardLogStatementRule {
|
||||
|
||||
@Override
|
||||
public Object visit(ASTCompilationUnit unit, Object data) {
|
||||
if (isSlf4jImported(unit)) {
|
||||
return data;
|
||||
}
|
||||
|
||||
String[] logLevels = getProperty(LOG_LEVELS);
|
||||
String[] guardMethods = getProperty(GUARD_METHODS);
|
||||
|
||||
@ -37,6 +43,16 @@ public class GuardLogStatementJavaUtilRule extends GuardLogStatementRule {
|
||||
return super.visit(unit,data);
|
||||
}
|
||||
|
||||
private boolean isSlf4jImported(ASTCompilationUnit unit) {
|
||||
List<ASTImportDeclaration> imports = unit.findChildrenOfType(ASTImportDeclaration.class);
|
||||
for (ASTImportDeclaration i : imports) {
|
||||
if (i.getImportedName().startsWith("org.slf4j")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void configureGuards(String[] logLevels, String[] guardMethods) {
|
||||
String[] methods = guardMethods;
|
||||
if (methods.length != logLevels.length) {
|
||||
|
@ -64,6 +64,22 @@ public class Foo {
|
||||
LOGGER.log(Level.FINE, "This is a severe message" + " and concat"); // no violation
|
||||
}
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description>#1335 GuardLogStatementJavaUtil should not apply to SLF4J Logger</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class GuardLogTest {
|
||||
Logger LOGGER = LoggerFactory.getLogger(getClass());
|
||||
|
||||
public void foo() {
|
||||
LOGGER.info("foo" + "bar");
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
@ -12,6 +12,7 @@
|
||||
|
||||
**Bugfixes:**
|
||||
|
||||
* [#1335](https://sourceforge.net/p/pmd/bugs/1335/): GuardLogStatementJavaUtil should not apply to SLF4J Logger
|
||||
* [#1342](https://sourceforge.net/p/pmd/bugs/1342/): UseConcurrentHashMap false positive (with documentation example)
|
||||
|
||||
**API Changes:**
|
||||
|
Reference in New Issue
Block a user