01: package net.sourceforge.pmd.rules.basic;
02:
03: import java.util.regex.Pattern;
04:
05: import net.sourceforge.pmd.AbstractRule;
06: import net.sourceforge.pmd.PropertyDescriptor;
07: import net.sourceforge.pmd.ast.ASTLiteral;
08: import net.sourceforge.pmd.properties.BooleanProperty;
09:
10: public class AvoidUsingOctalValues extends AbstractRule {
11:
12: public static final Pattern OCTAL_PATTERN = Pattern
13: .compile("0[0-7]{2,}[lL]?");
14:
15: public static final Pattern STRICT_OCTAL_PATTERN = Pattern
16: .compile("0[0-7]+[lL]?");
17:
18: private static final PropertyDescriptor strictMethodsDescriptor = new BooleanProperty(
19: "strict", "Detect violations for 00 to 07.", false, 1.0f);
20:
21: public Object visit(ASTLiteral node, Object data) {
22: boolean strict = getBooleanProperty(strictMethodsDescriptor);
23: Pattern p = strict ? STRICT_OCTAL_PATTERN : OCTAL_PATTERN;
24:
25: String img = node.getImage();
26: if (img != null && p.matcher(img).matches()) {
27: addViolation(data, node);
28: }
29:
30: return data;
31: }
32:
33: }
|