16
16
package com .google .testing .compile ;
17
17
18
18
import static com .google .common .base .Preconditions .checkArgument ;
19
+ import static javax .tools .Diagnostic .NOPOS ;
19
20
20
21
import com .sun .source .tree .CompilationUnitTree ;
21
22
import com .sun .source .tree .LineMap ;
24
25
import com .sun .source .util .TreePath ;
25
26
import com .sun .source .util .Trees ;
26
27
27
- import javax .tools .Diagnostic ;
28
-
29
28
/**
30
29
* A class for managing and retrieving contextual information for Compilation Trees.
31
30
*
@@ -74,71 +73,118 @@ TreePath getNodePath(Tree node) {
74
73
}
75
74
76
75
/**
77
- * Returns start line of the given sub-{@code Tree} of this object's {@code CompilationUnitTree}.
76
+ * Returns start line of the given sub-{@code Tree} of this object's {@code CompilationUnitTree},
77
+ * climbing the associated {@code TreePath} until a value other than
78
+ * {@link javax.tools.Diagnostic.NOPOS} is found.
79
+ *
80
+ * <p>This method will return {@link javax.tools.Diagnostic.NOPOS} if that value is returned
81
+ * by a call to {@link SourcePositions#getStartPosition} for every node in the {@link TreePath}
82
+ * provided.
78
83
*
79
84
* @throws IllegalArgumentException if the node provided is not a sub-{@code Tree} of this
80
85
* object's {@code CompilationUnitTree}.
81
86
*/
82
87
long getNodeStartLine (Tree node ) {
83
- return lineMap .getLineNumber (getNodeStartPosition (node ));
88
+ long startPosition = getNodeStartPosition (node );
89
+ return startPosition == NOPOS ? NOPOS : lineMap .getLineNumber (startPosition );
84
90
}
85
91
86
92
/**
87
93
* Returns start column of the given sub-{@code Tree} of this object's
88
- * {@code CompilationUnitTree}.
94
+ * {@code CompilationUnitTree}, climbing the associated {@code TreePath} until a value other than
95
+ * {@link javax.tools.Diagnostic.NOPOS} is found.
96
+ *
97
+ * <p>This method will return {@link javax.tools.Diagnostic.NOPOS} if that value is returned
98
+ * by a call to {@link SourcePositions#getStartPosition} for every node in the {@link TreePath}
99
+ * provided.
89
100
*
90
101
* @throws IllegalArgumentException if the node provided is not a sub-{@code Tree} of this
91
102
* object's {@code CompilationUnitTree}.
92
103
*/
93
104
long getNodeStartColumn (Tree node ) {
94
- return lineMap .getColumnNumber (getNodeStartPosition (node ));
105
+ long startPosition = getNodeStartPosition (node );
106
+ return startPosition == NOPOS ? NOPOS : lineMap .getColumnNumber (startPosition );
95
107
}
96
108
97
109
/**
98
110
* Returns end line of the given sub-{@code Tree} of this object's {@code CompilationUnitTree}.
111
+ * climbing the associated {@code TreePath} until a value other than
112
+ * {@link javax.tools.Diagnostic.NOPOS} is found.
113
+ *
114
+ * <p>This method will return {@link javax.tools.Diagnostic.NOPOS} if that value is returned
115
+ * by a call to {@link SourcePositions#getEndPosition} for every node in the {@link TreePath}
116
+ * provided.
99
117
*
100
118
* @throws IllegalArgumentException if the node provided is not a sub-{@code Tree} of this
101
119
* object's {@code CompilationUnitTree}.
102
120
*/
103
121
long getNodeEndLine (Tree node ) {
104
- return lineMap .getLineNumber (getNodeEndPosition (node ));
122
+ long endPosition = getNodeEndPosition (node );
123
+ return endPosition == NOPOS ? NOPOS : lineMap .getLineNumber (endPosition );
105
124
}
106
125
107
126
/**
108
127
* Returns end column of the given sub-{@code Tree} of this object's {@code CompilationUnitTree}.
128
+ * climbing the associated {@code TreePath} until a value other than
129
+ * {@link javax.tools.Diagnostic.NOPOS} is found.
130
+ *
131
+ * <p>This method will return {@link javax.tools.Diagnostic.NOPOS} if that value is returned
132
+ * by a call to {@link SourcePositions#getEndPosition} for every node in the {@link TreePath}
133
+ * provided.
109
134
*
110
135
* @throws IllegalArgumentException if the node provided is not a sub-{@code Tree} of this
111
136
* object's {@code CompilationUnitTree}.
112
137
*/
113
138
long getNodeEndColumn (Tree node ) {
114
- return lineMap .getColumnNumber (getNodeEndPosition (node ));
139
+ long endPosition = getNodeEndPosition (node );
140
+ return endPosition == NOPOS ? NOPOS : lineMap .getColumnNumber (endPosition );
115
141
}
116
142
117
143
/**
118
144
* Returns start position of the given sub-{@code Tree} of this object's
119
- * {@code CompilationUnitTree}.
145
+ * {@code CompilationUnitTree}, climbing the associated {@code TreePath} until a value other than
146
+ * {@link javax.tools.Diagnostic.NOPOS} is found.
147
+ *
148
+ * <p>This method will return {@link javax.tools.Diagnostic.NOPOS} if that value is returned
149
+ * by a call to {@link SourcePositions#getStartPosition} for every node in the {@link TreePath}
150
+ * provided.
120
151
*
121
152
* @throws IllegalArgumentException if the node provided is not a sub-{@code Tree} of this
122
153
* object's {@code CompilationUnitTree}.
123
154
*/
124
155
long getNodeStartPosition (Tree node ) {
125
- long startPosition = sourcePositions .getStartPosition (compilationUnit , node );
126
- checkArgument (startPosition != Diagnostic .NOPOS ,
127
- "The node provided was not a subtree of this context's CompilationUnitTree: %s" , node );
128
- return startPosition ;
156
+ TreePath currentNode = getNodePath (node );
157
+ while (currentNode != null ) {
158
+ long startPosition = sourcePositions .getStartPosition (compilationUnit , currentNode .getLeaf ());
159
+ if (startPosition != NOPOS ) {
160
+ return startPosition ;
161
+ }
162
+ currentNode = currentNode .getParentPath ();
163
+ }
164
+ return NOPOS ;
129
165
}
130
166
131
167
/**
132
168
* Returns end position of the given sub-{@code Tree} of this object's
133
- * {@code CompilationUnitTree}.
169
+ * {@code CompilationUnitTree}, climbing the associated {@code TreePath} until a value other than
170
+ * {@link javax.tools.Diagnostic.NOPOS} is found.
171
+ *
172
+ * <p>This method will return {@link javax.tools.Diagnostic.NOPOS} if that value is returned
173
+ * by a call to {@link SourcePositions#getEndPosition} for every node in the {@link TreePath}
174
+ * provided.
134
175
*
135
176
* @throws IllegalArgumentException if the node provided is not a sub-{@code Tree} of this
136
177
* object's {@code CompilationUnitTree}.
137
178
*/
138
179
long getNodeEndPosition (Tree node ) {
139
- long endPosition = sourcePositions .getEndPosition (compilationUnit , node );
140
- checkArgument (endPosition != Diagnostic .NOPOS ,
141
- "The node provided was not a subtree of this context's CompilationUnitTree: %s" , node );
142
- return endPosition ;
180
+ TreePath currentNode = getNodePath (node );
181
+ while (node != null ) {
182
+ long endPosition = sourcePositions .getEndPosition (compilationUnit , currentNode .getLeaf ());
183
+ if (endPosition != NOPOS ) {
184
+ return endPosition ;
185
+ }
186
+ currentNode = currentNode .getParentPath ();
187
+ }
188
+ return NOPOS ;
143
189
}
144
190
}
0 commit comments