View Javadoc

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