1 /*
2 * Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.source.util;
27
28 import com.sun.source.tree.*;
29
30 /**
31 * Provides methods to obtain the position of a Tree within a CompilationUnit.
32 * A position is defined as a simple character offset from the start of a
33 * CompilationUnit where the first character is at offset 0.
34 *
35 * @author Peter von der Ahé
36 * @since 1.6
37 */
38 public interface SourcePositions {
39
40 /**
41 * Returns the starting position of tree within file. If tree is not found within
42 * file, or if the starting position is not available,
43 * returns {@link javax.tools.Diagnostic#NOPOS}.
44 * The returned position must be at the start of the yield of this tree, that
45 * is for any sub-tree of this tree, the following must hold:
46 *
47 * <p>
48 * {@code getStartPosition(file, tree) <= getStartPosition(file, subtree)} or <br>
49 * {@code getStartPosition(file, tree) == NOPOS} or <br>
50 * {@code getStartPosition(file, subtree) == NOPOS}
51 * </p>
52 *
53 * @param file CompilationUnit in which to find tree
54 * @param tree tree for which a position is sought
55 * @return the start position of tree
56 * @deprecated use {@link #getStartPosition(Tree)} instead
57 */
58 @Deprecated(since = "27", forRemoval = true)
59 default long getStartPosition(CompilationUnitTree file, Tree tree) {
60 return getStartPosition(tree);
61 }
62
63 /**
64 * {@return the starting position of the given {@link Tree}, or if the starting position is not available, returns
65 * {@link javax.tools.Diagnostic#NOPOS}}
66 *
67 * <p>The returned position must be at the start of the yield of this tree, that is for any sub-tree of this tree,
68 * the following must hold:
69 *
70 * <p>
71 * {@code getStartPosition(tree) <= getStartPosition(subtree)} or <br>
72 * {@code getStartPosition(tree) == NOPOS} or <br>
73 * {@code getStartPosition(subtree) == NOPOS}
74 * </p>
75 *
76 * @param tree tree for which a position is sought
77 * @since 27
78 */
79 long getStartPosition(Tree tree);
80
81 /**
82 * Returns the ending position of tree within file. If tree is not found within
83 * file, or if the ending position is not available,
84 * returns {@link javax.tools.Diagnostic#NOPOS}.
85 * The returned position must be at the end of the yield of this tree,
86 * that is for any sub-tree of this tree, the following must hold:
87 *
88 * <p>
89 * {@code getEndPosition(file, tree) >= getEndPosition(file, subtree)} or <br>
90 * {@code getEndPosition(file, tree) == NOPOS} or <br>
91 * {@code getEndPosition(file, subtree) == NOPOS}
92 * </p>
93 *
94 * In addition, the following must hold:
95 *
96 * <p>
97 * {@code getStartPosition(file, tree) <= getEndPosition(file, tree)} or <br>
98 * {@code getStartPosition(file, tree) == NOPOS} or <br>
99 * {@code getEndPosition(file, tree) == NOPOS}
100 * </p>
101 *
102 * @param file CompilationUnit in which to find tree
103 * @param tree tree for which a position is sought
104 * @return the end position of tree
105 * @deprecated use {@link #getEndPosition(Tree)} instead
106 */
107 @Deprecated(since = "27", forRemoval = true)
108 default long getEndPosition(CompilationUnitTree file, Tree tree) {
109 return getEndPosition(tree);
110 }
111
112 /**
113 * {@return the ending position of the given {@link Tree}. If the ending position is not available,
114 * returns {@link javax.tools.Diagnostic#NOPOS}}
115 *
116 * <p>The returned position must be at the end of the yield of this tree, that is for any sub-tree of this tree,
117 * the following must hold:
118 *
119 * <p>
120 * {@code getEndPosition(tree) >= getEndPosition(subtree)} or <br>
121 * {@code getEndPosition(tree) == NOPOS} or <br>
122 * {@code getEndPosition(subtree) == NOPOS}
123 * </p>
124 *
125 * In addition, the following must hold:
126 *
127 * <p>
128 * {@code getStartPosition(tree) <= getEndPosition(tree)} or <br>
129 * {@code getStartPosition(tree) == NOPOS} or <br>
130 * {@code getEndPosition(tree) == NOPOS}
131 * </p>
132 *
133 * @param tree tree for which a position is sought
134 * @since 27
135 */
136 long getEndPosition(Tree tree);
137 }