View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.basic;
5   
6   import java.util.regex.Pattern;
7   
8   import net.sourceforge.pmd.lang.java.ast.ASTLiteral;
9   import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
10  import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
11  
12  public class AvoidUsingOctalValuesRule extends AbstractJavaRule {
13  
14      public static final Pattern OCTAL_PATTERN = Pattern.compile("0[0-7]{2,}[lL]?");
15  
16      public static final Pattern STRICT_OCTAL_PATTERN = Pattern.compile("0[0-7]+[lL]?");
17  
18      private static final BooleanProperty STRICT_METHODS_DESCRIPTOR = new BooleanProperty(
19              "strict", "Detect violations between 00 and 07", false, 1.0f
20              );
21  
22      public AvoidUsingOctalValuesRule() {
23  	definePropertyDescriptor(STRICT_METHODS_DESCRIPTOR);
24      }
25  
26      public Object visit(ASTLiteral node, Object data) {
27          boolean strict = getProperty(STRICT_METHODS_DESCRIPTOR);
28          Pattern p = strict ? STRICT_OCTAL_PATTERN : OCTAL_PATTERN;
29  
30          String img = node.getImage();
31          if (img != null && p.matcher(img).matches()) {
32              addViolation(data, node);
33          }
34  
35          return data;
36      }
37  }