forked from phoedos/pmd
BigIntegerInstantiation rules merged into one
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4840 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
?????, 2006 - 3.9:
|
||||
New rules:
|
||||
Basic ruleset: BigIntegerInstantiation(1.4 and 1.5), AvoidUsingOctalValues
|
||||
Basic ruleset: BigIntegerInstantiation, AvoidUsingOctalValues
|
||||
Codesize ruleset: NPathComplexity, NcssTypeCount, NcssMethodCount, NcssConstructorCount
|
||||
Design ruleset: UseCollectionIsEmpty
|
||||
Strings ruleset: StringBufferInstantiationWithChar
|
||||
|
@ -7,16 +7,13 @@ import net.sourceforge.pmd.Rule;
|
||||
import test.net.sourceforge.pmd.testframework.SimpleAggregatorTst;
|
||||
|
||||
public class BigIntegerInstantiationTest extends SimpleAggregatorTst {
|
||||
private Rule rule14;
|
||||
private Rule rule15;
|
||||
private Rule rule;
|
||||
|
||||
public void setUp() {
|
||||
rule14 = findRule("basic", "BigIntegerInstantiation_1.4");
|
||||
rule15 = findRule("basic", "BigIntegerInstantiation_1.5");
|
||||
rule = findRule("basic", "BigIntegerInstantiation");
|
||||
}
|
||||
|
||||
public void testAll() {
|
||||
runTests(rule14);
|
||||
runTests(rule15);
|
||||
runTests(rule);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,105 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<test-data>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Fail, BigInteger(1)
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigInteger b = new BigInteger("1");
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Pass, BigInteger(10)
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigInteger b = new BigInteger("10");
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Fail, BigInteger(0)
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigInteger b = new BigInteger("0");
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Pass, BigInteger("10") and BigDecimal in 1.4 mode
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
void test14() {
|
||||
BigInteger b = new BigInteger("10");
|
||||
BigDecimal d;
|
||||
d = new BigDecimal(0);
|
||||
d = new BigDecimal(1);
|
||||
d = new BigDecimal(10);
|
||||
d = new BigDecimal("0");
|
||||
d = new BigDecimal("1");
|
||||
d = new BigDecimal("10");
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
<source-type>java 1.4</source-type>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Fail, BigInteger(10) 1.5 mode
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigInteger b = new BigInteger("10");
|
||||
}
|
||||
]]></code>
|
||||
<source-type>java 1.5</source-type>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Fail, BigDecimal(1)
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigDecimal b = new BigDecimal(1);
|
||||
}
|
||||
]]></code>
|
||||
<source-type>java 1.5</source-type>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Fail, BigDecimal(10)
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigDecimal b = new BigDecimal(10);
|
||||
}
|
||||
]]></code>
|
||||
<source-type>java 1.5</source-type>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Fail, BigDecimal(0)
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigDecimal b = new BigDecimal(0);
|
||||
}
|
||||
]]></code>
|
||||
<source-type>java 1.5</source-type>
|
||||
</test-code>
|
||||
</test-data>
|
@ -1,36 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<test-data>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Fail, BigInteger(1)
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigInteger b = new BigInteger(1);
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Pass, BigInteger(10)
|
||||
]]></description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigInteger b = new BigInteger(10);
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Fail, BigInteger(0)
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigInteger b = new BigInteger(0);
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
@ -1,36 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<test-data>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Fail, BigInteger(1)
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigInteger b = new BigInteger(1);
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Pass, BigInteger(10)
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigInteger b = new BigInteger(10);
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
<test-code>
|
||||
<description><![CDATA[
|
||||
Fail, BigInteger(0)
|
||||
]]></description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
BigInteger b = new BigInteger(0);
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
</test-data>
|
@ -1002,77 +1002,21 @@ class Foo {
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="BigIntegerInstantiation_1.5"
|
||||
<rule name="BigIntegerInstantiation"
|
||||
message="Don't create instances of already existing
|
||||
BigInteger and BigDecimal (ZERO, ONE, TEN)"
|
||||
class="net.sourceforge.pmd.rules.XPathRule">
|
||||
class="net.sourceforge.pmd.rules.basic.BigIntegerInstantiation">
|
||||
<description>
|
||||
Don't create instances of already existing BigInteger
|
||||
(BigInteger.ZERO, BigInteger.ONE, BigInteger.TEN)
|
||||
and BigDecimal (BigDecimal.ZERO, BigDecimal.ONE,
|
||||
BigDecimal.TEN)
|
||||
(BigInteger.ZERO, BigInteger.ONE) and for 1.5 on,
|
||||
BigInteger.TEN and BigDecimal (BigDecimal.ZERO,
|
||||
BigDecimal.ONE, BigDecimal.TEN)
|
||||
</description>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//Expression[./PrimaryExpression/PrimaryPrefix/AllocationExpression[./ClassOrInterfaceType[@Image="BigInteger"
|
||||
or
|
||||
@Image="BigDecimal"]]/Arguments/ArgumentList[count(./Expression)=1]/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image=1
|
||||
or @Image='"1"' or @Image=0 or
|
||||
@Image='"0"' or @Image=10 or
|
||||
@Image='"10"']]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<priority>3</priority>
|
||||
<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(10);
|
||||
|
||||
BigDecimal bd=new BigDecimal(1);
|
||||
|
||||
BigDecimal bd2=new BigDecimal(0);
|
||||
|
||||
BigDecimal bd3=new BigDecimal(10);
|
||||
|
||||
}
|
||||
}
|
||||
]]>
|
||||
</example>
|
||||
</rule>
|
||||
|
||||
<rule name="BigIntegerInstantiation_1.4"
|
||||
message="Don't create instances of already existing
|
||||
BigInteger (BigInteger.ZERO, BigInteger.ONE)"
|
||||
class="net.sourceforge.pmd.rules.XPathRule">
|
||||
<description>
|
||||
Don't create instances of already existing BigInteger
|
||||
(BigInteger.ZERO, BigInteger.ONE)
|
||||
</description>
|
||||
<properties>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//Expression[./PrimaryExpression/PrimaryPrefix/AllocationExpression[./ClassOrInterfaceType[@Image="BigInteger"]]/Arguments/ArgumentList[count(./Expression)=1]/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image=1
|
||||
or @Image='"1"' or @Image=0 or
|
||||
@Image='"0"']]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<priority>3</priority>
|
||||
<example>
|
||||
<![CDATA[
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
BigInteger bi=new BigInteger(1);
|
||||
BigInteger bi2=new BigInteger("0");
|
||||
|
@ -6,7 +6,7 @@
|
||||
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/BigIntegerInstantiation"/>
|
||||
<rule ref="rulesets/basic.xml/AvoidUsingOctalValues"/>
|
||||
<rule ref="rulesets/strings.xml/StringBufferInstantiationWithChar"/>
|
||||
<rule ref="rulesets/codesize.xml/NPathComplexity"/>
|
||||
|
@ -0,0 +1,50 @@
|
||||
package net.sourceforge.pmd.rules.basic;
|
||||
|
||||
import net.sourceforge.pmd.AbstractRule;
|
||||
import net.sourceforge.pmd.RuleContext;
|
||||
import net.sourceforge.pmd.SourceType;
|
||||
import net.sourceforge.pmd.ast.ASTAllocationExpression;
|
||||
import net.sourceforge.pmd.ast.ASTArguments;
|
||||
import net.sourceforge.pmd.ast.ASTArrayDimsAndInits;
|
||||
import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
|
||||
import net.sourceforge.pmd.ast.ASTLiteral;
|
||||
import net.sourceforge.pmd.ast.Node;
|
||||
|
||||
public class BigIntegerInstantiation extends AbstractRule {
|
||||
|
||||
public Object visit(ASTAllocationExpression node, Object data) {
|
||||
Node type = node.jjtGetChild(0);
|
||||
|
||||
if (!(type instanceof ASTClassOrInterfaceType)) {
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
String img = ((ASTClassOrInterfaceType) type).getImage();
|
||||
if (img.startsWith("java.math.")) {
|
||||
img = img.substring(10);
|
||||
}
|
||||
|
||||
boolean jdk15 = ((RuleContext) data).getSourceType().compareTo(SourceType.JAVA_15) >= 0;
|
||||
|
||||
if (("BigInteger".equals(img) || (jdk15 && "BigDecimal".equals(img))) &&
|
||||
(node.getFirstChildOfType(ASTArrayDimsAndInits.class) == null)
|
||||
) {
|
||||
ASTArguments args = (ASTArguments) node.getFirstChildOfType(ASTArguments.class);
|
||||
if (args.getArgumentCount() == 1) {
|
||||
ASTLiteral literal = (ASTLiteral) node.getFirstChildOfType(ASTLiteral.class);
|
||||
|
||||
img = literal.getImage();
|
||||
if ((img.length() > 2 && img.charAt(0) == '"')) {
|
||||
img = img.substring(1, img.length() - 1);
|
||||
}
|
||||
|
||||
if ("0".equals(img) || "1".equals(img) || (jdk15 && "10".equals(img))) {
|
||||
addViolation(data, node);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
return super.visit(node, data);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user