InvalidSlf4jMessageFormat: Fix class cast exception with method calls

This commit is contained in:
Andreas Dangel
2015-11-01 18:51:37 +01:00
parent a0bef4d9fe
commit e7c0d03180
2 changed files with 19 additions and 2 deletions

View File

@ -26,6 +26,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
import net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
import org.apache.commons.lang3.StringUtils;
@ -42,8 +43,8 @@ public class InvalidSlf4jMessageFormatRule extends AbstractJavaRule {
@Override
public Object visit(final ASTName node, final Object data) {
final NameDeclaration nameDeclaration = node.getNameDeclaration();
// ignore imports
if (nameDeclaration == null) {
// ignore imports or methods
if (nameDeclaration == null || !(nameDeclaration instanceof VariableNameDeclaration)) {
return super.visit(node, data);
}

View File

@ -74,6 +74,22 @@ public class Foo {
public void call() {
LOGGER.error("params {} and {}", "arg1", "arg2", new IllegalStateException("Extra arg"));
}
}
]]></code>
</test-code>
<test-code>
<description>Class cast exception with Method calls</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class MethodCallClassCastExceptionProblem {
public void foo() {
// a method call
otherMethod();
}
private void otherMethod() {
}
}
]]></code>
</test-code>