View Javadoc

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