This commit is contained in:
Clément Fournier
2017-11-09 20:54:03 +01:00
parent b159143a06
commit 947d4ab1a8
2 changed files with 45 additions and 3 deletions

View File

@ -19,7 +19,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTName;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression;
import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
@ -158,9 +158,9 @@ public class InvalidSlf4jMessageFormatRule extends AbstractJavaRule {
} else if (node.getFirstDescendantOfType(ASTName.class) != null) {
final String variableName = node.getFirstDescendantOfType(ASTName.class).getImage();
// look if the message is defined locally
final List<ASTVariableDeclarator> localValiables = node.getFirstParentOfType(ASTMethodDeclaration.class)
final List<ASTVariableDeclarator> localVariables = node.getFirstParentOfType(ASTMethodOrConstructorDeclaration.class)
.findDescendantsOfType(ASTVariableDeclarator.class);
count = getAmountOfExpectedArguments(variableName, localValiables);
count = getAmountOfExpectedArguments(variableName, localVariables);
if (count == 0) {
// look if the message is defined in a field

View File

@ -224,4 +224,46 @@ public class Foo
}
]]></code>
</test-code>
<test-code>
<description>#721 NPE in PMD 5.8.1 InvalidSlf4jMessageFormat</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.slf4j.Logger;
public class LoggerHelper {
public LoggerHelper(int entriesCount, long sleepingInterval, String loggerName, String message) {
Logger logger = LoggerFactory.getLogger(loggerName);
for (int i = 0; i < entriesCount; i++) {
logger.info(message);
try {
Thread.sleep(sleepingInterval);
}
catch(InterruptedException e) {
// nothing to do here!
}
}
}
public static void main(String[] args) {
if (args.length != 4) {
System.err.println("Invalid command line parameters ...");
System.out.println("USAGE: LoggerHelper <count of log entries> <sleeping interval> <logger-name> <message>");
System.exit(-1);
}
int entriesCount = Integer.parseInt(args[0]);
long sleepingInterval = Long.parseLong(args[1]);
String loggerName = args[2];
String logMessage = args[3];
new LoggerHelper(entriesCount, sleepingInterval, loggerName, logMessage);
}
}
]]></code>
</test-code>
</test-data>