72N/A/*
553N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
72N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
72N/A *
72N/A * This code is free software; you can redistribute it and/or modify it
72N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
72N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
72N/A *
72N/A * This code is distributed in the hope that it will be useful, but WITHOUT
72N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
72N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
72N/A * version 2 for more details (a copy is included in the LICENSE file that
72N/A * accompanied this code).
72N/A *
72N/A * You should have received a copy of the GNU General Public License version
72N/A * 2 along with this work; if not, write to the Free Software Foundation,
72N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
72N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
72N/A */
72N/A
72N/Apackage com.sun.tools.javac.util;
72N/A
72N/Aimport java.io.IOException;
72N/Aimport java.lang.ref.SoftReference;
72N/Aimport java.nio.CharBuffer;
72N/Aimport java.util.Map;
72N/Aimport javax.tools.JavaFileObject;
72N/A
72N/Aimport com.sun.tools.javac.file.JavacFileManager;
72N/Aimport com.sun.tools.javac.tree.JCTree;
72N/A
72N/Aimport static com.sun.tools.javac.util.LayoutCharacters.*;
72N/A
72N/A/**
72N/A * A simple abstraction of a source file, as needed for use in a diagnostic message.
72N/A * Provides access to the line and position in a line for any given character offset.
72N/A *
580N/A * <p><b>This is NOT part of any supported API.
580N/A * If you write code that depends on this, you do so at your own risk.
72N/A * This code and its internal interfaces are subject to change or
72N/A * deletion without notice.</b>
72N/A */
72N/Apublic class DiagnosticSource {
302N/A
302N/A /* constant DiagnosticSource to be used when sourcefile is missing */
302N/A public static final DiagnosticSource NO_SOURCE = new DiagnosticSource() {
302N/A @Override
302N/A protected boolean findLine(int pos) {
302N/A return false;
302N/A }
302N/A };
302N/A
72N/A public DiagnosticSource(JavaFileObject fo, AbstractLog log) {
72N/A this.fileObject = fo;
72N/A this.log = log;
72N/A }
72N/A
302N/A private DiagnosticSource() {}
302N/A
72N/A /** Return the underlying file object handled by this
72N/A * DiagnosticSource object.
72N/A */
72N/A public JavaFileObject getFile() {
72N/A return fileObject;
72N/A }
72N/A
72N/A /** Return the one-based line number associated with a given pos
72N/A * for the current source file. Zero is returned if no line exists
72N/A * for the given position.
72N/A */
72N/A public int getLineNumber(int pos) {
72N/A try {
72N/A if (findLine(pos)) {
72N/A return line;
72N/A }
72N/A return 0;
72N/A } finally {
72N/A buf = null;
72N/A }
72N/A }
72N/A
72N/A /** Return the one-based column number associated with a given pos
72N/A * for the current source file. Zero is returned if no column exists
72N/A * for the given position.
72N/A */
99N/A public int getColumnNumber(int pos, boolean expandTabs) {
72N/A try {
72N/A if (findLine(pos)) {
72N/A int column = 0;
72N/A for (int bp = lineStart; bp < pos; bp++) {
72N/A if (bp >= bufLen) {
72N/A return 0;
72N/A }
99N/A if (buf[bp] == '\t' && expandTabs) {
72N/A column = (column / TabInc * TabInc) + TabInc;
72N/A } else {
72N/A column++;
72N/A }
72N/A }
72N/A return column + 1; // positions are one-based
72N/A }
72N/A return 0;
72N/A } finally {
72N/A buf = null;
72N/A }
72N/A }
72N/A
72N/A /** Return the content of the line containing a given pos.
72N/A */
72N/A public String getLine(int pos) {
72N/A try {
72N/A if (!findLine(pos))
72N/A return null;
72N/A
72N/A int lineEnd = lineStart;
72N/A while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF)
72N/A lineEnd++;
72N/A if (lineEnd - lineStart == 0)
72N/A return null;
72N/A return new String(buf, lineStart, lineEnd - lineStart);
72N/A } finally {
72N/A buf = null;
72N/A }
72N/A }
72N/A
72N/A public Map<JCTree, Integer> getEndPosTable() {
72N/A return endPosTable;
72N/A }
72N/A
72N/A public void setEndPosTable(Map<JCTree, Integer> t) {
72N/A if (endPosTable != null && endPosTable != t)
72N/A throw new IllegalStateException("endPosTable already set");
72N/A endPosTable = t;
72N/A }
72N/A
72N/A /** Find the line in the buffer that contains the current position
72N/A * @param pos Character offset into the buffer
72N/A */
302N/A protected boolean findLine(int pos) {
72N/A if (pos == Position.NOPOS)
72N/A return false;
72N/A
72N/A try {
72N/A // try and recover buffer from soft reference cache
72N/A if (buf == null && refBuf != null)
72N/A buf = refBuf.get();
72N/A
72N/A if (buf == null) {
72N/A buf = initBuf(fileObject);
72N/A lineStart = 0;
72N/A line = 1;
72N/A } else if (lineStart > pos) { // messages don't come in order
72N/A lineStart = 0;
72N/A line = 1;
72N/A }
72N/A
72N/A int bp = lineStart;
72N/A while (bp < bufLen && bp < pos) {
72N/A switch (buf[bp++]) {
72N/A case CR:
72N/A if (bp < bufLen && buf[bp] == LF) bp++;
72N/A line++;
72N/A lineStart = bp;
72N/A break;
72N/A case LF:
72N/A line++;
72N/A lineStart = bp;
72N/A break;
72N/A }
72N/A }
72N/A return bp <= bufLen;
72N/A } catch (IOException e) {
72N/A log.directError("source.unavailable");
72N/A buf = new char[0];
72N/A return false;
72N/A }
72N/A }
72N/A
72N/A protected char[] initBuf(JavaFileObject fileObject) throws IOException {
72N/A char[] buf;
72N/A CharSequence cs = fileObject.getCharContent(true);
72N/A if (cs instanceof CharBuffer) {
72N/A CharBuffer cb = (CharBuffer) cs;
72N/A buf = JavacFileManager.toArray(cb);
72N/A bufLen = cb.limit();
72N/A } else {
72N/A buf = cs.toString().toCharArray();
72N/A bufLen = buf.length;
72N/A }
72N/A refBuf = new SoftReference<char[]>(buf);
72N/A return buf;
72N/A }
72N/A
72N/A /** The underlying file object. */
72N/A protected JavaFileObject fileObject;
72N/A
72N/A protected Map<JCTree, Integer> endPosTable;
72N/A
72N/A /** A soft reference to the content of the file object. */
72N/A protected SoftReference<char[]> refBuf;
72N/A
72N/A /** A temporary hard reference to the content of the file object. */
72N/A protected char[] buf;
72N/A
72N/A /** The length of the content. */
72N/A protected int bufLen;
72N/A
72N/A /** The start of a line found by findLine. */
72N/A protected int lineStart;
72N/A
72N/A /** The line number of a line found by findLine. */
72N/A protected int line;
72N/A
72N/A /** A log for reporting errors, such as errors accessing the content. */
72N/A protected AbstractLog log;
72N/A}