1 ///////////////////////////////////////////////////////////////////////////////////////////////
2 // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3 // Copyright (C) 2001-2026 the original author or authors.
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 * Represents a full identifier, including dots, with associated position information.
29 *
30 * <p>
31 * Identifiers such as {@code java.util.HashMap} are spread across
32 * multiple AST nodes in the syntax tree (three IDENT nodes, two DOT nodes).
33 * A FullIdent represents the whole String (excluding any intermediate
34 * whitespace), which is often easier to work with in Checks.
35 * </p>
36 *
37 * @see TokenTypes#DOT
38 * @see TokenTypes#IDENT
39 */
40 public final class FullIdent {
41
42 /** The list holding subsequent elements of identifier. */
43 private final List<String> elements = new ArrayList<>();
44 /** The topmost and leftmost AST of the full identifier. */
45 private DetailAST detailAst;
46
47 /** Hide default constructor. */
48 private FullIdent() {
49 }
50
51 /**
52 * Creates a new FullIdent starting from the child of the specified node.
53 *
54 * @param ast the parent node from where to start from
55 * @return a {@code FullIdent} value
56 */
57 public static FullIdent createFullIdentBelow(DetailAST ast) {
58 return createFullIdent(ast.getFirstChild());
59 }
60
61 /**
62 * Creates a new FullIdent starting from the specified node.
63 *
64 * @param ast the node to start from
65 * @return a {@code FullIdent} value
66 */
67 public static FullIdent createFullIdent(DetailAST ast) {
68 DetailAST node = ast;
69 if (node != null && node.getType() == TokenTypes.SINGLE_LINE_COMMENT) {
70 node = node.getNextSibling();
71 }
72 final FullIdent ident = new FullIdent();
73 extractFullIdent(ident, node);
74 return ident;
75 }
76
77 /**
78 * Extracts a FullIdent.
79 *
80 * @param full the FullIdent to add to
81 * @param ast the node
82 */
83 private static void extractFullIdent(FullIdent full, DetailAST ast) {
84 final Deque<DetailAST> identStack = new ArrayDeque<>();
85 pushToIdentStack(identStack, ast);
86 boolean bracketsExist = false;
87 int dotCounter = 0;
88 while (!identStack.isEmpty()) {
89 final DetailAST currentAst = identStack.pop();
90
91 final DetailAST nextSibling = currentAst.getNextSibling();
92
93 // Here we want type declaration, but not initialization
94 final boolean isArrayTypeDeclarationStart = nextSibling != null
95 && (nextSibling.getType() == TokenTypes.ARRAY_DECLARATOR
96 || nextSibling.getType() == TokenTypes.ANNOTATIONS)
97 && isArrayTypeDeclaration(nextSibling);
98
99 final int typeOfAst = currentAst.getType();
100 bracketsExist = bracketsExist || isArrayTypeDeclarationStart;
101 final DetailAST firstChild = currentAst.getFirstChild();
102
103 if (typeOfAst == TokenTypes.LITERAL_NEW && currentAst.hasChildren()) {
104 pushToIdentStack(identStack, firstChild);
105 }
106 else if (typeOfAst == TokenTypes.DOT) {
107 DetailAST sibling = firstChild.getNextSibling();
108 if (sibling.getType() == TokenTypes.SINGLE_LINE_COMMENT) {
109 sibling = sibling.getNextSibling();
110 }
111 pushToIdentStack(identStack, sibling);
112 pushToIdentStack(identStack, firstChild);
113 dotCounter++;
114 }
115 else {
116 dotCounter = appendToFull(full, currentAst, dotCounter,
117 bracketsExist, isArrayTypeDeclarationStart);
118 }
119 }
120 }
121
122 /**
123 * Populates the FullIdent node.
124 *
125 * @param full the FullIdent to add to
126 * @param ast the node
127 * @param dotCounter no of dots
128 * @param bracketsExist yes if true
129 * @param isArrayTypeDeclarationStart true if array type declaration start
130 * @return updated value of dotCounter
131 */
132 private static int appendToFull(FullIdent full, DetailAST ast,
133 int dotCounter, boolean bracketsExist, boolean isArrayTypeDeclarationStart) {
134 int result = dotCounter;
135 if (isArrayTypeDeclarationStart) {
136 full.append(ast);
137 appendBrackets(full, ast);
138 }
139 else if (ast.getType() != TokenTypes.ANNOTATIONS) {
140 full.append(ast);
141 if (dotCounter > 0) {
142 full.append(".");
143 result--;
144 }
145 if (bracketsExist) {
146 appendBrackets(full, ast.getParent());
147 }
148 }
149 return result;
150 }
151
152 /**
153 * Pushes to stack if ast is not null.
154 *
155 * @param stack stack to push into
156 * @param ast node to push into stack
157 */
158 private static void pushToIdentStack(Deque<DetailAST> stack, DetailAST ast) {
159 if (ast != null) {
160 stack.push(ast);
161 }
162 }
163
164 /**
165 * Checks an `ARRAY_DECLARATOR` ast to verify that it is not an
166 * array initialization, i.e. 'new int [2][2]'. We do this by
167 * making sure that no 'EXPR' token exists in this branch.
168 *
169 * @param arrayDeclarator the first ARRAY_DECLARATOR token in the ast
170 * @return true if ast is an array type declaration
171 */
172 private static boolean isArrayTypeDeclaration(DetailAST arrayDeclarator) {
173 DetailAST expression = arrayDeclarator;
174 while (expression != null) {
175 if (expression.getType() == TokenTypes.EXPR) {
176 break;
177 }
178 expression = expression.getFirstChild();
179 }
180 return expression == null;
181 }
182
183 /**
184 * Appends the brackets of an array type to a {@code FullIdent}.
185 *
186 * @param full the FullIdent to append brackets to
187 * @param ast the type ast we are building a {@code FullIdent} for
188 */
189 private static void appendBrackets(FullIdent full, DetailAST ast) {
190 final int bracketCount =
191 ast.getParent().getChildCount(TokenTypes.ARRAY_DECLARATOR);
192 for (int index = 0; index < bracketCount; index++) {
193 full.append("[]");
194 }
195 }
196
197 /**
198 * Gets the text.
199 *
200 * @return the text
201 */
202 public String getText() {
203 return String.join("", elements);
204 }
205
206 /**
207 * Gets the topmost leftmost DetailAST for this FullIdent.
208 *
209 * @return the topmost leftmost ast
210 */
211 public DetailAST getDetailAst() {
212 return detailAst;
213 }
214
215 /**
216 * Gets the line number.
217 *
218 * @return the line number
219 */
220 public int getLineNo() {
221 return detailAst.getLineNo();
222 }
223
224 /**
225 * Gets the column number.
226 *
227 * @return the column number
228 */
229 public int getColumnNo() {
230 return detailAst.getColumnNo();
231 }
232
233 @Override
234 public String toString() {
235 return String.join("", elements)
236 + "[" + detailAst.getLineNo() + "x" + detailAst.getColumnNo() + "]";
237 }
238
239 /**
240 * Append the specified text.
241 *
242 * @param text the text to append
243 */
244 private void append(String text) {
245 elements.add(text);
246 }
247
248 /**
249 * Append the specified token and also recalibrate the first line and column.
250 *
251 * @param ast the token to append
252 */
253 private void append(DetailAST ast) {
254 elements.add(ast.getText());
255 if (detailAst == null) {
256 detailAst = ast;
257 }
258 }
259
260 }