author | ohair |
Tue, 28 Dec 2010 15:54:52 -0800 | |
changeset 7681 | 1f0819a3341f |
parent 7335 | 8b390fd27190 |
child 10461 | 998e642f9811 |
permissions | -rw-r--r-- |
4548 | 1 |
/* |
7681 | 2 |
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. |
4548 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
4548 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
4548 | 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 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
4548 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.util; |
|
27 |
||
28 |
import com.sun.tools.javac.code.Source; |
|
29 |
import com.sun.tools.javac.main.JavacOption; |
|
30 |
import com.sun.tools.javac.main.OptionName; |
|
31 |
import com.sun.tools.javac.main.RecognizedOptions; |
|
32 |
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition; |
|
33 |
import java.io.ByteArrayOutputStream; |
|
34 |
import java.io.Closeable; |
|
35 |
import java.io.IOException; |
|
36 |
import java.io.InputStream; |
|
37 |
import java.io.OutputStreamWriter; |
|
38 |
import java.lang.ref.SoftReference; |
|
39 |
import java.lang.reflect.Constructor; |
|
40 |
import java.net.URL; |
|
41 |
import java.net.URLClassLoader; |
|
42 |
import java.nio.ByteBuffer; |
|
43 |
import java.nio.CharBuffer; |
|
44 |
import java.nio.charset.Charset; |
|
45 |
import java.nio.charset.CharsetDecoder; |
|
46 |
import java.nio.charset.CoderResult; |
|
47 |
import java.nio.charset.CodingErrorAction; |
|
48 |
import java.nio.charset.IllegalCharsetNameException; |
|
49 |
import java.nio.charset.UnsupportedCharsetException; |
|
50 |
import java.util.Collection; |
|
51 |
import java.util.HashMap; |
|
52 |
import java.util.Iterator; |
|
53 |
import java.util.Map; |
|
54 |
import javax.tools.JavaFileObject; |
|
55 |
import javax.tools.JavaFileObject.Kind; |
|
56 |
||
57 |
/** |
|
58 |
* Utility methods for building a filemanager. |
|
59 |
* There are no references here to file-system specific objects such as |
|
60 |
* java.io.File or java.nio.file.Path. |
|
61 |
*/ |
|
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
5520
diff
changeset
|
62 |
public abstract class BaseFileManager { |
4548 | 63 |
protected BaseFileManager(Charset charset) { |
64 |
this.charset = charset; |
|
65 |
byteBufferCache = new ByteBufferCache(); |
|
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Set the context for JavacPathFileManager. |
|
70 |
*/ |
|
71 |
protected void setContext(Context context) { |
|
72 |
log = Log.instance(context); |
|
73 |
options = Options.instance(context); |
|
74 |
classLoaderClass = options.get("procloader"); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* The log to be used for error reporting. |
|
79 |
*/ |
|
80 |
public Log log; |
|
81 |
||
82 |
/** |
|
83 |
* User provided charset (through javax.tools). |
|
84 |
*/ |
|
85 |
protected Charset charset; |
|
86 |
||
87 |
protected Options options; |
|
88 |
||
89 |
protected String classLoaderClass; |
|
90 |
||
91 |
protected Source getSource() { |
|
92 |
String sourceName = options.get(OptionName.SOURCE); |
|
93 |
Source source = null; |
|
94 |
if (sourceName != null) |
|
95 |
source = Source.lookup(sourceName); |
|
96 |
return (source != null ? source : Source.DEFAULT); |
|
97 |
} |
|
98 |
||
99 |
protected ClassLoader getClassLoader(URL[] urls) { |
|
100 |
ClassLoader thisClassLoader = getClass().getClassLoader(); |
|
101 |
||
102 |
// Bug: 6558476 |
|
103 |
// Ideally, ClassLoader should be Closeable, but before JDK7 it is not. |
|
104 |
// On older versions, try the following, to get a closeable classloader. |
|
105 |
||
106 |
// 1: Allow client to specify the class to use via hidden option |
|
107 |
if (classLoaderClass != null) { |
|
108 |
try { |
|
109 |
Class<? extends ClassLoader> loader = |
|
110 |
Class.forName(classLoaderClass).asSubclass(ClassLoader.class); |
|
111 |
Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class }; |
|
112 |
Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes); |
|
113 |
return constr.newInstance(new Object[] { urls, thisClassLoader }); |
|
114 |
} catch (Throwable t) { |
|
115 |
// ignore errors loading user-provided class loader, fall through |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
// 2: If URLClassLoader implements Closeable, use that. |
|
120 |
if (Closeable.class.isAssignableFrom(URLClassLoader.class)) |
|
121 |
return new URLClassLoader(urls, thisClassLoader); |
|
122 |
||
123 |
// 3: Try using private reflection-based CloseableURLClassLoader |
|
124 |
try { |
|
125 |
return new CloseableURLClassLoader(urls, thisClassLoader); |
|
126 |
} catch (Throwable t) { |
|
127 |
// ignore errors loading workaround class loader, fall through |
|
128 |
} |
|
129 |
||
130 |
// 4: If all else fails, use plain old standard URLClassLoader |
|
131 |
return new URLClassLoader(urls, thisClassLoader); |
|
132 |
} |
|
133 |
||
134 |
// <editor-fold defaultstate="collapsed" desc="Option handling"> |
|
135 |
public boolean handleOption(String current, Iterator<String> remaining) { |
|
136 |
for (JavacOption o: javacFileManagerOptions) { |
|
137 |
if (o.matches(current)) { |
|
138 |
if (o.hasArg()) { |
|
139 |
if (remaining.hasNext()) { |
|
140 |
if (!o.process(options, current, remaining.next())) |
|
141 |
return true; |
|
142 |
} |
|
143 |
} else { |
|
144 |
if (!o.process(options, current)) |
|
145 |
return true; |
|
146 |
} |
|
147 |
// operand missing, or process returned false |
|
148 |
throw new IllegalArgumentException(current); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
return false; |
|
153 |
} |
|
154 |
// where |
|
155 |
private static JavacOption[] javacFileManagerOptions = |
|
156 |
RecognizedOptions.getJavacFileManagerOptions( |
|
157 |
new RecognizedOptions.GrumpyHelper()); |
|
158 |
||
159 |
public int isSupportedOption(String option) { |
|
160 |
for (JavacOption o : javacFileManagerOptions) { |
|
161 |
if (o.matches(option)) |
|
162 |
return o.hasArg() ? 1 : 0; |
|
163 |
} |
|
164 |
return -1; |
|
165 |
} |
|
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
5520
diff
changeset
|
166 |
|
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
5520
diff
changeset
|
167 |
public abstract boolean isDefaultBootClassPath(); |
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
5520
diff
changeset
|
168 |
|
4548 | 169 |
// </editor-fold> |
170 |
||
171 |
// <editor-fold defaultstate="collapsed" desc="Encoding"> |
|
172 |
private String defaultEncodingName; |
|
173 |
private String getDefaultEncodingName() { |
|
174 |
if (defaultEncodingName == null) { |
|
175 |
defaultEncodingName = |
|
176 |
new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding(); |
|
177 |
} |
|
178 |
return defaultEncodingName; |
|
179 |
} |
|
180 |
||
181 |
public String getEncodingName() { |
|
182 |
String encName = options.get(OptionName.ENCODING); |
|
183 |
if (encName == null) |
|
184 |
return getDefaultEncodingName(); |
|
185 |
else |
|
186 |
return encName; |
|
187 |
} |
|
188 |
||
189 |
public CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) { |
|
190 |
String encodingName = getEncodingName(); |
|
191 |
CharsetDecoder decoder; |
|
192 |
try { |
|
193 |
decoder = getDecoder(encodingName, ignoreEncodingErrors); |
|
194 |
} catch (IllegalCharsetNameException e) { |
|
195 |
log.error("unsupported.encoding", encodingName); |
|
196 |
return (CharBuffer)CharBuffer.allocate(1).flip(); |
|
197 |
} catch (UnsupportedCharsetException e) { |
|
198 |
log.error("unsupported.encoding", encodingName); |
|
199 |
return (CharBuffer)CharBuffer.allocate(1).flip(); |
|
200 |
} |
|
201 |
||
202 |
// slightly overestimate the buffer size to avoid reallocation. |
|
203 |
float factor = |
|
204 |
decoder.averageCharsPerByte() * 0.8f + |
|
205 |
decoder.maxCharsPerByte() * 0.2f; |
|
206 |
CharBuffer dest = CharBuffer. |
|
207 |
allocate(10 + (int)(inbuf.remaining()*factor)); |
|
208 |
||
209 |
while (true) { |
|
210 |
CoderResult result = decoder.decode(inbuf, dest, true); |
|
211 |
dest.flip(); |
|
212 |
||
213 |
if (result.isUnderflow()) { // done reading |
|
214 |
// make sure there is at least one extra character |
|
215 |
if (dest.limit() == dest.capacity()) { |
|
216 |
dest = CharBuffer.allocate(dest.capacity()+1).put(dest); |
|
217 |
dest.flip(); |
|
218 |
} |
|
219 |
return dest; |
|
220 |
} else if (result.isOverflow()) { // buffer too small; expand |
|
221 |
int newCapacity = |
|
222 |
10 + dest.capacity() + |
|
223 |
(int)(inbuf.remaining()*decoder.maxCharsPerByte()); |
|
224 |
dest = CharBuffer.allocate(newCapacity).put(dest); |
|
225 |
} else if (result.isMalformed() || result.isUnmappable()) { |
|
226 |
// bad character in input |
|
227 |
||
228 |
// report coding error (warn only pre 1.5) |
|
229 |
if (!getSource().allowEncodingErrors()) { |
|
230 |
log.error(new SimpleDiagnosticPosition(dest.limit()), |
|
231 |
"illegal.char.for.encoding", |
|
232 |
charset == null ? encodingName : charset.name()); |
|
233 |
} else { |
|
234 |
log.warning(new SimpleDiagnosticPosition(dest.limit()), |
|
235 |
"illegal.char.for.encoding", |
|
236 |
charset == null ? encodingName : charset.name()); |
|
237 |
} |
|
238 |
||
239 |
// skip past the coding error |
|
240 |
inbuf.position(inbuf.position() + result.length()); |
|
241 |
||
242 |
// undo the flip() to prepare the output buffer |
|
243 |
// for more translation |
|
244 |
dest.position(dest.limit()); |
|
245 |
dest.limit(dest.capacity()); |
|
246 |
dest.put((char)0xfffd); // backward compatible |
|
247 |
} else { |
|
248 |
throw new AssertionError(result); |
|
249 |
} |
|
250 |
} |
|
251 |
// unreached |
|
252 |
} |
|
253 |
||
254 |
public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) { |
|
255 |
Charset cs = (this.charset == null) |
|
256 |
? Charset.forName(encodingName) |
|
257 |
: this.charset; |
|
258 |
CharsetDecoder decoder = cs.newDecoder(); |
|
259 |
||
260 |
CodingErrorAction action; |
|
261 |
if (ignoreEncodingErrors) |
|
262 |
action = CodingErrorAction.REPLACE; |
|
263 |
else |
|
264 |
action = CodingErrorAction.REPORT; |
|
265 |
||
266 |
return decoder |
|
267 |
.onMalformedInput(action) |
|
268 |
.onUnmappableCharacter(action); |
|
269 |
} |
|
270 |
// </editor-fold> |
|
271 |
||
272 |
// <editor-fold defaultstate="collapsed" desc="ByteBuffers"> |
|
273 |
/** |
|
274 |
* Make a byte buffer from an input stream. |
|
275 |
*/ |
|
276 |
public ByteBuffer makeByteBuffer(InputStream in) |
|
277 |
throws IOException { |
|
278 |
int limit = in.available(); |
|
279 |
if (limit < 1024) limit = 1024; |
|
280 |
ByteBuffer result = byteBufferCache.get(limit); |
|
281 |
int position = 0; |
|
282 |
while (in.available() != 0) { |
|
283 |
if (position >= limit) |
|
284 |
// expand buffer |
|
285 |
result = ByteBuffer. |
|
286 |
allocate(limit <<= 1). |
|
287 |
put((ByteBuffer)result.flip()); |
|
288 |
int count = in.read(result.array(), |
|
289 |
position, |
|
290 |
limit - position); |
|
291 |
if (count < 0) break; |
|
292 |
result.position(position += count); |
|
293 |
} |
|
294 |
return (ByteBuffer)result.flip(); |
|
295 |
} |
|
296 |
||
297 |
public void recycleByteBuffer(ByteBuffer bb) { |
|
298 |
byteBufferCache.put(bb); |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* A single-element cache of direct byte buffers. |
|
303 |
*/ |
|
304 |
private static class ByteBufferCache { |
|
305 |
private ByteBuffer cached; |
|
306 |
ByteBuffer get(int capacity) { |
|
307 |
if (capacity < 20480) capacity = 20480; |
|
308 |
ByteBuffer result = |
|
309 |
(cached != null && cached.capacity() >= capacity) |
|
310 |
? (ByteBuffer)cached.clear() |
|
311 |
: ByteBuffer.allocate(capacity + capacity>>1); |
|
312 |
cached = null; |
|
313 |
return result; |
|
314 |
} |
|
315 |
void put(ByteBuffer x) { |
|
316 |
cached = x; |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
private final ByteBufferCache byteBufferCache; |
|
321 |
// </editor-fold> |
|
322 |
||
323 |
// <editor-fold defaultstate="collapsed" desc="Content cache"> |
|
324 |
public CharBuffer getCachedContent(JavaFileObject file) { |
|
325 |
SoftReference<CharBuffer> r = contentCache.get(file); |
|
326 |
return (r == null ? null : r.get()); |
|
327 |
} |
|
328 |
||
329 |
public void cache(JavaFileObject file, CharBuffer cb) { |
|
330 |
contentCache.put(file, new SoftReference<CharBuffer>(cb)); |
|
331 |
} |
|
332 |
||
333 |
protected final Map<JavaFileObject, SoftReference<CharBuffer>> contentCache |
|
334 |
= new HashMap<JavaFileObject, SoftReference<CharBuffer>>(); |
|
335 |
// </editor-fold> |
|
336 |
||
337 |
public static Kind getKind(String name) { |
|
338 |
if (name.endsWith(Kind.CLASS.extension)) |
|
339 |
return Kind.CLASS; |
|
340 |
else if (name.endsWith(Kind.SOURCE.extension)) |
|
341 |
return Kind.SOURCE; |
|
342 |
else if (name.endsWith(Kind.HTML.extension)) |
|
343 |
return Kind.HTML; |
|
344 |
else |
|
345 |
return Kind.OTHER; |
|
346 |
} |
|
347 |
||
348 |
protected static <T> T nullCheck(T o) { |
|
349 |
o.getClass(); // null check |
|
350 |
return o; |
|
351 |
} |
|
352 |
||
353 |
protected static <T> Collection<T> nullCheck(Collection<T> it) { |
|
354 |
for (T t : it) |
|
355 |
t.getClass(); // null check |
|
356 |
return it; |
|
357 |
} |
|
358 |
} |