Merge branch 'pr-2251'

[java] FP for InvalidLogMessageFormat when using slf4j-Markers

Fixes #2250
This commit is contained in:
Andreas Dangel
2020-01-26 19:56:04 +01:00
3 changed files with 36 additions and 0 deletions

View File

@ -21,10 +21,14 @@ For the changes, see [PMD Designer Changelog](https://github.com/pmd/pmd-designe
### Fixed Issues ### Fixed Issues
* java-errorprone
* [#2250](https://github.com/pmd/pmd/issues/2250): \[java] InvalidLogMessageFormat flags logging calls using a slf4j-Marker
### API Changes ### API Changes
### External Contributions ### External Contributions
* [#2251](https://github.com/pmd/pmd/pull/2251): \[java] FP for InvalidLogMessageFormat when using slf4j-Markers - [Kris Scheibe](https://github.com/kris-scheibe)
* [#2253](https://github.com/pmd/pmd/pull/2253): \[modelica] Remove duplicated dependencies - [Piotrek Żygieło](https://github.com/pzygielo) * [#2253](https://github.com/pmd/pmd/pull/2253): \[modelica] Remove duplicated dependencies - [Piotrek Żygieło](https://github.com/pzygielo)
{% endtocmaker %} {% endtocmaker %}

View File

@ -89,6 +89,11 @@ public class InvalidLogMessageFormatRule extends AbstractJavaRule {
final List<ASTExpression> argumentList = parentNode.getFirstChildOfType(ASTPrimarySuffix.class) final List<ASTExpression> argumentList = parentNode.getFirstChildOfType(ASTPrimarySuffix.class)
.getFirstDescendantOfType(ASTArgumentList.class).findChildrenOfType(ASTExpression.class); .getFirstDescendantOfType(ASTArgumentList.class).findChildrenOfType(ASTExpression.class);
// ignore the first argument if it is a known non-string value, e.g. a slf4j-Marker
if (argumentList.get(0).getType() != null && !argumentList.get(0).getType().equals(String.class)) {
argumentList.remove(0);
}
// remove the message parameter // remove the message parameter
final ASTExpression messageParam = argumentList.remove(0); final ASTExpression messageParam = argumentList.remove(0);
final int expectedArguments = expectedArguments(messageParam); final int expectedArguments = expectedArguments(messageParam);

View File

@ -780,6 +780,33 @@ public class InvalidLogMessageFormatTest {
logger.warn("foo {}", "flibble", "moo", "blah", "blah"); // PMD flags this logger.warn("foo {}", "flibble", "moo", "blah", "blah"); // PMD flags this
logger.warn("foo", "flibble", "moo", "blah", "blah"); // PMD doesn't flag this logger.warn("foo", "flibble", "moo", "blah", "blah"); // PMD doesn't flag this
} }
}
]]></code>
</test-code>
<test-code>
<description>ignore slf4j-Markers when detecting the number of arguments</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>11,17</expected-linenumbers>
<code><![CDATA[
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.helpers.BasicMarkerFactory;
public class InvalidLogMessageFormatTest {
private static final Logger logger = LoggerFactory.getLogger("MyLogger");
private static final Marker marker = BasicMarkerFactory.getMarker("MARKER");
public static void main(String[] args) {
logger.warn(marker, "foo {}", "flibble", "moo", "blah", "blah"); // wrong number of arguments
logger.warn(marker, "foo"); // correct: marker and no arguments
logger.warn(marker, "foo", new Exception()); // correct: marker and one exception parameter
logger.warn(marker, "foo {}", "bar"); // correct: marker and one argument
final var otherMarker = MarkerFactory.getMarker("OTHER_MARKER");
logger.warn(otherMarker, "foo"); // gets flagged as we can't statically determine the type of the "otherMarker" variable
}
} }
]]></code> ]]></code>
</test-code> </test-code>