001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2019 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 specified node. 052 * @param ast the node to start from 053 * @return a {@code FullIdent} value 054 */ 055 public static FullIdent createFullIdent(DetailAST ast) { 056 final FullIdent ident = new FullIdent(); 057 extractFullIdent(ident, ast); 058 return ident; 059 } 060 061 /** 062 * Creates a new FullIdent starting from the child of the specified node. 063 * @param ast the parent node from where to start from 064 * @return a {@code FullIdent} value 065 */ 066 public static FullIdent createFullIdentBelow(DetailAST ast) { 067 return createFullIdent(ast.getFirstChild()); 068 } 069 070 /** 071 * Gets the text. 072 * @return the text 073 */ 074 public String getText() { 075 return String.join("", elements); 076 } 077 078 /** 079 * Gets the topmost leftmost DetailAST for this FullIdent. 080 * @return the topmost leftmost ast 081 */ 082 public DetailAST getDetailAst() { 083 return detailAst; 084 } 085 086 /** 087 * Gets the line number. 088 * @return the line number 089 */ 090 public int getLineNo() { 091 return detailAst.getLineNo(); 092 } 093 094 /** 095 * Gets the column number. 096 * @return the column number 097 */ 098 public int getColumnNo() { 099 return detailAst.getColumnNo(); 100 } 101 102 @Override 103 public String toString() { 104 return String.join("", elements) 105 + "[" + detailAst.getLineNo() + "x" + detailAst.getColumnNo() + "]"; 106 } 107 108 /** 109 * Recursively extract a FullIdent. 110 * 111 * @param full the FullIdent to add to 112 * @param ast the node to recurse from 113 */ 114 private static void extractFullIdent(FullIdent full, DetailAST ast) { 115 if (ast != null) { 116 if (ast.getType() == TokenTypes.DOT) { 117 extractFullIdent(full, ast.getFirstChild()); 118 full.append("."); 119 extractFullIdent( 120 full, ast.getFirstChild().getNextSibling()); 121 } 122 else if (ast.getType() == TokenTypes.ARRAY_DECLARATOR) { 123 extractFullIdent(full, ast.getFirstChild()); 124 full.append("[]"); 125 } 126 else { 127 full.append(ast); 128 } 129 } 130 } 131 132 /** 133 * Append the specified text. 134 * @param text the text to append 135 */ 136 private void append(String text) { 137 elements.add(text); 138 } 139 140 /** 141 * Append the specified token and also recalibrate the first line and 142 * column. 143 * @param ast the token to append 144 */ 145 private void append(DetailAST ast) { 146 elements.add(ast.getText()); 147 if (detailAst == null) { 148 detailAst = ast; 149 } 150 } 151 152}