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
114 public String getEscapedStringLiteral() {
115 String image = getImage();
116 if (!isStringLiteral() && !isCharLiteral()) {
117 return image;
118 }
119 int fullLength = getEndColumn() - getBeginColumn();
120 if (fullLength > image.length()) {
121 StringBuilder result = new StringBuilder(fullLength);
122 for (int i = 0; i < image.length(); i++) {
123 char c = image.charAt(i);
124 if (c < 0x20 || c > 0xff || image.length() == 1) {
125 String hex = "0000" + Integer.toHexString(c);
126 result.append("\\u").append(hex.substring(hex.length() - 4));
127 } else {
128 result.append(c);
129 }
130 }
131 return result.toString();
132 }
133 return image;
134 }
135
136
137
138
139
140
141
142 public boolean isSingleCharacterStringLiteral() {
143 if (isString) {
144 String image = getImage();
145 int length = image.length();
146 if (length == 3) {
147 return true;
148 } else if (image.charAt(1) == '\\') {
149 return SINGLE_CHAR_ESCAPE_PATTERN.matcher(image).matches();
150 }
151 }
152 return false;
153 }
154
155
156
157
158 private static final Pattern SINGLE_CHAR_ESCAPE_PATTERN = Pattern
159 .compile("^\"\\\\(([ntbrf\\\\'\\\"])|([0-7][0-7]?)|([0-3][0-7][0-7]))\"");
160
161 }