Applied patch 3130615: New Rule GuardDebugLogging for jakarta-commons logging. Thanks to Tammo van Lessen.

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@7355 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Andreas Dangel
2011-09-26 19:40:51 +00:00
parent 73c258e712
commit 165bb3a24c
6 changed files with 106 additions and 1 deletions

View File

@ -459,6 +459,7 @@ New Java rules:
StrictException ruleset: AvoidThrowingNewInstanceOfSameException, AvoidCatchingGenericException, AvoidLosingExceptionInformation
Unnecessary ruleset: UselessParentheses
JUnit ruleset: JUnitTestContainsTooManyAsserts, UseAssertTrueInsteadOfAssertEquals
Logging with Jakarta Commons ruleset: GuardDebugLogging
New Java ruleset:
android.xml: new rules specific to the Android platform

View File

@ -12,6 +12,7 @@ public class LoggingJakartaCommonsRulesTest extends SimpleAggregatorTst {
public void setUp() {
addRule(RULESET, "ProperLogger");
addRule(RULESET, "UseCorrectExceptionLogging");
addRule(RULESET, "GuardDebugLogging");
}
public static junit.framework.Test suite() {

View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
ok
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Test {
private static final Log __log = LogFactory.getLog(Test.class);
public void test() {
// okay:
__log.debug("log something");
// okay:
__log.debug("log something with exception", e);
// good:
if (__log.isDebugEnabled()) {
__log.debug("bla" + "",e );
}
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
Complex logging without guard
]]></description>
<expected-problems>2</expected-problems>
<code><![CDATA[
public class Test {
private static final Log __log = LogFactory.getLog(Test.class);
public void test() {
// okay:
__log.debug("log something");
// okay:
__log.debug("log something with exception", e);
// bad:
__log.debug("log something" + " and " + "concat strings");
// bad:
__log.debug("log something" + " and " + "concat strings", e);
// good:
if (__log.isDebugEnabled()) {
__log.debug("bla" + "",e );
}
}
}
]]></code>
</test-code>
</test-data>

View File

@ -88,5 +88,51 @@ public class Foo {
]]>
</example>
</rule>
<rule name="GuardDebugLogging"
language="Java"
since="4.3"
message="debug logging that involves string concatenation should be guarded with isDebugEnabled() checks"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/java/logging-jakarta-commons.html#GuardDebugLogging">
<description>
When log messages are composed by concatenating strings, the whole section should be guarded
by a isDebugEnabled() check to avoid performance and memory issues.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryPrefix[ends-with(Name/@Image, '.debug') and count(../descendant::AdditiveExpression) > 0 and count(ancestor::IfStatement/descendant::PrimaryExpression[ends-with(descendant::PrimaryPrefix/Name/@Image, 'isDebugEnabled')]) = 0]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Test {
private static final Log __log = LogFactory.getLog(Test.class);
public void test() {
// okay:
__log.debug("log something");
// okay:
__log.debug("log something with exception", e);
// bad:
__log.debug("log something" + " and " + "concat strings");
// bad:
__log.debug("log something" + " and " + "concat strings", e);
// good:
if (__log.isDebugEnabled()) {
__log.debug("bla" + "",e );
}
}
}
]]>
</example>
</rule>
</ruleset>

View File

@ -40,6 +40,7 @@ This ruleset contains links to rules that are new in PMD v5.0
<rule ref="rulesets/java/imports.xml/UnnecessaryFullyQualifiedName"/>
<rule ref="rulesets/java/junit.xml/JUnitTestContainsTooManyAsserts"/>
<rule ref="rulesets/java/junit.xml/UseAssertTrueInsteadOfAssertEquals"/>
<rule ref="rulesets/java/logging-jakarta-commons.xml/GuardDebugLogging"/>
<rule ref="rulesets/java/naming.xml/ShortClassName"/>
<rule ref="rulesets/java/optimizations.xml/RedundantFieldInitializer"/>
<rule ref="rulesets/java/strictexception.xml/AvoidCatchingGenericException"/>

View File

@ -321,6 +321,7 @@
<li>Lucian Ciufudean - RedundantFieldInitializerRule</li>
<li>Andreas Dangel - GodClass and LawOfDemeter rules, several bugfixes and cleanup</li>
<li>Riku Nykanen - patch improving TooManyMethods rule</li>
<li>Tammo van Lessen - new rule GuardDebugLogging for Jakarta Commons Logging ruleset.</li>
</ul>
</subsection>
<subsection name="Organizations">