forked from phoedos/pmd
Merge branch 'pr-1504'
This commit is contained in:
@ -50,6 +50,8 @@ This means, you can use CPD to find duplicated code in your Kotlin projects.
|
||||
* java-design
|
||||
* [#1151](https://github.com/pmd/pmd/issues/1151): \[java] ImmutableField false positive with multiple constructors
|
||||
* [#1483](https://github.com/pmd/pmd/issues/1483): \[java] Cyclo metric should count conditions of for statements correctly
|
||||
* java-errorprone
|
||||
* [#1512](https://github.com/pmd/pmd/issues/1512): \[java] InvalidSlf4jMessageFormatRule causes NPE in lambda and static blocks
|
||||
* plsql
|
||||
* [#1454](https://github.com/pmd/pmd/issues/1454): \[plsql] ParseException for IF/CASE statement with >=, <=, !=
|
||||
|
||||
@ -258,6 +260,7 @@ removal:
|
||||
* [#1464](https://github.com/pmd/pmd/pull/1464): \[doc] Fix XSS on documentation web page - [Maxime Robert](https://github.com/marob)
|
||||
* [#1469](https://github.com/pmd/pmd/pull/1469): \[core] Configurable max loops in DAAPathFinder - [Alberto Fernández](https://github.com/albfernandez)
|
||||
* [#1494](https://github.com/pmd/pmd/pull/1494): \[java] 1151: Rephrase ImmutableField documentation in design.xml - [Robbie Martinus](https://github.com/rmartinus)
|
||||
* [#1504](https://github.com/pmd/pmd/pull/1504): \[java] NPE in InvalidSlf4jMessageFormatRule if a logger call with a variable as parameter is not inside a method or constructor - [kris-scheibe](https://github.com/kris-scheibe)
|
||||
|
||||
{% endtocmaker %}
|
||||
|
||||
|
@ -22,6 +22,8 @@ import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBody;
|
||||
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.ASTInitializer;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTName;
|
||||
@ -31,6 +33,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId;
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer;
|
||||
import net.sourceforge.pmd.lang.java.ast.JavaNode;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
|
||||
import net.sourceforge.pmd.lang.java.typeresolution.TypeHelper;
|
||||
@ -163,11 +166,12 @@ public class InvalidSlf4jMessageFormatRule extends AbstractJavaRule {
|
||||
count = countPlaceholders(node);
|
||||
} 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> localVariables = node
|
||||
.getFirstParentOfType(ASTMethodOrConstructorDeclaration.class)
|
||||
.findDescendantsOfType(ASTVariableDeclarator.class);
|
||||
count = getAmountOfExpectedArguments(variableName, localVariables);
|
||||
// look if the message is defined locally in a method/constructor, initializer block or lambda expression
|
||||
final JavaNode parentBlock = node.getFirstParentOfAnyType(ASTMethodOrConstructorDeclaration.class, ASTInitializer.class, ASTLambdaExpression.class);
|
||||
if (parentBlock != null) {
|
||||
final List<ASTVariableDeclarator> localVariables = parentBlock.findDescendantsOfType(ASTVariableDeclarator.class);
|
||||
count = getAmountOfExpectedArguments(variableName, localVariables);
|
||||
}
|
||||
|
||||
if (count == 0) {
|
||||
// look if the message is defined in a field
|
||||
|
@ -269,4 +269,70 @@ public final class Main {
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>NPE in static block (see #1512)</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class LoggerHelper {
|
||||
static {
|
||||
Logger logger = LoggerFactory.getLogger(loggerName);
|
||||
logger.info(message);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>missing argument in static block</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>8</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class LoggerHelper {
|
||||
static {
|
||||
final String pattern = "log: {}";
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(loggerName);
|
||||
logger.info(pattern, 1, 2);
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>NPE in lambda call (see #1512)</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class LoggerHelper {
|
||||
final Logger logger = LoggerFactory.getLogger(loggerName);
|
||||
|
||||
final List<String> list = someMethod(message -> logger.info(message));
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>missing argument in lambda call</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>9</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
import org.slf4j.Logger;
|
||||
|
||||
public class LoggerHelper {
|
||||
final Logger logger = LoggerFactory.getLogger(loggerName);
|
||||
|
||||
final List<String> list = someMethod(message -> {
|
||||
final String pattern = "log: {}";
|
||||
|
||||
logger.info(pattern, 1, 2);
|
||||
});
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
Reference in New Issue
Block a user