001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2020 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 if (ast.getType() == TokenTypes.DOT) { 081 extractFullIdent(full, ast.getFirstChild()); 082 full.append("."); 083 extractFullIdent( 084 full, ast.getFirstChild().getNextSibling()); 085 } 086 else if (ast.getType() == TokenTypes.ARRAY_DECLARATOR) { 087 extractFullIdent(full, ast.getFirstChild()); 088 full.append("[]"); 089 } 090 else { 091 full.append(ast); 092 } 093 } 094 } 095 096 /** 097 * Gets the text. 098 * 099 * @return the text 100 */ 101 public String getText() { 102 return String.join("", elements); 103 } 104 105 /** 106 * Gets the topmost leftmost DetailAST for this FullIdent. 107 * 108 * @return the topmost leftmost ast 109 */ 110 public DetailAST getDetailAst() { 111 return detailAst; 112 } 113 114 /** 115 * Gets the line number. 116 * 117 * @return the line number 118 */ 119 public int getLineNo() { 120 return detailAst.getLineNo(); 121 } 122 123 /** 124 * Gets the column number. 125 * 126 * @return the column number 127 */ 128 public int getColumnNo() { 129 return detailAst.getColumnNo(); 130 } 131 132 @Override 133 public String toString() { 134 return String.join("", elements) 135 + "[" + detailAst.getLineNo() + "x" + detailAst.getColumnNo() + "]"; 136 } 137 138 /** 139 * Append the specified text. 140 * 141 * @param text the text to append 142 */ 143 private void append(String text) { 144 elements.add(text); 145 } 146 147 /** 148 * Append the specified token and also recalibrate the first line and 149 * column. 150 * 151 * @param ast the token to append 152 */ 153 private void append(DetailAST ast) { 154 elements.add(ast.getText()); 155 if (detailAst == null) { 156 detailAst = ast; 157 } 158 } 159 160}