View Javadoc

1   /* Generated By:JJTree: Do not edit this line. ASTLiteral.java */
2   
3   package net.sourceforge.pmd.lang.java.ast;
4   
5   import java.util.regex.Pattern;
6   
7   public class ASTLiteral extends AbstractJavaTypeNode {
8   
9       private boolean isInt;
10      private boolean isFloat;
11      private boolean isChar;
12      private boolean isString;
13  
14      public ASTLiteral(int id) {
15          super(id);
16      }
17  
18      public ASTLiteral(JavaParser p, int id) {
19          super(p, id);
20      }
21  
22      /**
23       * Accept the visitor. *
24       */
25      @Override
26      public Object jjtAccept(JavaParserVisitor visitor, Object data) {
27          return visitor.visit(this, data);
28      }
29  
30      public void setIntLiteral() {
31          this.isInt = true;
32      }
33  
34      public boolean isIntLiteral() {
35          return isInt;
36      }
37  
38      public void setFloatLiteral() {
39          this.isFloat = true;
40      }
41  
42      public boolean isFloatLiteral() {
43          return isFloat;
44      }
45  
46      public void setCharLiteral() {
47          this.isChar = true;
48      }
49  
50      public boolean isCharLiteral() {
51          return isChar;
52      }
53  
54      public void setStringLiteral() {
55          this.isString = true;
56      }
57  
58      public boolean isStringLiteral() {
59          return isString;
60      }
61  
62      /**
63       * Returns true if this is a String literal with only one character.
64       * Handles octal and escape characters.
65       *
66       * @return true is this is a String literal with only one character
67       */
68      public boolean isSingleCharacterStringLiteral() {
69          if (isString) {
70              String image = getImage();
71              int length = image.length();
72              if (length == 3) {
73                  return true;
74              } else if (image.charAt(1) == '\\') {
75                  return SINGLE_CHAR_ESCAPE_PATTERN.matcher(image).matches();
76              }
77          }
78          return false;
79      }
80  
81      /**
82       * Pattern used to detect a single escaped character or octal character in a String.
83       */
84      private static final Pattern SINGLE_CHAR_ESCAPE_PATTERN = Pattern
85              .compile("^\"\\\\(([ntbrf\\\\'\\\"])|([0-7][0-7]?)|([0-3][0-7][0-7]))\"");
86  
87  }