-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
FullIdent.java
executable file
·151 lines (138 loc) · 4.25 KB
/
FullIdent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2013 Oliver Burn
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.api;
/**
* Represents a full identifier, including dots, with associated
* position information.
*
* <p>
* Identifiers such as <code>java.util.HashMap</code> are spread across
* multiple AST nodes in the syntax tree (three IDENT nodes, two DOT nodes).
* A FullIdent represents the whole String (excluding any intermediate
* whitespace), which is often easier to work with in Checks.
* </p>
*
* @author Oliver Burn
* @see TokenTypes#DOT
* @see TokenTypes#IDENT
**/
public final class FullIdent
{
/** the string **/
private final StringBuffer mBuffer = new StringBuffer();
/** the line number **/
private int mLineNo;
/** the column number **/
private int mColNo;
/** hide default constructor */
private FullIdent()
{
}
/** @return the text **/
public String getText()
{
return mBuffer.toString();
}
/** @return the line number **/
public int getLineNo()
{
return mLineNo;
}
/** @return the column number **/
public int getColumnNo()
{
return mColNo;
}
/**
* Append the specified text.
* @param aText the text to append
*/
private void append(String aText)
{
mBuffer.append(aText);
}
/**
* Append the specified token and also recalibrate the first line and
* column.
* @param aAST the token to append
*/
private void append(DetailAST aAST)
{
mBuffer.append(aAST.getText());
if (mLineNo == 0) {
mLineNo = aAST.getLineNo();
}
else if (aAST.getLineNo() > 0) {
mLineNo = Math.min(mLineNo, aAST.getLineNo());
}
// TODO: make a function
if (mColNo == 0) {
mColNo = aAST.getColumnNo();
}
else if (aAST.getColumnNo() > 0) {
mColNo = Math.min(mColNo, aAST.getColumnNo());
}
}
/**
* Creates a new FullIdent starting from the specified node.
* @param aAST the node to start from
* @return a <code>FullIdent</code> value
*/
public static FullIdent createFullIdent(DetailAST aAST)
{
final FullIdent fi = new FullIdent();
extractFullIdent(fi, aAST);
return fi;
}
/**
* Creates a new FullIdent starting from the child of the specified node.
* @param aAST the parent node from where to start from
* @return a <code>FullIdent</code> value
*/
public static FullIdent createFullIdentBelow(DetailAST aAST)
{
return createFullIdent(aAST.getFirstChild());
}
/**
* Recursively extract a FullIdent.
*
* @param aFull the FullIdent to add to
* @param aAST the node to recurse from
*/
private static void extractFullIdent(FullIdent aFull, DetailAST aAST)
{
if (aAST == null) {
return;
}
if (aAST.getType() == TokenTypes.DOT) {
extractFullIdent(aFull, aAST.getFirstChild());
aFull.append(".");
extractFullIdent(
aFull, aAST.getFirstChild().getNextSibling());
}
else {
aFull.append(aAST);
}
}
@Override
public String toString()
{
return getText() + "[" + getLineNo() + "x" + getColumnNo() + "]";
}
}