View Javadoc

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