01: /*
02: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25:
26: package com.sun.tools.javac.model;
27:
28: import javax.tools.JavaFileObject;
29: import com.sun.tools.javac.util.Position;
30: import com.sun.tools.javac.util.Version;
31:
32: /**
33: * Implementation of model API SourcePosition based on javac internal state.
34: *
35: * <p><b>This is NOT part of any API supported by Sun Microsystems.
36: * If you write code that depends on this, you do so at your own
37: * risk. This code and its internal interfaces are subject to change
38: * or deletion without notice.</b></p>
39: */
40: @Version("@(#)JavacSourcePosition.java 1.8 07/05/05")
41: class JavacSourcePosition {
42:
43: final JavaFileObject sourcefile;
44: final int pos;
45: final Position.LineMap lineMap;
46:
47: JavacSourcePosition(JavaFileObject sourcefile, int pos,
48: Position.LineMap lineMap) {
49: this .sourcefile = sourcefile;
50: this .pos = pos;
51: this .lineMap = (pos != Position.NOPOS) ? lineMap : null;
52: }
53:
54: public JavaFileObject getFile() {
55: return sourcefile;
56: }
57:
58: public int getOffset() {
59: return pos; // makes use of fact that Position.NOPOS == -1
60: }
61:
62: public int getLine() {
63: return (lineMap != null) ? lineMap.getLineNumber(pos) : -1;
64: }
65:
66: public int getColumn() {
67: return (lineMap != null) ? lineMap.getColumnNumber(pos) : -1;
68: }
69:
70: public String toString() {
71: int line = getLine();
72: return (line > 0) ? sourcefile + ":" + line : sourcefile
73: .toString();
74: }
75: }
|