jjg@73: /* jjg@1280: * Copyright (c) 1999, 2012, Oracle and/or its affiliates. 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 ohair@554: * published by the Free Software Foundation. Oracle designates this jjg@73: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle 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: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * 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 javax.tools.JavaFileObject; jjg@73: jjg@73: import com.sun.tools.javac.file.JavacFileManager; jjg@1280: import com.sun.tools.javac.tree.EndPosTable; 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@581: *
This is NOT part of any supported API.
jjg@581: * If 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 {
mcimadamore@303:
mcimadamore@303: /* constant DiagnosticSource to be used when sourcefile is missing */
mcimadamore@303: public static final DiagnosticSource NO_SOURCE = new DiagnosticSource() {
mcimadamore@303: @Override
mcimadamore@303: protected boolean findLine(int pos) {
mcimadamore@303: return false;
mcimadamore@303: }
mcimadamore@303: };
mcimadamore@303:
jjg@73: public DiagnosticSource(JavaFileObject fo, AbstractLog log) {
jjg@73: this.fileObject = fo;
jjg@73: this.log = log;
jjg@73: }
jjg@73:
mcimadamore@303: private DiagnosticSource() {}
mcimadamore@303:
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: /** 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:
ksrini@1138: public EndPosTable getEndPosTable() {
jjg@73: return endPosTable;
jjg@73: }
jjg@73:
ksrini@1138: public void setEndPosTable(EndPosTable t) {
jjg@73: if (endPosTable != null && endPosTable != t)
jjg@73: throw new IllegalStateException("endPosTable already set");
jjg@73: endPosTable = t;
jjg@73: }
jjg@73:
jjg@73: /** Find the line in the buffer that contains the current position
jjg@73: * @param pos Character offset into the buffer
jjg@73: */
mcimadamore@303: protected boolean findLine(int pos) {
jjg@73: if (pos == Position.NOPOS)
jjg@73: return false;
jjg@73:
jjg@73: try {
jjg@73: // try and recover buffer from soft reference cache
jjg@73: if (buf == null && refBuf != null)
jjg@73: buf = refBuf.get();
jjg@73:
jjg@73: if (buf == null) {
jjg@73: buf = initBuf(fileObject);
jjg@73: lineStart = 0;
jjg@73: line = 1;
jjg@73: } else if (lineStart > pos) { // messages don't come in order
jjg@73: lineStart = 0;
jjg@73: line = 1;
jjg@73: }
jjg@73:
jjg@73: int bp = lineStart;
jjg@73: while (bp < bufLen && bp < pos) {
jjg@73: switch (buf[bp++]) {
jjg@73: case CR:
jjg@73: if (bp < bufLen && buf[bp] == LF) bp++;
jjg@73: line++;
jjg@73: lineStart = bp;
jjg@73: break;
jjg@73: case LF:
jjg@73: line++;
jjg@73: lineStart = bp;
jjg@73: break;
jjg@73: }
jjg@73: }
jjg@73: return bp <= bufLen;
jjg@73: } catch (IOException e) {
jjg@73: log.directError("source.unavailable");
jjg@73: buf = new char[0];
jjg@73: return false;
jjg@73: }
jjg@73: }
jjg@73:
jjg@73: protected char[] initBuf(JavaFileObject fileObject) throws IOException {
jjg@73: char[] buf;
jjg@73: CharSequence cs = fileObject.getCharContent(true);
jjg@73: if (cs instanceof CharBuffer) {
jjg@73: CharBuffer cb = (CharBuffer) cs;
jjg@73: buf = JavacFileManager.toArray(cb);
jjg@73: bufLen = cb.limit();
jjg@73: } else {
jjg@73: buf = cs.toString().toCharArray();
jjg@73: bufLen = buf.length;
jjg@73: }
jjg@73: refBuf = new SoftReference