From 80209cb2416bd637ccdf92c9e36a3ad03ffb3775 Mon Sep 17 00:00:00 2001 From: Xavier Le Vourch Date: Thu, 30 Nov 2006 04:44:20 +0000 Subject: [PATCH] BigIntegerInstantiation rules merged into one git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4840 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 2 +- .../rules/BigIntegerInstantiationTest.java | 9 +- .../pmd/rules/xml/BigIntegerInstantiation.xml | 105 ++++++++++++++++++ .../rules/xml/BigIntegerInstantiation_1.4.xml | 36 ------ .../rules/xml/BigIntegerInstantiation_1.5.xml | 36 ------ pmd/rulesets/basic.xml | 66 +---------- pmd/rulesets/releases/39.xml | 2 +- .../rules/basic/BigIntegerInstantiation.java | 50 +++++++++ 8 files changed, 165 insertions(+), 141 deletions(-) create mode 100644 pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation.xml delete mode 100644 pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation_1.4.xml delete mode 100644 pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation_1.5.xml create mode 100644 pmd/src/net/sourceforge/pmd/rules/basic/BigIntegerInstantiation.java diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 3a0ae22294..b8a9e26e53 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/BigIntegerInstantiationTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/BigIntegerInstantiationTest.java index 74facb4eef..bff42d9a7b 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/BigIntegerInstantiationTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/BigIntegerInstantiationTest.java @@ -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); } } diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation.xml b/pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation.xml new file mode 100644 index 0000000000..f116cd31a0 --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation.xml @@ -0,0 +1,105 @@ + + + + + 1 + + + + + 0 + + + + + 1 + + + + + 0 + + java 1.4 + + + + 1 + + java 1.5 + + + + 1 + + java 1.5 + + + + 1 + + java 1.5 + + + + 1 + + java 1.5 + + diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation_1.4.xml b/pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation_1.4.xml deleted file mode 100644 index 45a5acfc4c..0000000000 --- a/pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation_1.4.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - 1 - - - - - 0 - - - - - 1 - - - diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation_1.5.xml b/pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation_1.5.xml deleted file mode 100644 index ef6a3c4483..0000000000 --- a/pmd/regress/test/net/sourceforge/pmd/rules/xml/BigIntegerInstantiation_1.5.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - 1 - - - - - 1 - - - - - 1 - - - diff --git a/pmd/rulesets/basic.xml b/pmd/rulesets/basic.xml index e664303578..0a5f384b84 100644 --- a/pmd/rulesets/basic.xml +++ b/pmd/rulesets/basic.xml @@ -1002,77 +1002,21 @@ class Foo { - + class="net.sourceforge.pmd.rules.basic.BigIntegerInstantiation"> 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) - - - - - - - 3 - - - - - -Don't create instances of already existing BigInteger -(BigInteger.ZERO, BigInteger.ONE) - - - - - - - - - 3 - - - + diff --git a/pmd/src/net/sourceforge/pmd/rules/basic/BigIntegerInstantiation.java b/pmd/src/net/sourceforge/pmd/rules/basic/BigIntegerInstantiation.java new file mode 100644 index 0000000000..1ab554a503 --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/rules/basic/BigIntegerInstantiation.java @@ -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); + } + +}