Checked in Piar's implementation of AvoidUsingOctalValues

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4790 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2006-11-14 00:50:40 +00:00
parent 79c7222547
commit 2c4bdd28aa
6 changed files with 115 additions and 40 deletions

View File

@ -1,6 +1,6 @@
?????, 2006 - 3.9:
New rules:
Basic ruleset: BigIntegerInstantiation(1.4 and 1.5)
Basic ruleset: BigIntegerInstantiation(1.4 and 1.5), AvoidUsingOctalValues
Codesize ruleset: NPathComplexity, NcssTypeCount, NcssMethodCount, NcssConstructorCount
Design ruleset: UseCollectionIsEmpty
Strings ruleset: StringBufferInstantiationWithChar

View File

@ -0,0 +1,40 @@
package test.net.sourceforge.pmd.rules;
import net.sourceforge.pmd.PMD;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSetNotFoundException;
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
import test.net.sourceforge.pmd.testframework.TestDescriptor;
public class AvoidUsingOctalValuesTest extends SimpleAggregatorTst {
private Rule rule;
public void setUp() throws RuleSetNotFoundException {
rule = findRule("basic", "AvoidUsingOctalValues");
}
public void testAll() {
runTests(new TestDescriptor[]{
new TestDescriptor(TEST1, "bad, 012", 1, rule),
new TestDescriptor(TEST2, "OK, hex value", 0, rule),
new TestDescriptor(TEST3, "OK, long value", 0, rule),
});
}
private static final String TEST1 =
"public class Foo {" + PMD.EOL +
" int x = 012;" + PMD.EOL +
"}";
private static final String TEST2 =
"public class Foo {" + PMD.EOL +
" int x = 0xCAFE;" + PMD.EOL +
"}";
private static final String TEST3 =
"public class Foo {" + PMD.EOL +
" long x = 0L;" + PMD.EOL +
"}";
}

View File

@ -1073,14 +1073,10 @@ or @Image='"1"' or @Image=0 or
<example>
<![CDATA[
public class Test {
public static void main(String[] args) {
BigInteger bi=new BigInteger(1);
BigInteger bi2=new BigInteger("0");
BigInteger bi3=new BigInteger(0.0);
BigInteger bi4;
bi4=new BigInteger(0);
}
@ -1088,5 +1084,39 @@ public class Test {
]]>
</example>
</rule>
<rule name="AvoidUsingOctalValues"
message="Do not start a literal by 0 unless it's an octal value"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#AvoidUsingOctalValues">
<description>
Integer literals should not start with zero.
Zero means that the rest of literal will be interpreted as an octal value.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Literal
[(starts-with(@Image, 0))]
[string-length(@Image)>1]
[not(substring(@Image, 2, 1) = 'x')]
[not(substring(@Image, 2, 1) = 'L')]
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
int i = 012; // set i with 10 not 12
int j = 010; // set j with 8 not 10
k = i * j; // set k with 80 not 120
}
]]>
</example>
</rule>
</ruleset>

View File

@ -7,6 +7,8 @@ This ruleset contains links to rules that are new in PMD v3.9
</description>
<rule ref="rulesets/basic.xml/BigIntegerInstantiation_1.5"/>
<rule ref="rulesets/basic.xml/AvoidUsingOctalValues"/>
<rule ref="rulesets/strings.xml/StringBufferInstantiationWithChar"/>
<rule ref="rulesets/codesize.xml/NPathComplexity"/>
<rule ref="rulesets/design.xml/UseCollectionIsEmpty"/>
<rule ref="rulesets/codesize.xml/NcssMethodCount"/>

View File

@ -10,41 +10,44 @@ These are new rules that are still in progress.
</description>
<rule name="StringBufferInstantiationWithChar"
message="Do not instantiate a StringBuffer with a char"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/strings.html#StringBufferInstantiationWithChar">
<description>
StringBuffer sb = new StringBuffer('c'); The
char will be converted into int to intialize
StringBuffer size.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//AllocationExpression/ClassOrInterfaceType
[@Image='StringBuffer']
/../Arguments/ArgumentList/Expression/PrimaryExpression
/PrimaryPrefix/
Literal [(starts-with(@Image, "'"))][(ends-with
(@Image, "'"))]
<rule name="AvoidUsingOctalValues"
message="Do not start a literal by 0 unless it's an octal value"
class="net.sourceforge.pmd.rules.XPathRule"
externalInfoUrl="http://pmd.sourceforge.net/rules/basic.html#AvoidUsingOctalValues">
<description>
Integers literals should not start with zero.
Zero means that the rest of literal have to be
interprated as an octal.
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Literal
[(starts-with(@Image, 0))]
[not(substring(@Image, 2, 1) = 'x')]
[not(substring(@Image, 2, 1) = 'L')]
[string-length(@Image)>1]
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
...
int i = 012; // set i with 10 not 12
int j = 010; // set j with 8 not 10
...
k = i * j; // set k with 80 not 120
..
}
]]>
</example>
</rule>
]]>
</value>
</property>
</properties>
<priority>4</priority>
<example>
<![CDATA[
class Foo {
StringBuffer sb1 = new StringBuffer('c'); //Bad
StringBuffer sb2 = new StringBuffer("c"); //Better
}
]]>
</example>
</rule>
<!--
<rule name="UselessAssignment"
message="This assignment to ''{0}'' is useless"

View File

@ -52,7 +52,7 @@
</subsection>
<subsection name="Contributors">
<ul>
<li>piair - Implemented StringBufferInstantiationWithChar</li>
<li>piair - Implemented StringBufferInstantiationWithChar, AvoidUsingOctalValues</li>
<li>Christopher Eagan - Reported bug in VariableNamingConventions</li>
<li>Jason Bennett - Noticed useless line in XSLT scripts, fix for UnnecessaryLocalBeforeReturn, wrote NPathComplexity rule, patches to improve CyclomaticComplexity rule, Implemented: UseCollectionIsEmpty, NcssTypeCount, NcssMethodCount, NcssConstructor</li>
<li><a href="http://www.livejournal.com/users/insac/">Fabio Insaccanebbia</a> - Improvement for UseArraysAsList, UnusedNullCheckInEquals, MisplacedNullCheck, UselessOperationOnImmutable, AvoidArrayLoops, UseArraysAsList, AvoidConstantsInterface, AvoidDecimalLiteralsInBigDecimalConstructor, ClassCastExceptionWithToArray, BigIntegerInstantiation_1.4, BigIntegerInstantiation_1.5</li>