From 7c2f7bf62cc48fac20bb803f20da0ac9239d9048 Mon Sep 17 00:00:00 2001 From: Xavier Le Vourch Date: Thu, 30 Nov 2006 18:58:13 +0000 Subject: [PATCH] strict boolean property to check for 00 to 07, off by default git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@4843 51baf565-9d33-0410-a72c-fc3788e3496d --- .../pmd/rules/AvoidUsingOctalValuesTest.java | 39 +------- .../pmd/rules/xml/AvoidUsingOctalValues.xml | 92 +++++++++++++++++++ .../rules/basic/AvoidUsingOctalValues.java | 15 ++- 3 files changed, 106 insertions(+), 40 deletions(-) create mode 100644 pmd/regress/test/net/sourceforge/pmd/rules/xml/AvoidUsingOctalValues.xml diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/AvoidUsingOctalValuesTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/AvoidUsingOctalValuesTest.java index 8b938e4b74..75637600ef 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/AvoidUsingOctalValuesTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/AvoidUsingOctalValuesTest.java @@ -15,44 +15,7 @@ public class AvoidUsingOctalValuesTest extends SimpleAggregatorTst { } 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), - new TestDescriptor(TEST4, "OK, double value", 0, rule), - new TestDescriptor(TEST5, "OK, double value", 0, rule), - new TestDescriptor(TEST6, "bad, 012L", 1, rule), - }); + runTests(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 + - "}"; - - private static final String TEST4 = - "public class Foo {" + PMD.EOL + - " double d = 0.1;" + PMD.EOL + - "}"; - - private static final String TEST5 = - "public class Foo {" + PMD.EOL + - " float f = 0f;" + PMD.EOL + - "}"; - - private static final String TEST6 = - "public class Foo {" + PMD.EOL + - " long x = 012L;" + PMD.EOL + - "}"; - } diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/xml/AvoidUsingOctalValues.xml b/pmd/regress/test/net/sourceforge/pmd/rules/xml/AvoidUsingOctalValues.xml new file mode 100644 index 0000000000..ec3ff9593b --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/rules/xml/AvoidUsingOctalValues.xml @@ -0,0 +1,92 @@ + + + + + 1 + + + + + 0 + + + + + 0 + + + + + 0 + + + + + 0 + + + + + 1 + + + + + 0 + + + + + 1 + true + + + diff --git a/pmd/src/net/sourceforge/pmd/rules/basic/AvoidUsingOctalValues.java b/pmd/src/net/sourceforge/pmd/rules/basic/AvoidUsingOctalValues.java index 3e2a33f870..6721435032 100644 --- a/pmd/src/net/sourceforge/pmd/rules/basic/AvoidUsingOctalValues.java +++ b/pmd/src/net/sourceforge/pmd/rules/basic/AvoidUsingOctalValues.java @@ -3,15 +3,26 @@ package net.sourceforge.pmd.rules.basic; import java.util.regex.Pattern; import net.sourceforge.pmd.AbstractRule; +import net.sourceforge.pmd.PropertyDescriptor; import net.sourceforge.pmd.ast.ASTLiteral; +import net.sourceforge.pmd.properties.BooleanProperty; public class AvoidUsingOctalValues extends AbstractRule { - public static final Pattern OCTAL_PATTERN = Pattern.compile("0[0-7]+[lL]?"); + public static final Pattern OCTAL_PATTERN = Pattern.compile("0[0-7]{2,}[lL]?"); + + public static final Pattern STRICT_OCTAL_PATTERN = Pattern.compile("0[0-7]+[lL]?"); + + private final PropertyDescriptor strictMethodsDescriptor = new BooleanProperty( + "strict", "Detect violations for 00 to 07.", false, 1.0f + ); public Object visit(ASTLiteral node, Object data) { + boolean strict = getBooleanProperty(strictMethodsDescriptor); + Pattern p = strict?STRICT_OCTAL_PATTERN:OCTAL_PATTERN; + String img = node.getImage(); - if (img != null && OCTAL_PATTERN.matcher(img).matches()) { + if (img != null && p.matcher(img).matches()) { addViolation(data, node); }