001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2021 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.api; 021 022import java.util.ArrayList; 023import java.util.List; 024 025/** 026 * Represents a full identifier, including dots, with associated 027 * position information. 028 * 029 * <p> 030 * Identifiers such as {@code java.util.HashMap} are spread across 031 * multiple AST nodes in the syntax tree (three IDENT nodes, two DOT nodes). 032 * A FullIdent represents the whole String (excluding any intermediate 033 * whitespace), which is often easier to work with in Checks. 034 * </p> 035 * 036 * @see TokenTypes#DOT 037 * @see TokenTypes#IDENT 038 **/ 039public final class FullIdent { 040 041 /** The list holding subsequent elements of identifier. **/ 042 private final List<String> elements = new ArrayList<>(); 043 /** The topmost and leftmost AST of the full identifier. */ 044 private DetailAST detailAst; 045 046 /** Hide default constructor. */ 047 private FullIdent() { 048 } 049 050 /** 051 * Creates a new FullIdent starting from the child of the specified node. 052 * 053 * @param ast the parent node from where to start from 054 * @return a {@code FullIdent} value 055 */ 056 public static FullIdent createFullIdentBelow(DetailAST ast) { 057 return createFullIdent(ast.getFirstChild()); 058 } 059 060 /** 061 * Creates a new FullIdent starting from the specified node. 062 * 063 * @param ast the node to start from 064 * @return a {@code FullIdent} value 065 */ 066 public static FullIdent createFullIdent(DetailAST ast) { 067 final FullIdent ident = new FullIdent(); 068 extractFullIdent(ident, ast); 069 return ident; 070 } 071 072 /** 073 * Recursively extract a FullIdent. 074 * 075 * @param full the FullIdent to add to 076 * @param ast the node to recurse from 077 */ 078 private static void extractFullIdent(FullIdent full, DetailAST ast) { 079 if (ast != null) { 080 final DetailAST nextSibling = ast.getNextSibling(); 081 082 // Here we want type declaration, but not initialization 083 final boolean isArrayTypeDeclarationStart = nextSibling != null 084 && (nextSibling.getType() == TokenTypes.ARRAY_DECLARATOR 085 || nextSibling.getType() == TokenTypes.ANNOTATIONS) 086 && isArrayTypeDeclaration(nextSibling); 087 088 if (ast.getType() == TokenTypes.DOT) { 089 final DetailAST firstChild = ast.getFirstChild(); 090 extractFullIdent(full, firstChild); 091 full.append("."); 092 extractFullIdent(full, firstChild.getNextSibling()); 093 appendBrackets(full, ast); 094 } 095 else if (isArrayTypeDeclarationStart) { 096 full.append(ast); 097 appendBrackets(full, ast); 098 } 099 else if (ast.getType() != TokenTypes.ANNOTATIONS) { 100 full.append(ast); 101 } 102 } 103 } 104 105 /** 106 * Checks an `ARRAY_DECLARATOR` ast to verify that it is not a 107 * array initialization, i.e. 'new int [2][2]'. We do this by 108 * making sure that no 'EXPR' token exists in this branch. 109 * 110 * @param arrayDeclarator the first ARRAY_DECLARATOR token in the ast 111 * @return true if ast is an array type declaration 112 */ 113 private static boolean isArrayTypeDeclaration(DetailAST arrayDeclarator) { 114 DetailAST expression = arrayDeclarator.getFirstChild(); 115 while (expression != null) { 116 if (expression.getType() == TokenTypes.EXPR) { 117 break; 118 } 119 expression = expression.getFirstChild(); 120 } 121 return expression == null; 122 } 123 124 /** 125 * Appends the brackets of an array type to a {@code FullIdent}. 126 * 127 * @param full the FullIdent to append brackets to 128 * @param ast the type ast we are building a {@code FullIdent} for 129 */ 130 private static void appendBrackets(FullIdent full, DetailAST ast) { 131 final int bracketCount = 132 ast.getParent().getChildCount(TokenTypes.ARRAY_DECLARATOR); 133 for (int i = 0; i < bracketCount; i++) { 134 full.append("[]"); 135 } 136 } 137 138 /** 139 * Gets the text. 140 * 141 * @return the text 142 */ 143 public String getText() { 144 return String.join("", elements); 145 } 146 147 /** 148 * Gets the topmost leftmost DetailAST for this FullIdent. 149 * 150 * @return the topmost leftmost ast 151 */ 152 public DetailAST getDetailAst() { 153 return detailAst; 154 } 155 156 /** 157 * Gets the line number. 158 * 159 * @return the line number 160 */ 161 public int getLineNo() { 162 return detailAst.getLineNo(); 163 } 164 165 /** 166 * Gets the column number. 167 * 168 * @return the column number 169 */ 170 public int getColumnNo() { 171 return detailAst.getColumnNo(); 172 } 173 174 @Override 175 public String toString() { 176 return String.join("", elements) 177 + "[" + detailAst.getLineNo() + "x" + detailAst.getColumnNo() + "]"; 178 } 179 180 /** 181 * Append the specified text. 182 * 183 * @param text the text to append 184 */ 185 private void append(String text) { 186 elements.add(text); 187 } 188 189 /** 190 * Append the specified token and also recalibrate the first line and 191 * column. 192 * 193 * @param ast the token to append 194 */ 195 private void append(DetailAST ast) { 196 elements.add(ast.getText()); 197 if (detailAst == null) { 198 detailAst = ast; 199 } 200 } 201 202}