1
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
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
64
65
66
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
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 }