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      */
 78      long getStartPosition(Tree tree);
 79 
 80     /**
 81      * Returns the ending position of tree within file.  If tree is not found within
 82      * file, or if the ending position is not available,
 83      * returns {@link javax.tools.Diagnostic#NOPOS}.
 84      * The returned position must be at the end of the yield of this tree,
 85      * that is for any sub-tree of this tree, the following must hold:
 86      *
 87      * <p>
 88      * {@code getEndPosition(file, tree) >= getEndPosition(file, subtree)} or <br>
 89      * {@code getEndPosition(file, tree) == NOPOS} or <br>
 90      * {@code getEndPosition(file, subtree) == NOPOS}
 91      * </p>
 92      *
 93      * In addition, the following must hold:
 94      *
 95      * <p>
 96      * {@code getStartPosition(file, tree) <= getEndPosition(file, tree)} or <br>
 97      * {@code getStartPosition(file, tree) == NOPOS} or <br>
 98      * {@code getEndPosition(file, tree) == NOPOS}
 99      * </p>
100      *
101      * @param file CompilationUnit in which to find tree
102      * @param tree tree for which a position is sought
103      * @return the end position of tree
104      * @deprecated use {@link #getEndPosition(Tree)} instead
105      */
106      @Deprecated(since = "27", forRemoval = true)
107      default long getEndPosition(CompilationUnitTree file, Tree tree) {
108          return getEndPosition(tree);
109      }
110 
111     /**
112      * {@return the ending position of the given {@link Tree}. If the ending position is not available,
113      * returns {@link javax.tools.Diagnostic#NOPOS}}
114      *
115      * <p>The returned position must be at the end of the yield of this tree, that is for any sub-tree of this tree,
116      * the following must hold:
117      *
118      * <p>
119      * {@code getEndPosition(tree) >= getEndPosition(subtree)} or <br>
120      * {@code getEndPosition(tree) == NOPOS} or <br>
121      * {@code getEndPosition(subtree) == NOPOS}
122      * </p>
123      *
124      * In addition, the following must hold:
125      *
126      * <p>
127      * {@code getStartPosition(tree) <= getEndPosition(tree)} or <br>
128      * {@code getStartPosition(tree) == NOPOS} or <br>
129      * {@code getEndPosition(tree) == NOPOS}
130      * </p>
131      *
132      * @param tree tree for which a position is sought
133      */
134     long getEndPosition(Tree tree);
135 }