src/share/classes/com/sun/tools/javac/util/DiagnosticSource.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1357
c75be5bc5283
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.javac.util;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.lang.ref.SoftReference;
aoqi@0 30 import java.nio.CharBuffer;
aoqi@0 31 import javax.tools.JavaFileObject;
aoqi@0 32
aoqi@0 33 import com.sun.tools.javac.file.JavacFileManager;
aoqi@0 34 import com.sun.tools.javac.tree.EndPosTable;
aoqi@0 35
aoqi@0 36 import static com.sun.tools.javac.util.LayoutCharacters.*;
aoqi@0 37
aoqi@0 38 /**
aoqi@0 39 * A simple abstraction of a source file, as needed for use in a diagnostic message.
aoqi@0 40 * Provides access to the line and position in a line for any given character offset.
aoqi@0 41 *
aoqi@0 42 * <p><b>This is NOT part of any supported API.
aoqi@0 43 * If you write code that depends on this, you do so at your own risk.
aoqi@0 44 * This code and its internal interfaces are subject to change or
aoqi@0 45 * deletion without notice.</b>
aoqi@0 46 */
aoqi@0 47 public class DiagnosticSource {
aoqi@0 48
aoqi@0 49 /* constant DiagnosticSource to be used when sourcefile is missing */
aoqi@0 50 public static final DiagnosticSource NO_SOURCE = new DiagnosticSource() {
aoqi@0 51 @Override
aoqi@0 52 protected boolean findLine(int pos) {
aoqi@0 53 return false;
aoqi@0 54 }
aoqi@0 55 };
aoqi@0 56
aoqi@0 57 public DiagnosticSource(JavaFileObject fo, AbstractLog log) {
aoqi@0 58 this.fileObject = fo;
aoqi@0 59 this.log = log;
aoqi@0 60 }
aoqi@0 61
aoqi@0 62 private DiagnosticSource() {}
aoqi@0 63
aoqi@0 64 /** Return the underlying file object handled by this
aoqi@0 65 * DiagnosticSource object.
aoqi@0 66 */
aoqi@0 67 public JavaFileObject getFile() {
aoqi@0 68 return fileObject;
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 /** Return the one-based line number associated with a given pos
aoqi@0 72 * for the current source file. Zero is returned if no line exists
aoqi@0 73 * for the given position.
aoqi@0 74 */
aoqi@0 75 public int getLineNumber(int pos) {
aoqi@0 76 try {
aoqi@0 77 if (findLine(pos)) {
aoqi@0 78 return line;
aoqi@0 79 }
aoqi@0 80 return 0;
aoqi@0 81 } finally {
aoqi@0 82 buf = null;
aoqi@0 83 }
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 /** Return the one-based column number associated with a given pos
aoqi@0 87 * for the current source file. Zero is returned if no column exists
aoqi@0 88 * for the given position.
aoqi@0 89 */
aoqi@0 90 public int getColumnNumber(int pos, boolean expandTabs) {
aoqi@0 91 try {
aoqi@0 92 if (findLine(pos)) {
aoqi@0 93 int column = 0;
aoqi@0 94 for (int bp = lineStart; bp < pos; bp++) {
aoqi@0 95 if (bp >= bufLen) {
aoqi@0 96 return 0;
aoqi@0 97 }
aoqi@0 98 if (buf[bp] == '\t' && expandTabs) {
aoqi@0 99 column = (column / TabInc * TabInc) + TabInc;
aoqi@0 100 } else {
aoqi@0 101 column++;
aoqi@0 102 }
aoqi@0 103 }
aoqi@0 104 return column + 1; // positions are one-based
aoqi@0 105 }
aoqi@0 106 return 0;
aoqi@0 107 } finally {
aoqi@0 108 buf = null;
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 /** Return the content of the line containing a given pos.
aoqi@0 113 */
aoqi@0 114 public String getLine(int pos) {
aoqi@0 115 try {
aoqi@0 116 if (!findLine(pos))
aoqi@0 117 return null;
aoqi@0 118
aoqi@0 119 int lineEnd = lineStart;
aoqi@0 120 while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF)
aoqi@0 121 lineEnd++;
aoqi@0 122 if (lineEnd - lineStart == 0)
aoqi@0 123 return null;
aoqi@0 124 return new String(buf, lineStart, lineEnd - lineStart);
aoqi@0 125 } finally {
aoqi@0 126 buf = null;
aoqi@0 127 }
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 public EndPosTable getEndPosTable() {
aoqi@0 131 return endPosTable;
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 public void setEndPosTable(EndPosTable t) {
aoqi@0 135 if (endPosTable != null && endPosTable != t)
aoqi@0 136 throw new IllegalStateException("endPosTable already set");
aoqi@0 137 endPosTable = t;
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 /** Find the line in the buffer that contains the current position
aoqi@0 141 * @param pos Character offset into the buffer
aoqi@0 142 */
aoqi@0 143 protected boolean findLine(int pos) {
aoqi@0 144 if (pos == Position.NOPOS)
aoqi@0 145 return false;
aoqi@0 146
aoqi@0 147 try {
aoqi@0 148 // try and recover buffer from soft reference cache
aoqi@0 149 if (buf == null && refBuf != null)
aoqi@0 150 buf = refBuf.get();
aoqi@0 151
aoqi@0 152 if (buf == null) {
aoqi@0 153 buf = initBuf(fileObject);
aoqi@0 154 lineStart = 0;
aoqi@0 155 line = 1;
aoqi@0 156 } else if (lineStart > pos) { // messages don't come in order
aoqi@0 157 lineStart = 0;
aoqi@0 158 line = 1;
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 int bp = lineStart;
aoqi@0 162 while (bp < bufLen && bp < pos) {
aoqi@0 163 switch (buf[bp++]) {
aoqi@0 164 case CR:
aoqi@0 165 if (bp < bufLen && buf[bp] == LF) bp++;
aoqi@0 166 line++;
aoqi@0 167 lineStart = bp;
aoqi@0 168 break;
aoqi@0 169 case LF:
aoqi@0 170 line++;
aoqi@0 171 lineStart = bp;
aoqi@0 172 break;
aoqi@0 173 }
aoqi@0 174 }
aoqi@0 175 return bp <= bufLen;
aoqi@0 176 } catch (IOException e) {
aoqi@0 177 log.directError("source.unavailable");
aoqi@0 178 buf = new char[0];
aoqi@0 179 return false;
aoqi@0 180 }
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 protected char[] initBuf(JavaFileObject fileObject) throws IOException {
aoqi@0 184 char[] buf;
aoqi@0 185 CharSequence cs = fileObject.getCharContent(true);
aoqi@0 186 if (cs instanceof CharBuffer) {
aoqi@0 187 CharBuffer cb = (CharBuffer) cs;
aoqi@0 188 buf = JavacFileManager.toArray(cb);
aoqi@0 189 bufLen = cb.limit();
aoqi@0 190 } else {
aoqi@0 191 buf = cs.toString().toCharArray();
aoqi@0 192 bufLen = buf.length;
aoqi@0 193 }
aoqi@0 194 refBuf = new SoftReference<char[]>(buf);
aoqi@0 195 return buf;
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 /** The underlying file object. */
aoqi@0 199 protected JavaFileObject fileObject;
aoqi@0 200
aoqi@0 201 protected EndPosTable endPosTable;
aoqi@0 202
aoqi@0 203 /** A soft reference to the content of the file object. */
aoqi@0 204 protected SoftReference<char[]> refBuf;
aoqi@0 205
aoqi@0 206 /** A temporary hard reference to the content of the file object. */
aoqi@0 207 protected char[] buf;
aoqi@0 208
aoqi@0 209 /** The length of the content. */
aoqi@0 210 protected int bufLen;
aoqi@0 211
aoqi@0 212 /** The start of a line found by findLine. */
aoqi@0 213 protected int lineStart;
aoqi@0 214
aoqi@0 215 /** The line number of a line found by findLine. */
aoqi@0 216 protected int line;
aoqi@0 217
aoqi@0 218 /** A log for reporting errors, such as errors accessing the content. */
aoqi@0 219 protected AbstractLog log;
aoqi@0 220 }

mercurial