Wed, 24 Jun 2009 10:50:54 +0100
6852595: Accessing scope using JSR199 API on erroneous tree causes Illegal Argument Exception
Summary: Fixed problem with empty DiagnosticSource objects causing IAE in the JCDiagnostic constructor
Reviewed-by: jjg
1 /*
2 * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * 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 */
26 package com.sun.tools.javac.util;
28 import java.io.IOException;
29 import java.lang.ref.SoftReference;
30 import java.nio.CharBuffer;
31 import java.util.Map;
32 import javax.tools.JavaFileObject;
34 import com.sun.tools.javac.file.JavacFileManager;
35 import com.sun.tools.javac.tree.JCTree;
37 import static com.sun.tools.javac.util.LayoutCharacters.*;
39 /**
40 * A simple abstraction of a source file, as needed for use in a diagnostic message.
41 * Provides access to the line and position in a line for any given character offset.
42 *
43 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
44 * you write code that depends on this, you do so at your own risk.
45 * This code and its internal interfaces are subject to change or
46 * deletion without notice.</b>
47 */
48 public class DiagnosticSource {
50 /* constant DiagnosticSource to be used when sourcefile is missing */
51 public static final DiagnosticSource NO_SOURCE = new DiagnosticSource() {
52 @Override
53 protected boolean findLine(int pos) {
54 return false;
55 }
56 };
58 public DiagnosticSource(JavaFileObject fo, AbstractLog log) {
59 this.fileObject = fo;
60 this.log = log;
61 }
63 private DiagnosticSource() {}
65 /** Return the underlying file object handled by this
66 * DiagnosticSource object.
67 */
68 public JavaFileObject getFile() {
69 return fileObject;
70 }
72 public CharSequence getName() {
73 return JavacFileManager.getJavacBaseFileName(fileObject);
74 }
76 /** Return the one-based line number associated with a given pos
77 * for the current source file. Zero is returned if no line exists
78 * for the given position.
79 */
80 public int getLineNumber(int pos) {
81 try {
82 if (findLine(pos)) {
83 return line;
84 }
85 return 0;
86 } finally {
87 buf = null;
88 }
89 }
91 /** Return the one-based column number associated with a given pos
92 * for the current source file. Zero is returned if no column exists
93 * for the given position.
94 */
95 public int getColumnNumber(int pos, boolean expandTabs) {
96 try {
97 if (findLine(pos)) {
98 int column = 0;
99 for (int bp = lineStart; bp < pos; bp++) {
100 if (bp >= bufLen) {
101 return 0;
102 }
103 if (buf[bp] == '\t' && expandTabs) {
104 column = (column / TabInc * TabInc) + TabInc;
105 } else {
106 column++;
107 }
108 }
109 return column + 1; // positions are one-based
110 }
111 return 0;
112 } finally {
113 buf = null;
114 }
115 }
117 /** Return the content of the line containing a given pos.
118 */
119 public String getLine(int pos) {
120 try {
121 if (!findLine(pos))
122 return null;
124 int lineEnd = lineStart;
125 while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF)
126 lineEnd++;
127 if (lineEnd - lineStart == 0)
128 return null;
129 return new String(buf, lineStart, lineEnd - lineStart);
130 } finally {
131 buf = null;
132 }
133 }
135 public Map<JCTree, Integer> getEndPosTable() {
136 return endPosTable;
137 }
139 public void setEndPosTable(Map<JCTree, Integer> t) {
140 if (endPosTable != null && endPosTable != t)
141 throw new IllegalStateException("endPosTable already set");
142 endPosTable = t;
143 }
145 /** Find the line in the buffer that contains the current position
146 * @param pos Character offset into the buffer
147 */
148 protected boolean findLine(int pos) {
149 if (pos == Position.NOPOS)
150 return false;
152 try {
153 // try and recover buffer from soft reference cache
154 if (buf == null && refBuf != null)
155 buf = refBuf.get();
157 if (buf == null) {
158 buf = initBuf(fileObject);
159 lineStart = 0;
160 line = 1;
161 } else if (lineStart > pos) { // messages don't come in order
162 lineStart = 0;
163 line = 1;
164 }
166 int bp = lineStart;
167 while (bp < bufLen && bp < pos) {
168 switch (buf[bp++]) {
169 case CR:
170 if (bp < bufLen && buf[bp] == LF) bp++;
171 line++;
172 lineStart = bp;
173 break;
174 case LF:
175 line++;
176 lineStart = bp;
177 break;
178 }
179 }
180 return bp <= bufLen;
181 } catch (IOException e) {
182 log.directError("source.unavailable");
183 buf = new char[0];
184 return false;
185 }
186 }
188 protected char[] initBuf(JavaFileObject fileObject) throws IOException {
189 char[] buf;
190 CharSequence cs = fileObject.getCharContent(true);
191 if (cs instanceof CharBuffer) {
192 CharBuffer cb = (CharBuffer) cs;
193 buf = JavacFileManager.toArray(cb);
194 bufLen = cb.limit();
195 } else {
196 buf = cs.toString().toCharArray();
197 bufLen = buf.length;
198 }
199 refBuf = new SoftReference<char[]>(buf);
200 return buf;
201 }
203 /** The underlying file object. */
204 protected JavaFileObject fileObject;
206 protected Map<JCTree, Integer> endPosTable;
208 /** A soft reference to the content of the file object. */
209 protected SoftReference<char[]> refBuf;
211 /** A temporary hard reference to the content of the file object. */
212 protected char[] buf;
214 /** The length of the content. */
215 protected int bufLen;
217 /** The start of a line found by findLine. */
218 protected int lineStart;
220 /** The line number of a line found by findLine. */
221 protected int line;
223 /** A log for reporting errors, such as errors accessing the content. */
224 protected AbstractLog log;
225 }