[java] Update rule UseCorrectExceptionLogging

This commit is contained in:
Andreas Dangel
2021-08-20 20:04:13 +02:00
parent 7103b7f96d
commit 542703b02e
5 changed files with 38 additions and 17 deletions

View File

@@ -267,7 +267,7 @@
<!-- <rule ref="category/java/errorprone.xml/UnnecessaryCaseChange"/> -->
<!-- <rule ref="category/java/errorprone.xml/UnnecessaryConversionTemporary"/> -->
<!-- <rule ref="category/java/errorprone.xml/UnusedNullCheckInEquals"/> -->
<!-- <rule ref="category/java/errorprone.xml/UseCorrectExceptionLogging"/> -->
<rule ref="category/java/errorprone.xml/UseCorrectExceptionLogging"/>
<!-- <rule ref="category/java/errorprone.xml/UseEqualsToCompareStrings"/> -->
<!-- <rule ref="category/java/errorprone.xml/UseLocaleWithCaseConversions"/> -->
<!-- <rule ref="category/java/errorprone.xml/UseProperClassLoader"/> -->

View File

@@ -267,5 +267,11 @@
<version>3.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -3359,23 +3359,17 @@ public class Test {
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_errorprone.html#usecorrectexceptionlogging">
<description>
To make sure the full stacktrace is printed out, use the logging statement with two arguments: a String and a Throwable.
This rule only applies to [Apache Commons Logging](https://commons.apache.org/proper/commons-logging/).
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//CatchStatement/Block/BlockStatement/Statement/StatementExpression
/PrimaryExpression
[PrimaryPrefix/Name
[starts-with(@Image,
concat((ancestor::ClassOrInterfaceDeclaration/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/FieldDeclaration
[Type//ClassOrInterfaceType[@Image='Log']]
/VariableDeclarator/VariableDeclaratorId/@Name)[1], '.'))
]
]
[PrimarySuffix/Arguments[@Size= 1]]
[PrimarySuffix/Arguments//Name/@Image = ancestor::CatchStatement/FormalParameter/VariableDeclaratorId/@Name]
//CatchClause/Block//MethodCall
[pmd-java:matchesSig('org.apache.commons.logging.Log#_(java.lang.Object)')]
[ArgumentList[@Size=1]/VariableAccess/@Name = ancestor::CatchClause/CatchParameter/VariableDeclaratorId/@Name]
]]>
</value>
</property>

View File

@@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.errorprone;
import net.sourceforge.pmd.testframework.PmdRuleTst;
@org.junit.Ignore("Rule has not been updated yet")
public class UseCorrectExceptionLoggingTest extends PmdRuleTst {
// no additional unit tests
}

View File

@@ -8,12 +8,15 @@
<description>ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Foo {
static final Log _LOG = LogFactory.getLog( Foo.class );
void foo() {
try {
} catch (OtherException oe) {
_LOG.error(oe.getMessage(), oe);
} catch (IllegalArgumentException iae) {
_LOG.error(iae.getMessage(), iae);
}
}
}
@@ -23,7 +26,11 @@ public class Foo {
<test-code>
<description>failure case - two calls</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>9,10</expected-linenumbers>
<code><![CDATA[
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Foo {
static final Log _LOG = LogFactory.getLog( Foo.class );
void foo() {
@@ -41,6 +48,9 @@ public class Foo {
<description>must be in a catch block</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Foo {
static final Log _LOG = LogFactory.getLog( Foo.class );
void foo(int e) {
@@ -52,8 +62,12 @@ public class Foo {
<test-code>
<description>bug 1626232, the rule should not be confused by inner classes</description>
<expected-problems>0</expected-problems>
<expected-problems>2</expected-problems>
<expected-linenumbers>13,14</expected-linenumbers>
<code><![CDATA[
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Foo {
static class Inner {
Log _LOG = LogFactory.getLog( Foo.class );
@@ -62,7 +76,7 @@ public class Foo {
void foo() {
try {
} catch (Exception e) {
Log _LOG = LogFactory.getLog( Main.class );
Log _LOG = LogFactory.getLog( Foo.class );
_LOG.error(e);
_LOG.info(e);
}
@@ -74,7 +88,11 @@ public class Foo {
<test-code>
<description>bug 1626232, should work with a static block</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>12</expected-linenumbers>
<code><![CDATA[
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class Foo {
static final Log _LOG = LogFactory.getLog( Foo.class );
@@ -94,7 +112,11 @@ public class Foo {
<description>XPath problem: A sequence of more than one item is not allowed as the first argument of concat()</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class UseCorrectExceptionLoggingCase {
private static final String DISCONNECTED_CLIENT_LOG_CATEGORY = "thecategory";
protected static final Log logger1 = LogFactory.getLog(DISCONNECTED_CLIENT_LOG_CATEGORY);
protected final Log logger2 = LogFactory.getLog(getClass());