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
This commit is contained in:
Xavier Le Vourch
2006-11-30 18:58:13 +00:00
parent 26907e0d0c
commit 7c2f7bf62c
3 changed files with 106 additions and 40 deletions

View File

@ -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 +
"}";
}

View File

@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description><![CDATA[
bad, 012
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
int x = 012;
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
OK, hex value
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
int x = 0xCAFE;
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
OK, long value
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
long x = 0L;
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
OK, double value
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
double d = 0.1;
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
OK, double value
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
float f = 0f;
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
bad, 012L
]]></description>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
long x = 012L;
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
OK, 06 if strict is not set
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
int x = 06;
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
BAD, 06 if strict is set
]]></description>
<expected-problems>1</expected-problems>
<rule-property name="strict">true</rule-property>
<code><![CDATA[
public class Foo {
int x = 06;
}
]]></code>
</test-code>
</test-data>

View File

@ -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);
}