1
2
3
4
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
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 String image = getImage();
39 if (isInt && image != null && image.length() > 0) {
40 if (!image.endsWith("l") && !image.endsWith("L")) {
41 return true;
42 }
43 }
44 return false;
45 }
46
47
48
49
50
51 public boolean isLongLiteral() {
52 String image = getImage();
53 if (isInt && image != null && image.length() > 0) {
54 if (image.endsWith("l") || image.endsWith("L")) {
55 return true;
56 }
57 }
58 return false;
59 }
60
61 public void setFloatLiteral() {
62 this.isFloat = true;
63 }
64
65 public boolean isFloatLiteral() {
66 String image = getImage();
67 if (isFloat && image != null && image.length() > 0) {
68 char lastChar = image.charAt(image.length() - 1);
69 if (lastChar == 'f' || lastChar == 'F') {
70 return true;
71 }
72 }
73 return false;
74 }
75
76
77
78
79
80 public boolean isDoubleLiteral() {
81 String image = getImage();
82 if (isFloat && image != null && image.length() > 0) {
83 char lastChar = image.charAt(image.length() - 1);
84 if (lastChar == 'd' || lastChar == 'D' || Character.isDigit(lastChar) || lastChar == '.') {
85 return true;
86 }
87 }
88 return false;
89 }
90
91 public void setCharLiteral() {
92 this.isChar = true;
93 }
94
95 public boolean isCharLiteral() {
96 return isChar;
97 }
98
99 public void setStringLiteral() {
100 this.isString = true;
101 }
102
103 public boolean isStringLiteral() {
104 return isString;
105 }
106
107
108
109
110
111
112
113 public boolean isSingleCharacterStringLiteral() {
114 if (isString) {
115 String image = getImage();
116 int length = image.length();
117 if (length == 3) {
118 return true;
119 } else if (image.charAt(1) == '\\') {
120 return SINGLE_CHAR_ESCAPE_PATTERN.matcher(image).matches();
121 }
122 }
123 return false;
124 }
125
126
127
128
129 private static final Pattern SINGLE_CHAR_ESCAPE_PATTERN = Pattern
130 .compile("^\"\\\\(([ntbrf\\\\'\\\"])|([0-7][0-7]?)|([0-3][0-7][0-7]))\"");
131
132 }