jjg@73: /* jjg@73: * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved. jjg@73: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@73: * jjg@73: * This code is free software; you can redistribute it and/or modify it jjg@73: * under the terms of the GNU General Public License version 2 only, as jjg@73: * published by the Free Software Foundation. Sun designates this jjg@73: * particular file as subject to the "Classpath" exception as provided jjg@73: * by Sun in the LICENSE file that accompanied this code. jjg@73: * jjg@73: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@73: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@73: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@73: * version 2 for more details (a copy is included in the LICENSE file that jjg@73: * accompanied this code). jjg@73: * jjg@73: * You should have received a copy of the GNU General Public License version jjg@73: * 2 along with this work; if not, write to the Free Software Foundation, jjg@73: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@73: * jjg@73: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@73: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@73: * have any questions. jjg@73: */ jjg@73: jjg@73: package com.sun.tools.javac.util; jjg@73: jjg@73: import java.io.IOException; jjg@73: import java.lang.ref.SoftReference; jjg@73: import java.nio.CharBuffer; jjg@73: import java.util.Map; jjg@73: import javax.tools.JavaFileObject; jjg@73: jjg@73: import com.sun.tools.javac.file.JavacFileManager; jjg@73: import com.sun.tools.javac.tree.JCTree; jjg@73: jjg@73: import static com.sun.tools.javac.util.LayoutCharacters.*; jjg@73: jjg@73: /** jjg@73: * A simple abstraction of a source file, as needed for use in a diagnostic message. jjg@73: * Provides access to the line and position in a line for any given character offset. jjg@73: * jjg@73: *
This is NOT part of any API supported by Sun Microsystems. If
jjg@73: * you write code that depends on this, you do so at your own risk.
jjg@73: * This code and its internal interfaces are subject to change or
jjg@73: * deletion without notice.
jjg@73: */
jjg@73: public class DiagnosticSource {
jjg@73: public DiagnosticSource(JavaFileObject fo, AbstractLog log) {
jjg@73: this.fileObject = fo;
jjg@73: this.log = log;
jjg@73: }
jjg@73:
jjg@73: /** Return the underlying file object handled by this
jjg@73: * DiagnosticSource object.
jjg@73: */
jjg@73: public JavaFileObject getFile() {
jjg@73: return fileObject;
jjg@73: }
jjg@73:
jjg@73: public CharSequence getName() {
jjg@73: return JavacFileManager.getJavacBaseFileName(fileObject);
jjg@73: }
jjg@73:
jjg@73: /** Return the one-based line number associated with a given pos
jjg@73: * for the current source file. Zero is returned if no line exists
jjg@73: * for the given position.
jjg@73: */
jjg@73: public int getLineNumber(int pos) {
jjg@73: try {
jjg@73: if (findLine(pos)) {
jjg@73: return line;
jjg@73: }
jjg@73: return 0;
jjg@73: } finally {
jjg@73: buf = null;
jjg@73: }
jjg@73: }
jjg@73:
jjg@73: /** Return the one-based column number associated with a given pos
jjg@73: * for the current source file. Zero is returned if no column exists
jjg@73: * for the given position.
jjg@73: */
mcimadamore@100: public int getColumnNumber(int pos, boolean expandTabs) {
jjg@73: try {
jjg@73: if (findLine(pos)) {
jjg@73: int column = 0;
jjg@73: for (int bp = lineStart; bp < pos; bp++) {
jjg@73: if (bp >= bufLen) {
jjg@73: return 0;
jjg@73: }
mcimadamore@100: if (buf[bp] == '\t' && expandTabs) {
jjg@73: column = (column / TabInc * TabInc) + TabInc;
jjg@73: } else {
jjg@73: column++;
jjg@73: }
jjg@73: }
jjg@73: return column + 1; // positions are one-based
jjg@73: }
jjg@73: return 0;
jjg@73: } finally {
jjg@73: buf = null;
jjg@73: }
jjg@73: }
jjg@73:
jjg@73: /** Return the content of the line containing a given pos.
jjg@73: */
jjg@73: public String getLine(int pos) {
jjg@73: try {
jjg@73: if (!findLine(pos))
jjg@73: return null;
jjg@73:
jjg@73: int lineEnd = lineStart;
jjg@73: while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF)
jjg@73: lineEnd++;
jjg@73: if (lineEnd - lineStart == 0)
jjg@73: return null;
jjg@73: return new String(buf, lineStart, lineEnd - lineStart);
jjg@73: } finally {
jjg@73: buf = null;
jjg@73: }
jjg@73: }
jjg@73:
jjg@73: public Map