1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.puppycrawl.tools.checkstyle.api;
21
22 import java.util.ArrayDeque;
23 import java.util.ArrayList;
24 import java.util.Deque;
25 import java.util.List;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public final class FullIdent {
42
43
44 private final List<String> elements = new ArrayList<>();
45
46 private DetailAST detailAst;
47
48
49 private FullIdent() {
50 }
51
52
53
54
55
56
57
58 public static FullIdent createFullIdentBelow(DetailAST ast) {
59 return createFullIdent(ast.getFirstChild());
60 }
61
62
63
64
65
66
67
68 public static FullIdent createFullIdent(DetailAST ast) {
69 final FullIdent ident = new FullIdent();
70 extractFullIdent(ident, ast);
71 return ident;
72 }
73
74
75
76
77
78
79
80 private static void extractFullIdent(FullIdent full, DetailAST ast) {
81 final Deque<DetailAST> identStack = new ArrayDeque<>();
82 pushToIdentStack(identStack, ast);
83 boolean bracketsExist = false;
84 int dotCounter = 0;
85 while (!identStack.isEmpty()) {
86 final DetailAST currentAst = identStack.pop();
87
88 final DetailAST nextSibling = currentAst.getNextSibling();
89
90
91 final boolean isArrayTypeDeclarationStart = nextSibling != null
92 && (nextSibling.getType() == TokenTypes.ARRAY_DECLARATOR
93 || nextSibling.getType() == TokenTypes.ANNOTATIONS)
94 && isArrayTypeDeclaration(nextSibling);
95
96 final int typeOfAst = currentAst.getType();
97 bracketsExist = bracketsExist || isArrayTypeDeclarationStart;
98 final DetailAST firstChild = currentAst.getFirstChild();
99
100 if (typeOfAst == TokenTypes.LITERAL_NEW && currentAst.hasChildren()) {
101 pushToIdentStack(identStack, firstChild);
102 }
103 else if (typeOfAst == TokenTypes.DOT) {
104 pushToIdentStack(identStack, firstChild.getNextSibling());
105 pushToIdentStack(identStack, firstChild);
106 dotCounter++;
107 }
108 else {
109 dotCounter = appendToFull(full, currentAst, dotCounter,
110 bracketsExist, isArrayTypeDeclarationStart);
111 }
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124
125 private static int appendToFull(FullIdent full, DetailAST ast,
126 int dotCounter, boolean bracketsExist, boolean isArrayTypeDeclarationStart) {
127 int result = dotCounter;
128 if (isArrayTypeDeclarationStart) {
129 full.append(ast);
130 appendBrackets(full, ast);
131 }
132 else if (ast.getType() != TokenTypes.ANNOTATIONS) {
133 full.append(ast);
134 if (dotCounter > 0) {
135 full.append(".");
136 result--;
137 }
138 if (bracketsExist) {
139 appendBrackets(full, ast.getParent());
140 }
141 }
142 return result;
143 }
144
145
146
147
148
149
150
151 private static void pushToIdentStack(Deque<DetailAST> stack, DetailAST ast) {
152 if (ast != null) {
153 stack.push(ast);
154 }
155 }
156
157
158
159
160
161
162
163
164
165 private static boolean isArrayTypeDeclaration(DetailAST arrayDeclarator) {
166 DetailAST expression = arrayDeclarator;
167 while (expression != null) {
168 if (expression.getType() == TokenTypes.EXPR) {
169 break;
170 }
171 expression = expression.getFirstChild();
172 }
173 return expression == null;
174 }
175
176
177
178
179
180
181
182 private static void appendBrackets(FullIdent full, DetailAST ast) {
183 final int bracketCount =
184 ast.getParent().getChildCount(TokenTypes.ARRAY_DECLARATOR);
185 for (int i = 0; i < bracketCount; i++) {
186 full.append("[]");
187 }
188 }
189
190
191
192
193
194
195 public String getText() {
196 return String.join("", elements);
197 }
198
199
200
201
202
203
204 public DetailAST getDetailAst() {
205 return detailAst;
206 }
207
208
209
210
211
212
213 public int getLineNo() {
214 return detailAst.getLineNo();
215 }
216
217
218
219
220
221
222 public int getColumnNo() {
223 return detailAst.getColumnNo();
224 }
225
226 @Override
227 public String toString() {
228 return String.join("", elements)
229 + "[" + detailAst.getLineNo() + "x" + detailAst.getColumnNo() + "]";
230 }
231
232
233
234
235
236
237 private void append(String text) {
238 elements.add(text);
239 }
240
241
242
243
244
245
246
247 private void append(DetailAST ast) {
248 elements.add(ast.getText());
249 if (detailAst == null) {
250 detailAst = ast;
251 }
252 }
253
254 }