1 //////////////////////////////////////////////////////////////////////////////// 2 // checkstyle: Checks Java source code for adherence to a set of rules. 3 // Copyright (C) 2001-2015 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 package com.puppycrawl.tools.checkstyle.api; 20 21 /** 22 * Represents a full identifier, including dots, with associated 23 * position information. 24 * 25 * <p> 26 * Identifiers such as <code>java.util.HashMap</code> are spread across 27 * multiple AST nodes in the syntax tree (three IDENT nodes, two DOT nodes). 28 * A FullIdent represents the whole String (excluding any intermediate 29 * whitespace), which is often easier to work with in Checks. 30 * </p> 31 * 32 * @author Oliver Burn 33 * @see TokenTypes#DOT 34 * @see TokenTypes#IDENT 35 **/ 36 public final class FullIdent 37 { 38 /** the string **/ 39 private final StringBuffer buffer = new StringBuffer(); 40 /** the line number **/ 41 private int lineNo; 42 /** the column number **/ 43 private int colNo; 44 45 /** hide default constructor */ 46 private FullIdent() 47 { 48 } 49 50 /** @return the text **/ 51 public String getText() 52 { 53 return buffer.toString(); 54 } 55 56 /** @return the line number **/ 57 public int getLineNo() 58 { 59 return lineNo; 60 } 61 62 /** @return the column number **/ 63 public int getColumnNo() 64 { 65 return colNo; 66 } 67 68 /** 69 * Append the specified text. 70 * @param text the text to append 71 */ 72 private void append(String text) 73 { 74 buffer.append(text); 75 } 76 77 /** 78 * Append the specified token and also recalibrate the first line and 79 * column. 80 * @param ast the token to append 81 */ 82 private void append(DetailAST ast) 83 { 84 buffer.append(ast.getText()); 85 if (lineNo == 0) { 86 lineNo = ast.getLineNo(); 87 } 88 else if (ast.getLineNo() > 0) { 89 lineNo = Math.min(lineNo, ast.getLineNo()); 90 } 91 // TODO: make a function 92 if (colNo == 0) { 93 colNo = ast.getColumnNo(); 94 } 95 else if (ast.getColumnNo() > 0) { 96 colNo = Math.min(colNo, ast.getColumnNo()); 97 } 98 } 99 100 /** 101 * Creates a new FullIdent starting from the specified node. 102 * @param ast the node to start from 103 * @return a <code>FullIdent</code> value 104 */ 105 public static FullIdent createFullIdent(DetailAST ast) 106 { 107 final FullIdent fi = new FullIdent(); 108 extractFullIdent(fi, ast); 109 return fi; 110 } 111 112 /** 113 * Creates a new FullIdent starting from the child of the specified node. 114 * @param ast the parent node from where to start from 115 * @return a <code>FullIdent</code> value 116 */ 117 public static FullIdent createFullIdentBelow(DetailAST ast) 118 { 119 return createFullIdent(ast.getFirstChild()); 120 } 121 122 /** 123 * Recursively extract a FullIdent. 124 * 125 * @param full the FullIdent to add to 126 * @param ast the node to recurse from 127 */ 128 private static void extractFullIdent(FullIdent full, DetailAST ast) 129 { 130 if (ast == null) { 131 return; 132 } 133 134 if (ast.getType() == TokenTypes.DOT) { 135 extractFullIdent(full, ast.getFirstChild()); 136 full.append("."); 137 extractFullIdent( 138 full, ast.getFirstChild().getNextSibling()); 139 } 140 else { 141 full.append(ast); 142 } 143 } 144 145 @Override 146 public String toString() 147 { 148 return getText() + "[" + getLineNo() + "x" + getColumnNo() + "]"; 149 } 150 151 }