001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2016 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 025import com.google.common.base.Joiner; 026 027/** 028 * Represents a full identifier, including dots, with associated 029 * position information. 030 * 031 * <p> 032 * Identifiers such as {@code java.util.HashMap} are spread across 033 * multiple AST nodes in the syntax tree (three IDENT nodes, two DOT nodes). 034 * A FullIdent represents the whole String (excluding any intermediate 035 * whitespace), which is often easier to work with in Checks. 036 * </p> 037 * 038 * @author Oliver Burn 039 * @see TokenTypes#DOT 040 * @see TokenTypes#IDENT 041 **/ 042public final class FullIdent { 043 /** The list holding subsequent elements of identifier. **/ 044 private final List<String> elements = new ArrayList<>(); 045 /** The line number. **/ 046 private int lineNo; 047 /** The column number. **/ 048 private int columnNo; 049 050 /** Hide default constructor. */ 051 private FullIdent() { 052 } 053 054 /** 055 * Creates a new FullIdent starting from the specified node. 056 * @param ast the node to start from 057 * @return a {@code FullIdent} value 058 */ 059 public static FullIdent createFullIdent(DetailAST ast) { 060 final FullIdent ident = new FullIdent(); 061 extractFullIdent(ident, ast); 062 return ident; 063 } 064 065 /** 066 * Creates a new FullIdent starting from the child of the specified node. 067 * @param ast the parent node from where to start from 068 * @return a {@code FullIdent} value 069 */ 070 public static FullIdent createFullIdentBelow(DetailAST ast) { 071 return createFullIdent(ast.getFirstChild()); 072 } 073 074 /** 075 * Gets the text. 076 * @return the text 077 */ 078 public String getText() { 079 return Joiner.on("").join(elements); 080 } 081 082 /** 083 * Gets the line number. 084 * @return the line number 085 */ 086 public int getLineNo() { 087 return lineNo; 088 } 089 090 /** 091 * Gets the column number. 092 * @return the column number 093 */ 094 public int getColumnNo() { 095 return columnNo; 096 } 097 098 @Override 099 public String toString() { 100 return getText() + "[" + lineNo + "x" + columnNo + "]"; 101 } 102 103 /** 104 * Recursively extract a FullIdent. 105 * 106 * @param full the FullIdent to add to 107 * @param ast the node to recurse from 108 */ 109 private static void extractFullIdent(FullIdent full, DetailAST ast) { 110 if (ast != null) { 111 if (ast.getType() == TokenTypes.DOT) { 112 extractFullIdent(full, ast.getFirstChild()); 113 full.append("."); 114 extractFullIdent( 115 full, ast.getFirstChild().getNextSibling()); 116 } 117 else { 118 full.append(ast); 119 } 120 } 121 } 122 123 /** 124 * Append the specified text. 125 * @param text the text to append 126 */ 127 private void append(String text) { 128 elements.add(text); 129 } 130 131 /** 132 * Append the specified token and also recalibrate the first line and 133 * column. 134 * @param ast the token to append 135 */ 136 private void append(DetailAST ast) { 137 elements.add(ast.getText()); 138 if (lineNo == 0) { 139 lineNo = ast.getLineNo(); 140 } 141 else if (ast.getLineNo() > 0) { 142 lineNo = Math.min(lineNo, ast.getLineNo()); 143 } 144 if (columnNo == 0) { 145 columnNo = ast.getColumnNo(); 146 } 147 else if (ast.getColumnNo() > 0) { 148 columnNo = Math.min(columnNo, ast.getColumnNo()); 149 } 150 } 151}