author | tbell |
Fri, 25 Sep 2009 22:04:43 -0700 | |
changeset 3998 | c66be272f350 |
parent 3890 | b53fced26fa4 |
parent 3995 | 73af8b6fb8bc |
child 4073 | 9788f4549740 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
3890 | 2 |
* Copyright 2005-2009 Sun Microsystems, Inc. All Rights Reserved. |
10 | 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 |
*/ |
|
25 |
||
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
26 |
package com.sun.tools.javac.file; |
10 | 27 |
|
28 |
import java.io.ByteArrayOutputStream; |
|
3782 | 29 |
import java.io.Closeable; |
10 | 30 |
import java.io.File; |
31 |
import java.io.FileInputStream; |
|
32 |
import java.io.FileNotFoundException; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.InputStream; |
|
35 |
import java.io.OutputStreamWriter; |
|
36 |
import java.lang.ref.SoftReference; |
|
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
37 |
import java.lang.reflect.Constructor; |
10 | 38 |
import java.net.MalformedURLException; |
39 |
import java.net.URI; |
|
3782 | 40 |
import java.net.URISyntaxException; |
10 | 41 |
import java.net.URL; |
42 |
import java.net.URLClassLoader; |
|
43 |
import java.nio.ByteBuffer; |
|
44 |
import java.nio.CharBuffer; |
|
45 |
import java.nio.channels.FileChannel; |
|
46 |
import java.nio.charset.Charset; |
|
47 |
import java.nio.charset.CharsetDecoder; |
|
48 |
import java.nio.charset.CoderResult; |
|
49 |
import java.nio.charset.CodingErrorAction; |
|
50 |
import java.nio.charset.IllegalCharsetNameException; |
|
51 |
import java.nio.charset.UnsupportedCharsetException; |
|
52 |
import java.util.ArrayList; |
|
53 |
import java.util.Arrays; |
|
54 |
import java.util.Collection; |
|
55 |
import java.util.Collections; |
|
56 |
import java.util.EnumSet; |
|
57 |
import java.util.HashMap; |
|
58 |
import java.util.Iterator; |
|
59 |
import java.util.Map; |
|
60 |
import java.util.Set; |
|
61 |
import java.util.zip.ZipFile; |
|
62 |
||
63 |
import javax.lang.model.SourceVersion; |
|
64 |
import javax.tools.FileObject; |
|
65 |
import javax.tools.JavaFileManager; |
|
66 |
import javax.tools.JavaFileObject; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
67 |
import javax.tools.StandardJavaFileManager; |
10 | 68 |
|
69 |
import com.sun.tools.javac.code.Source; |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
70 |
import com.sun.tools.javac.file.RelativePath.RelativeFile; |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
71 |
import com.sun.tools.javac.file.RelativePath.RelativeDirectory; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
72 |
import com.sun.tools.javac.main.JavacOption; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
73 |
import com.sun.tools.javac.main.OptionName; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
74 |
import com.sun.tools.javac.main.RecognizedOptions; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
75 |
import com.sun.tools.javac.util.Context; |
10 | 76 |
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
77 |
import com.sun.tools.javac.util.List; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
78 |
import com.sun.tools.javac.util.ListBuffer; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
79 |
import com.sun.tools.javac.util.Log; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
80 |
import com.sun.tools.javac.util.Options; |
10 | 81 |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
82 |
import static javax.tools.StandardLocation.*; |
10 | 83 |
import static com.sun.tools.javac.main.OptionName.*; |
84 |
||
85 |
/** |
|
86 |
* This class provides access to the source, class and other files |
|
87 |
* used by the compiler and related tools. |
|
3380
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1789
diff
changeset
|
88 |
* |
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1789
diff
changeset
|
89 |
* <p><b>This is NOT part of any API supported by Sun Microsystems. |
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1789
diff
changeset
|
90 |
* If you write code that depends on this, you do so at your own risk. |
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1789
diff
changeset
|
91 |
* This code and its internal interfaces are subject to change or |
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1789
diff
changeset
|
92 |
* deletion without notice.</b> |
10 | 93 |
*/ |
94 |
public class JavacFileManager implements StandardJavaFileManager { |
|
95 |
||
96 |
boolean useZipFileIndex; |
|
97 |
||
98 |
public static char[] toArray(CharBuffer buffer) { |
|
99 |
if (buffer.hasArray()) |
|
100 |
return ((CharBuffer)buffer.compact().flip()).array(); |
|
101 |
else |
|
102 |
return buffer.toString().toCharArray(); |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* The log to be used for error reporting. |
|
107 |
*/ |
|
108 |
protected Log log; |
|
109 |
||
110 |
/** Encapsulates knowledge of paths |
|
111 |
*/ |
|
112 |
private Paths paths; |
|
113 |
||
114 |
private Options options; |
|
115 |
||
1208
5072b0dd3d52
6743107: clean up use of static caches in file manager
jjg
parents:
1205
diff
changeset
|
116 |
private FSInfo fsInfo; |
5072b0dd3d52
6743107: clean up use of static caches in file manager
jjg
parents:
1205
diff
changeset
|
117 |
|
10 | 118 |
private final File uninited = new File("U N I N I T E D"); |
119 |
||
120 |
private final Set<JavaFileObject.Kind> sourceOrClass = |
|
121 |
EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS); |
|
122 |
||
123 |
/** The standard output directory, primarily used for classes. |
|
124 |
* Initialized by the "-d" option. |
|
125 |
* If classOutDir = null, files are written into same directory as the sources |
|
126 |
* they were generated from. |
|
127 |
*/ |
|
128 |
private File classOutDir = uninited; |
|
129 |
||
130 |
/** The output directory, used when generating sources while processing annotations. |
|
131 |
* Initialized by the "-s" option. |
|
132 |
*/ |
|
133 |
private File sourceOutDir = uninited; |
|
134 |
||
135 |
protected boolean mmappedIO; |
|
136 |
protected boolean ignoreSymbolFile; |
|
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
137 |
protected String classLoaderClass; |
10 | 138 |
|
139 |
/** |
|
140 |
* User provided charset (through javax.tools). |
|
141 |
*/ |
|
142 |
protected Charset charset; |
|
143 |
||
144 |
/** |
|
145 |
* Register a Context.Factory to create a JavacFileManager. |
|
146 |
*/ |
|
147 |
public static void preRegister(final Context context) { |
|
148 |
context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() { |
|
149 |
public JavaFileManager make() { |
|
150 |
return new JavacFileManager(context, true, null); |
|
151 |
} |
|
152 |
}); |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Create a JavacFileManager using a given context, optionally registering |
|
157 |
* it as the JavaFileManager for that context. |
|
158 |
*/ |
|
159 |
public JavacFileManager(Context context, boolean register, Charset charset) { |
|
160 |
if (register) |
|
161 |
context.put(JavaFileManager.class, this); |
|
162 |
byteBufferCache = new ByteBufferCache(); |
|
163 |
this.charset = charset; |
|
164 |
setContext(context); |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* Set the context for JavacFileManager. |
|
169 |
*/ |
|
170 |
public void setContext(Context context) { |
|
171 |
log = Log.instance(context); |
|
172 |
if (paths == null) { |
|
173 |
paths = Paths.instance(context); |
|
174 |
} else { |
|
175 |
// Reuse the Paths object as it stores the locations that |
|
176 |
// have been set with setLocation, etc. |
|
177 |
paths.setContext(context); |
|
178 |
} |
|
179 |
||
180 |
options = Options.instance(context); |
|
1208
5072b0dd3d52
6743107: clean up use of static caches in file manager
jjg
parents:
1205
diff
changeset
|
181 |
fsInfo = FSInfo.instance(context); |
10 | 182 |
|
183 |
useZipFileIndex = System.getProperty("useJavaUtilZip") == null;// TODO: options.get("useJavaUtilZip") == null; |
|
184 |
||
185 |
mmappedIO = options.get("mmappedIO") != null; |
|
186 |
ignoreSymbolFile = options.get("ignore.symbol.file") != null; |
|
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
187 |
classLoaderClass = options.get("procloader"); |
10 | 188 |
} |
189 |
||
190 |
public JavaFileObject getFileForInput(String name) { |
|
191 |
return getRegularFile(new File(name)); |
|
192 |
} |
|
193 |
||
194 |
public JavaFileObject getRegularFile(File file) { |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
195 |
return new RegularFileObject(this, file); |
10 | 196 |
} |
197 |
||
198 |
public JavaFileObject getFileForOutput(String classname, |
|
199 |
JavaFileObject.Kind kind, |
|
200 |
JavaFileObject sibling) |
|
201 |
throws IOException |
|
202 |
{ |
|
203 |
return getJavaFileForOutput(CLASS_OUTPUT, classname, kind, sibling); |
|
204 |
} |
|
205 |
||
206 |
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) { |
|
207 |
ListBuffer<File> files = new ListBuffer<File>(); |
|
208 |
for (String name : names) |
|
209 |
files.append(new File(nullCheck(name))); |
|
210 |
return getJavaFileObjectsFromFiles(files.toList()); |
|
211 |
} |
|
212 |
||
213 |
public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) { |
|
214 |
return getJavaFileObjectsFromStrings(Arrays.asList(nullCheck(names))); |
|
215 |
} |
|
216 |
||
217 |
protected JavaFileObject.Kind getKind(String extension) { |
|
218 |
if (extension.equals(JavaFileObject.Kind.CLASS.extension)) |
|
219 |
return JavaFileObject.Kind.CLASS; |
|
220 |
else if (extension.equals(JavaFileObject.Kind.SOURCE.extension)) |
|
221 |
return JavaFileObject.Kind.SOURCE; |
|
222 |
else if (extension.equals(JavaFileObject.Kind.HTML.extension)) |
|
223 |
return JavaFileObject.Kind.HTML; |
|
224 |
else |
|
225 |
return JavaFileObject.Kind.OTHER; |
|
226 |
} |
|
227 |
||
228 |
private static boolean isValidName(String name) { |
|
229 |
// Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ), |
|
230 |
// but the set of keywords depends on the source level, and we don't want |
|
231 |
// impls of JavaFileManager to have to be dependent on the source level. |
|
232 |
// Therefore we simply check that the argument is a sequence of identifiers |
|
233 |
// separated by ".". |
|
234 |
for (String s : name.split("\\.", -1)) { |
|
235 |
if (!SourceVersion.isIdentifier(s)) |
|
236 |
return false; |
|
237 |
} |
|
238 |
return true; |
|
239 |
} |
|
240 |
||
241 |
private static void validateClassName(String className) { |
|
242 |
if (!isValidName(className)) |
|
243 |
throw new IllegalArgumentException("Invalid class name: " + className); |
|
244 |
} |
|
245 |
||
246 |
private static void validatePackageName(String packageName) { |
|
247 |
if (packageName.length() > 0 && !isValidName(packageName)) |
|
248 |
throw new IllegalArgumentException("Invalid packageName name: " + packageName); |
|
249 |
} |
|
250 |
||
251 |
public static void testName(String name, |
|
252 |
boolean isValidPackageName, |
|
253 |
boolean isValidClassName) |
|
254 |
{ |
|
255 |
try { |
|
256 |
validatePackageName(name); |
|
257 |
if (!isValidPackageName) |
|
258 |
throw new AssertionError("Invalid package name accepted: " + name); |
|
259 |
printAscii("Valid package name: \"%s\"", name); |
|
260 |
} catch (IllegalArgumentException e) { |
|
261 |
if (isValidPackageName) |
|
262 |
throw new AssertionError("Valid package name rejected: " + name); |
|
263 |
printAscii("Invalid package name: \"%s\"", name); |
|
264 |
} |
|
265 |
try { |
|
266 |
validateClassName(name); |
|
267 |
if (!isValidClassName) |
|
268 |
throw new AssertionError("Invalid class name accepted: " + name); |
|
269 |
printAscii("Valid class name: \"%s\"", name); |
|
270 |
} catch (IllegalArgumentException e) { |
|
271 |
if (isValidClassName) |
|
272 |
throw new AssertionError("Valid class name rejected: " + name); |
|
273 |
printAscii("Invalid class name: \"%s\"", name); |
|
274 |
} |
|
275 |
} |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
276 |
|
10 | 277 |
private static void printAscii(String format, Object... args) { |
278 |
String message; |
|
279 |
try { |
|
280 |
final String ascii = "US-ASCII"; |
|
281 |
message = new String(String.format(null, format, args).getBytes(ascii), ascii); |
|
282 |
} catch (java.io.UnsupportedEncodingException ex) { |
|
283 |
throw new AssertionError(ex); |
|
284 |
} |
|
285 |
System.out.println(message); |
|
286 |
} |
|
287 |
||
288 |
/** |
|
289 |
* Insert all files in subdirectory `subdirectory' of `directory' which end |
|
290 |
* in one of the extensions in `extensions' into packageSym. |
|
291 |
*/ |
|
292 |
private void listDirectory(File directory, |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
293 |
RelativeDirectory subdirectory, |
10 | 294 |
Set<JavaFileObject.Kind> fileKinds, |
295 |
boolean recurse, |
|
296 |
ListBuffer<JavaFileObject> l) { |
|
297 |
Archive archive = archives.get(directory); |
|
298 |
||
1208
5072b0dd3d52
6743107: clean up use of static caches in file manager
jjg
parents:
1205
diff
changeset
|
299 |
boolean isFile = fsInfo.isFile(directory); |
10 | 300 |
|
301 |
if (archive != null || isFile) { |
|
302 |
if (archive == null) { |
|
303 |
try { |
|
304 |
archive = openArchive(directory); |
|
305 |
} catch (IOException ex) { |
|
306 |
log.error("error.reading.file", |
|
307 |
directory, ex.getLocalizedMessage()); |
|
308 |
return; |
|
309 |
} |
|
310 |
} |
|
311 |
||
312 |
List<String> files = archive.getFiles(subdirectory); |
|
313 |
if (files != null) { |
|
314 |
for (String file; !files.isEmpty(); files = files.tail) { |
|
315 |
file = files.head; |
|
316 |
if (isValidFile(file, fileKinds)) { |
|
317 |
l.append(archive.getFileObject(subdirectory, file)); |
|
318 |
} |
|
319 |
} |
|
320 |
} |
|
321 |
if (recurse) { |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
322 |
for (RelativeDirectory s: archive.getSubdirectories()) { |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
323 |
if (subdirectory.contains(s)) { |
10 | 324 |
// Because the archive map is a flat list of directories, |
325 |
// the enclosing loop will pick up all child subdirectories. |
|
326 |
// Therefore, there is no need to recurse deeper. |
|
327 |
listDirectory(directory, s, fileKinds, false, l); |
|
328 |
} |
|
329 |
} |
|
330 |
} |
|
331 |
} else { |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
332 |
File d = subdirectory.getFile(directory); |
10 | 333 |
if (!caseMapCheck(d, subdirectory)) |
334 |
return; |
|
335 |
||
336 |
File[] files = d.listFiles(); |
|
337 |
if (files == null) |
|
338 |
return; |
|
339 |
||
340 |
for (File f: files) { |
|
341 |
String fname = f.getName(); |
|
342 |
if (f.isDirectory()) { |
|
343 |
if (recurse && SourceVersion.isIdentifier(fname)) { |
|
344 |
listDirectory(directory, |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
345 |
new RelativeDirectory(subdirectory, fname), |
10 | 346 |
fileKinds, |
347 |
recurse, |
|
348 |
l); |
|
349 |
} |
|
350 |
} else { |
|
351 |
if (isValidFile(fname, fileKinds)) { |
|
352 |
JavaFileObject fe = |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
353 |
new RegularFileObject(this, fname, new File(d, fname)); |
10 | 354 |
l.append(fe); |
355 |
} |
|
356 |
} |
|
357 |
} |
|
358 |
} |
|
359 |
} |
|
360 |
||
361 |
private boolean isValidFile(String s, Set<JavaFileObject.Kind> fileKinds) { |
|
362 |
int lastDot = s.lastIndexOf("."); |
|
363 |
String extn = (lastDot == -1 ? s : s.substring(lastDot)); |
|
364 |
JavaFileObject.Kind kind = getKind(extn); |
|
365 |
return fileKinds.contains(kind); |
|
366 |
} |
|
367 |
||
368 |
private static final boolean fileSystemIsCaseSensitive = |
|
369 |
File.separatorChar == '/'; |
|
370 |
||
371 |
/** Hack to make Windows case sensitive. Test whether given path |
|
372 |
* ends in a string of characters with the same case as given name. |
|
373 |
* Ignore file separators in both path and name. |
|
374 |
*/ |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
375 |
private boolean caseMapCheck(File f, RelativePath name) { |
10 | 376 |
if (fileSystemIsCaseSensitive) return true; |
377 |
// Note that getCanonicalPath() returns the case-sensitive |
|
378 |
// spelled file name. |
|
379 |
String path; |
|
380 |
try { |
|
381 |
path = f.getCanonicalPath(); |
|
382 |
} catch (IOException ex) { |
|
383 |
return false; |
|
384 |
} |
|
385 |
char[] pcs = path.toCharArray(); |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
386 |
char[] ncs = name.path.toCharArray(); |
10 | 387 |
int i = pcs.length - 1; |
388 |
int j = ncs.length - 1; |
|
389 |
while (i >= 0 && j >= 0) { |
|
390 |
while (i >= 0 && pcs[i] == File.separatorChar) i--; |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
391 |
while (j >= 0 && ncs[j] == '/') j--; |
10 | 392 |
if (i >= 0 && j >= 0) { |
393 |
if (pcs[i] != ncs[j]) return false; |
|
394 |
i--; |
|
395 |
j--; |
|
396 |
} |
|
397 |
} |
|
398 |
return j < 0; |
|
399 |
} |
|
400 |
||
401 |
/** |
|
402 |
* An archive provides a flat directory structure of a ZipFile by |
|
403 |
* mapping directory names to lists of files (basenames). |
|
404 |
*/ |
|
405 |
public interface Archive { |
|
406 |
void close() throws IOException; |
|
407 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
408 |
boolean contains(RelativePath name); |
10 | 409 |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
410 |
JavaFileObject getFileObject(RelativeDirectory subdirectory, String file); |
10 | 411 |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
412 |
List<String> getFiles(RelativeDirectory subdirectory); |
10 | 413 |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
414 |
Set<RelativeDirectory> getSubdirectories(); |
10 | 415 |
} |
416 |
||
417 |
public class MissingArchive implements Archive { |
|
418 |
final File zipFileName; |
|
419 |
public MissingArchive(File name) { |
|
420 |
zipFileName = name; |
|
421 |
} |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
422 |
public boolean contains(RelativePath name) { |
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
423 |
return false; |
10 | 424 |
} |
425 |
||
426 |
public void close() { |
|
427 |
} |
|
428 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
429 |
public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) { |
10 | 430 |
return null; |
431 |
} |
|
432 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
433 |
public List<String> getFiles(RelativeDirectory subdirectory) { |
10 | 434 |
return List.nil(); |
435 |
} |
|
436 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
437 |
public Set<RelativeDirectory> getSubdirectories() { |
10 | 438 |
return Collections.emptySet(); |
439 |
} |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
440 |
|
3782 | 441 |
@Override |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
442 |
public String toString() { |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
443 |
return "MissingArchive[" + zipFileName + "]"; |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
444 |
} |
10 | 445 |
} |
446 |
||
447 |
/** A directory of zip files already opened. |
|
448 |
*/ |
|
449 |
Map<File, Archive> archives = new HashMap<File,Archive>(); |
|
450 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
451 |
private static final String[] symbolFileLocation = { "lib", "ct.sym" }; |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
452 |
private static final RelativeDirectory symbolFilePrefix |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
453 |
= new RelativeDirectory("META-INF/sym/rt.jar/"); |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
454 |
|
10 | 455 |
/** Open a new zip file directory. |
456 |
*/ |
|
457 |
protected Archive openArchive(File zipFileName) throws IOException { |
|
458 |
Archive archive = archives.get(zipFileName); |
|
459 |
if (archive == null) { |
|
460 |
File origZipFileName = zipFileName; |
|
461 |
if (!ignoreSymbolFile && paths.isBootClassPathRtJar(zipFileName)) { |
|
462 |
File file = zipFileName.getParentFile().getParentFile(); // ${java.home} |
|
463 |
if (new File(file.getName()).equals(new File("jre"))) |
|
464 |
file = file.getParentFile(); |
|
465 |
// file == ${jdk.home} |
|
466 |
for (String name : symbolFileLocation) |
|
467 |
file = new File(file, name); |
|
468 |
// file == ${jdk.home}/lib/ct.sym |
|
469 |
if (file.exists()) |
|
470 |
zipFileName = file; |
|
471 |
} |
|
472 |
||
473 |
try { |
|
474 |
||
475 |
ZipFile zdir = null; |
|
476 |
||
477 |
boolean usePreindexedCache = false; |
|
478 |
String preindexCacheLocation = null; |
|
479 |
||
480 |
if (!useZipFileIndex) { |
|
481 |
zdir = new ZipFile(zipFileName); |
|
482 |
} |
|
483 |
else { |
|
484 |
usePreindexedCache = options.get("usezipindex") != null; |
|
485 |
preindexCacheLocation = options.get("java.io.tmpdir"); |
|
486 |
String optCacheLoc = options.get("cachezipindexdir"); |
|
487 |
||
488 |
if (optCacheLoc != null && optCacheLoc.length() != 0) { |
|
489 |
if (optCacheLoc.startsWith("\"")) { |
|
490 |
if (optCacheLoc.endsWith("\"")) { |
|
491 |
optCacheLoc = optCacheLoc.substring(1, optCacheLoc.length() - 1); |
|
492 |
} |
|
493 |
else { |
|
494 |
optCacheLoc = optCacheLoc.substring(1); |
|
495 |
} |
|
496 |
} |
|
497 |
||
498 |
File cacheDir = new File(optCacheLoc); |
|
499 |
if (cacheDir.exists() && cacheDir.canWrite()) { |
|
500 |
preindexCacheLocation = optCacheLoc; |
|
501 |
if (!preindexCacheLocation.endsWith("/") && |
|
502 |
!preindexCacheLocation.endsWith(File.separator)) { |
|
503 |
preindexCacheLocation += File.separator; |
|
504 |
} |
|
505 |
} |
|
506 |
} |
|
507 |
} |
|
508 |
||
509 |
if (origZipFileName == zipFileName) { |
|
510 |
if (!useZipFileIndex) { |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
511 |
archive = new ZipArchive(this, zdir); |
10 | 512 |
} else { |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
513 |
archive = new ZipFileIndexArchive(this, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
514 |
ZipFileIndex.getZipFileIndex(zipFileName, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
515 |
null, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
516 |
usePreindexedCache, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
517 |
preindexCacheLocation, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
518 |
options.get("writezipindexfiles") != null)); |
10 | 519 |
} |
520 |
} |
|
521 |
else { |
|
522 |
if (!useZipFileIndex) { |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
523 |
archive = new SymbolArchive(this, origZipFileName, zdir, symbolFilePrefix); |
10 | 524 |
} |
525 |
else { |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
526 |
archive = new ZipFileIndexArchive(this, |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
527 |
ZipFileIndex.getZipFileIndex(zipFileName, |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
528 |
symbolFilePrefix, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
529 |
usePreindexedCache, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
530 |
preindexCacheLocation, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
531 |
options.get("writezipindexfiles") != null)); |
10 | 532 |
} |
533 |
} |
|
534 |
} catch (FileNotFoundException ex) { |
|
535 |
archive = new MissingArchive(zipFileName); |
|
536 |
} catch (IOException ex) { |
|
809
2106a64c0a38
6625520: javac handles missing entries on classpath badly
jjg
parents:
731
diff
changeset
|
537 |
if (zipFileName.exists()) |
2106a64c0a38
6625520: javac handles missing entries on classpath badly
jjg
parents:
731
diff
changeset
|
538 |
log.error("error.reading.file", zipFileName, ex.getLocalizedMessage()); |
10 | 539 |
archive = new MissingArchive(zipFileName); |
540 |
} |
|
541 |
||
542 |
archives.put(origZipFileName, archive); |
|
543 |
} |
|
544 |
return archive; |
|
545 |
} |
|
546 |
||
547 |
/** Flush any output resources. |
|
548 |
*/ |
|
549 |
public void flush() { |
|
550 |
contentCache.clear(); |
|
551 |
} |
|
552 |
||
553 |
/** |
|
554 |
* Close the JavaFileManager, releasing resources. |
|
555 |
*/ |
|
556 |
public void close() { |
|
557 |
for (Iterator<Archive> i = archives.values().iterator(); i.hasNext(); ) { |
|
558 |
Archive a = i.next(); |
|
559 |
i.remove(); |
|
560 |
try { |
|
561 |
a.close(); |
|
562 |
} catch (IOException e) { |
|
563 |
} |
|
564 |
} |
|
565 |
} |
|
566 |
||
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
567 |
CharBuffer getCachedContent(JavaFileObject file) { |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
568 |
SoftReference<CharBuffer> r = contentCache.get(file); |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
569 |
return (r == null ? null : r.get()); |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
570 |
} |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
571 |
|
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
572 |
void cache(JavaFileObject file, CharBuffer cb) { |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
573 |
contentCache.put(file, new SoftReference<CharBuffer>(cb)); |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
574 |
} |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
575 |
|
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
576 |
private final Map<JavaFileObject, SoftReference<CharBuffer>> contentCache |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
577 |
= new HashMap<JavaFileObject, SoftReference<CharBuffer>>(); |
10 | 578 |
|
579 |
private String defaultEncodingName; |
|
580 |
private String getDefaultEncodingName() { |
|
581 |
if (defaultEncodingName == null) { |
|
582 |
defaultEncodingName = |
|
583 |
new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding(); |
|
584 |
} |
|
585 |
return defaultEncodingName; |
|
586 |
} |
|
587 |
||
588 |
protected String getEncodingName() { |
|
589 |
String encName = options.get(OptionName.ENCODING); |
|
590 |
if (encName == null) |
|
591 |
return getDefaultEncodingName(); |
|
592 |
else |
|
593 |
return encName; |
|
594 |
} |
|
595 |
||
596 |
protected Source getSource() { |
|
597 |
String sourceName = options.get(OptionName.SOURCE); |
|
598 |
Source source = null; |
|
599 |
if (sourceName != null) |
|
600 |
source = Source.lookup(sourceName); |
|
601 |
return (source != null ? source : Source.DEFAULT); |
|
602 |
} |
|
603 |
||
604 |
/** |
|
605 |
* Make a byte buffer from an input stream. |
|
606 |
*/ |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
607 |
ByteBuffer makeByteBuffer(InputStream in) |
10 | 608 |
throws IOException { |
609 |
int limit = in.available(); |
|
610 |
if (mmappedIO && in instanceof FileInputStream) { |
|
611 |
// Experimental memory mapped I/O |
|
612 |
FileInputStream fin = (FileInputStream)in; |
|
613 |
return fin.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, limit); |
|
614 |
} |
|
615 |
if (limit < 1024) limit = 1024; |
|
616 |
ByteBuffer result = byteBufferCache.get(limit); |
|
617 |
int position = 0; |
|
618 |
while (in.available() != 0) { |
|
619 |
if (position >= limit) |
|
620 |
// expand buffer |
|
621 |
result = ByteBuffer. |
|
622 |
allocate(limit <<= 1). |
|
623 |
put((ByteBuffer)result.flip()); |
|
624 |
int count = in.read(result.array(), |
|
625 |
position, |
|
626 |
limit - position); |
|
627 |
if (count < 0) break; |
|
628 |
result.position(position += count); |
|
629 |
} |
|
630 |
return (ByteBuffer)result.flip(); |
|
631 |
} |
|
632 |
||
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
633 |
void recycleByteBuffer(ByteBuffer bb) { |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
634 |
byteBufferCache.put(bb); |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
635 |
} |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
636 |
|
10 | 637 |
/** |
638 |
* A single-element cache of direct byte buffers. |
|
639 |
*/ |
|
640 |
private static class ByteBufferCache { |
|
641 |
private ByteBuffer cached; |
|
642 |
ByteBuffer get(int capacity) { |
|
643 |
if (capacity < 20480) capacity = 20480; |
|
644 |
ByteBuffer result = |
|
645 |
(cached != null && cached.capacity() >= capacity) |
|
646 |
? (ByteBuffer)cached.clear() |
|
647 |
: ByteBuffer.allocate(capacity + capacity>>1); |
|
648 |
cached = null; |
|
649 |
return result; |
|
650 |
} |
|
651 |
void put(ByteBuffer x) { |
|
652 |
cached = x; |
|
653 |
} |
|
654 |
} |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
655 |
|
10 | 656 |
private final ByteBufferCache byteBufferCache; |
657 |
||
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
658 |
CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) { |
3782 | 659 |
Charset cs = (this.charset == null) |
10 | 660 |
? Charset.forName(encodingName) |
661 |
: this.charset; |
|
3782 | 662 |
CharsetDecoder decoder = cs.newDecoder(); |
10 | 663 |
|
664 |
CodingErrorAction action; |
|
665 |
if (ignoreEncodingErrors) |
|
666 |
action = CodingErrorAction.REPLACE; |
|
667 |
else |
|
668 |
action = CodingErrorAction.REPORT; |
|
669 |
||
670 |
return decoder |
|
671 |
.onMalformedInput(action) |
|
672 |
.onUnmappableCharacter(action); |
|
673 |
} |
|
674 |
||
675 |
/** |
|
676 |
* Decode a ByteBuffer into a CharBuffer. |
|
677 |
*/ |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
678 |
CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) { |
10 | 679 |
String encodingName = getEncodingName(); |
680 |
CharsetDecoder decoder; |
|
681 |
try { |
|
682 |
decoder = getDecoder(encodingName, ignoreEncodingErrors); |
|
683 |
} catch (IllegalCharsetNameException e) { |
|
684 |
log.error("unsupported.encoding", encodingName); |
|
685 |
return (CharBuffer)CharBuffer.allocate(1).flip(); |
|
686 |
} catch (UnsupportedCharsetException e) { |
|
687 |
log.error("unsupported.encoding", encodingName); |
|
688 |
return (CharBuffer)CharBuffer.allocate(1).flip(); |
|
689 |
} |
|
690 |
||
691 |
// slightly overestimate the buffer size to avoid reallocation. |
|
692 |
float factor = |
|
693 |
decoder.averageCharsPerByte() * 0.8f + |
|
694 |
decoder.maxCharsPerByte() * 0.2f; |
|
695 |
CharBuffer dest = CharBuffer. |
|
696 |
allocate(10 + (int)(inbuf.remaining()*factor)); |
|
697 |
||
698 |
while (true) { |
|
699 |
CoderResult result = decoder.decode(inbuf, dest, true); |
|
700 |
dest.flip(); |
|
701 |
||
702 |
if (result.isUnderflow()) { // done reading |
|
703 |
// make sure there is at least one extra character |
|
704 |
if (dest.limit() == dest.capacity()) { |
|
705 |
dest = CharBuffer.allocate(dest.capacity()+1).put(dest); |
|
706 |
dest.flip(); |
|
707 |
} |
|
708 |
return dest; |
|
709 |
} else if (result.isOverflow()) { // buffer too small; expand |
|
710 |
int newCapacity = |
|
711 |
10 + dest.capacity() + |
|
712 |
(int)(inbuf.remaining()*decoder.maxCharsPerByte()); |
|
713 |
dest = CharBuffer.allocate(newCapacity).put(dest); |
|
714 |
} else if (result.isMalformed() || result.isUnmappable()) { |
|
715 |
// bad character in input |
|
716 |
||
717 |
// report coding error (warn only pre 1.5) |
|
718 |
if (!getSource().allowEncodingErrors()) { |
|
719 |
log.error(new SimpleDiagnosticPosition(dest.limit()), |
|
720 |
"illegal.char.for.encoding", |
|
721 |
charset == null ? encodingName : charset.name()); |
|
722 |
} else { |
|
723 |
log.warning(new SimpleDiagnosticPosition(dest.limit()), |
|
724 |
"illegal.char.for.encoding", |
|
725 |
charset == null ? encodingName : charset.name()); |
|
726 |
} |
|
727 |
||
728 |
// skip past the coding error |
|
729 |
inbuf.position(inbuf.position() + result.length()); |
|
730 |
||
731 |
// undo the flip() to prepare the output buffer |
|
732 |
// for more translation |
|
733 |
dest.position(dest.limit()); |
|
734 |
dest.limit(dest.capacity()); |
|
735 |
dest.put((char)0xfffd); // backward compatible |
|
736 |
} else { |
|
737 |
throw new AssertionError(result); |
|
738 |
} |
|
739 |
} |
|
740 |
// unreached |
|
741 |
} |
|
742 |
||
743 |
public ClassLoader getClassLoader(Location location) { |
|
744 |
nullCheck(location); |
|
745 |
Iterable<? extends File> path = getLocation(location); |
|
746 |
if (path == null) |
|
747 |
return null; |
|
748 |
ListBuffer<URL> lb = new ListBuffer<URL>(); |
|
749 |
for (File f: path) { |
|
750 |
try { |
|
751 |
lb.append(f.toURI().toURL()); |
|
752 |
} catch (MalformedURLException e) { |
|
753 |
throw new AssertionError(e); |
|
754 |
} |
|
755 |
} |
|
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
756 |
|
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
757 |
URL[] urls = lb.toArray(new URL[lb.size()]); |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
758 |
ClassLoader thisClassLoader = getClass().getClassLoader(); |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
759 |
|
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
760 |
// Bug: 6558476 |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
761 |
// Ideally, ClassLoader should be Closeable, but before JDK7 it is not. |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
762 |
// On older versions, try the following, to get a closeable classloader. |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
763 |
|
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
764 |
// 1: Allow client to specify the class to use via hidden option |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
765 |
if (classLoaderClass != null) { |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
766 |
try { |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
767 |
Class<? extends ClassLoader> loader = |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
768 |
Class.forName(classLoaderClass).asSubclass(ClassLoader.class); |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
769 |
Class<?>[] constrArgTypes = { URL[].class, ClassLoader.class }; |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
770 |
Constructor<? extends ClassLoader> constr = loader.getConstructor(constrArgTypes); |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
771 |
return constr.newInstance(new Object[] { urls, thisClassLoader }); |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
772 |
} catch (Throwable t) { |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
773 |
// ignore errors loading user-provided class loader, fall through |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
774 |
} |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
775 |
} |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
776 |
|
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
777 |
// 2: If URLClassLoader implements Closeable, use that. |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
778 |
if (Closeable.class.isAssignableFrom(URLClassLoader.class)) |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
779 |
return new URLClassLoader(urls, thisClassLoader); |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
780 |
|
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
781 |
// 3: Try using private reflection-based CloseableURLClassLoader |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
782 |
try { |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
783 |
return new CloseableURLClassLoader(urls, thisClassLoader); |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
784 |
} catch (Throwable t) { |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
785 |
// ignore errors loading workaround class loader, fall through |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
786 |
} |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
787 |
|
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
788 |
// 4: If all else fails, use plain old standard URLClassLoader |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
789 |
return new URLClassLoader(urls, thisClassLoader); |
10 | 790 |
} |
791 |
||
792 |
public Iterable<JavaFileObject> list(Location location, |
|
793 |
String packageName, |
|
794 |
Set<JavaFileObject.Kind> kinds, |
|
795 |
boolean recurse) |
|
796 |
throws IOException |
|
797 |
{ |
|
798 |
// validatePackageName(packageName); |
|
799 |
nullCheck(packageName); |
|
800 |
nullCheck(kinds); |
|
801 |
||
802 |
Iterable<? extends File> path = getLocation(location); |
|
803 |
if (path == null) |
|
804 |
return List.nil(); |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
805 |
RelativeDirectory subdirectory = RelativeDirectory.forPackage(packageName); |
10 | 806 |
ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>(); |
807 |
||
808 |
for (File directory : path) |
|
809 |
listDirectory(directory, subdirectory, kinds, recurse, results); |
|
810 |
||
811 |
return results.toList(); |
|
812 |
} |
|
813 |
||
814 |
public String inferBinaryName(Location location, JavaFileObject file) { |
|
815 |
file.getClass(); // null check |
|
816 |
location.getClass(); // null check |
|
817 |
// Need to match the path semantics of list(location, ...) |
|
818 |
Iterable<? extends File> path = getLocation(location); |
|
819 |
if (path == null) { |
|
820 |
return null; |
|
821 |
} |
|
822 |
||
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
823 |
if (file instanceof BaseFileObject) { |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
824 |
return ((BaseFileObject) file).inferBinaryName(path); |
10 | 825 |
} else |
826 |
throw new IllegalArgumentException(file.getClass().getName()); |
|
827 |
} |
|
828 |
||
829 |
public boolean isSameFile(FileObject a, FileObject b) { |
|
830 |
nullCheck(a); |
|
831 |
nullCheck(b); |
|
832 |
if (!(a instanceof BaseFileObject)) |
|
833 |
throw new IllegalArgumentException("Not supported: " + a); |
|
834 |
if (!(b instanceof BaseFileObject)) |
|
835 |
throw new IllegalArgumentException("Not supported: " + b); |
|
836 |
return a.equals(b); |
|
837 |
} |
|
838 |
||
839 |
public boolean handleOption(String current, Iterator<String> remaining) { |
|
840 |
for (JavacOption o: javacFileManagerOptions) { |
|
841 |
if (o.matches(current)) { |
|
842 |
if (o.hasArg()) { |
|
843 |
if (remaining.hasNext()) { |
|
844 |
if (!o.process(options, current, remaining.next())) |
|
845 |
return true; |
|
846 |
} |
|
847 |
} else { |
|
848 |
if (!o.process(options, current)) |
|
849 |
return true; |
|
850 |
} |
|
851 |
// operand missing, or process returned false |
|
852 |
throw new IllegalArgumentException(current); |
|
853 |
} |
|
854 |
} |
|
855 |
||
856 |
return false; |
|
857 |
} |
|
858 |
// where |
|
859 |
private static JavacOption[] javacFileManagerOptions = |
|
860 |
RecognizedOptions.getJavacFileManagerOptions( |
|
861 |
new RecognizedOptions.GrumpyHelper()); |
|
862 |
||
863 |
public int isSupportedOption(String option) { |
|
864 |
for (JavacOption o : javacFileManagerOptions) { |
|
865 |
if (o.matches(option)) |
|
866 |
return o.hasArg() ? 1 : 0; |
|
867 |
} |
|
868 |
return -1; |
|
869 |
} |
|
870 |
||
871 |
public boolean hasLocation(Location location) { |
|
872 |
return getLocation(location) != null; |
|
873 |
} |
|
874 |
||
875 |
public JavaFileObject getJavaFileForInput(Location location, |
|
876 |
String className, |
|
877 |
JavaFileObject.Kind kind) |
|
878 |
throws IOException |
|
879 |
{ |
|
880 |
nullCheck(location); |
|
881 |
// validateClassName(className); |
|
882 |
nullCheck(className); |
|
883 |
nullCheck(kind); |
|
884 |
if (!sourceOrClass.contains(kind)) |
|
885 |
throw new IllegalArgumentException("Invalid kind " + kind); |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
886 |
return getFileForInput(location, RelativeFile.forClass(className, kind)); |
10 | 887 |
} |
888 |
||
889 |
public FileObject getFileForInput(Location location, |
|
890 |
String packageName, |
|
891 |
String relativeName) |
|
892 |
throws IOException |
|
893 |
{ |
|
894 |
nullCheck(location); |
|
895 |
// validatePackageName(packageName); |
|
896 |
nullCheck(packageName); |
|
3782 | 897 |
if (!isRelativeUri(relativeName)) |
10 | 898 |
throw new IllegalArgumentException("Invalid relative name: " + relativeName); |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
899 |
RelativeFile name = packageName.length() == 0 |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
900 |
? new RelativeFile(relativeName) |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
901 |
: new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName); |
10 | 902 |
return getFileForInput(location, name); |
903 |
} |
|
904 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
905 |
private JavaFileObject getFileForInput(Location location, RelativeFile name) throws IOException { |
10 | 906 |
Iterable<? extends File> path = getLocation(location); |
907 |
if (path == null) |
|
908 |
return null; |
|
909 |
||
910 |
for (File dir: path) { |
|
911 |
if (dir.isDirectory()) { |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
912 |
File f = name.getFile(dir); |
10 | 913 |
if (f.exists()) |
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
914 |
return new RegularFileObject(this, f); |
10 | 915 |
} else { |
916 |
Archive a = openArchive(dir); |
|
917 |
if (a.contains(name)) { |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
918 |
return a.getFileObject(name.dirname(), name.basename()); |
10 | 919 |
} |
920 |
||
921 |
} |
|
922 |
} |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
923 |
|
10 | 924 |
return null; |
925 |
} |
|
926 |
||
927 |
public JavaFileObject getJavaFileForOutput(Location location, |
|
928 |
String className, |
|
929 |
JavaFileObject.Kind kind, |
|
930 |
FileObject sibling) |
|
931 |
throws IOException |
|
932 |
{ |
|
933 |
nullCheck(location); |
|
934 |
// validateClassName(className); |
|
935 |
nullCheck(className); |
|
936 |
nullCheck(kind); |
|
937 |
if (!sourceOrClass.contains(kind)) |
|
938 |
throw new IllegalArgumentException("Invalid kind " + kind); |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
939 |
return getFileForOutput(location, RelativeFile.forClass(className, kind), sibling); |
10 | 940 |
} |
941 |
||
942 |
public FileObject getFileForOutput(Location location, |
|
943 |
String packageName, |
|
944 |
String relativeName, |
|
945 |
FileObject sibling) |
|
946 |
throws IOException |
|
947 |
{ |
|
948 |
nullCheck(location); |
|
949 |
// validatePackageName(packageName); |
|
950 |
nullCheck(packageName); |
|
3782 | 951 |
if (!isRelativeUri(relativeName)) |
10 | 952 |
throw new IllegalArgumentException("relativeName is invalid"); |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
953 |
RelativeFile name = packageName.length() == 0 |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
954 |
? new RelativeFile(relativeName) |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
955 |
: new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName); |
10 | 956 |
return getFileForOutput(location, name, sibling); |
957 |
} |
|
958 |
||
959 |
private JavaFileObject getFileForOutput(Location location, |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
960 |
RelativeFile fileName, |
10 | 961 |
FileObject sibling) |
962 |
throws IOException |
|
963 |
{ |
|
964 |
File dir; |
|
965 |
if (location == CLASS_OUTPUT) { |
|
966 |
if (getClassOutDir() != null) { |
|
967 |
dir = getClassOutDir(); |
|
968 |
} else { |
|
969 |
File siblingDir = null; |
|
970 |
if (sibling != null && sibling instanceof RegularFileObject) { |
|
971 |
siblingDir = ((RegularFileObject)sibling).f.getParentFile(); |
|
972 |
} |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
973 |
return new RegularFileObject(this, new File(siblingDir, fileName.basename())); |
10 | 974 |
} |
975 |
} else if (location == SOURCE_OUTPUT) { |
|
976 |
dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir()); |
|
977 |
} else { |
|
978 |
Iterable<? extends File> path = paths.getPathForLocation(location); |
|
979 |
dir = null; |
|
980 |
for (File f: path) { |
|
981 |
dir = f; |
|
982 |
break; |
|
983 |
} |
|
984 |
} |
|
985 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
986 |
File file = fileName.getFile(dir); // null-safe |
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
987 |
return new RegularFileObject(this, file); |
10 | 988 |
|
989 |
} |
|
990 |
||
991 |
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles( |
|
992 |
Iterable<? extends File> files) |
|
993 |
{ |
|
994 |
ArrayList<RegularFileObject> result; |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1208
diff
changeset
|
995 |
if (files instanceof Collection<?>) |
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1208
diff
changeset
|
996 |
result = new ArrayList<RegularFileObject>(((Collection<?>)files).size()); |
10 | 997 |
else |
998 |
result = new ArrayList<RegularFileObject>(); |
|
999 |
for (File f: files) |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
1000 |
result.add(new RegularFileObject(this, nullCheck(f))); |
10 | 1001 |
return result; |
1002 |
} |
|
1003 |
||
1004 |
public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) { |
|
1005 |
return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files))); |
|
1006 |
} |
|
1007 |
||
1008 |
public void setLocation(Location location, |
|
1009 |
Iterable<? extends File> path) |
|
1010 |
throws IOException |
|
1011 |
{ |
|
1012 |
nullCheck(location); |
|
1013 |
paths.lazy(); |
|
1014 |
||
1015 |
final File dir = location.isOutputLocation() ? getOutputDirectory(path) : null; |
|
1016 |
||
1017 |
if (location == CLASS_OUTPUT) |
|
1018 |
classOutDir = getOutputLocation(dir, D); |
|
1019 |
else if (location == SOURCE_OUTPUT) |
|
1020 |
sourceOutDir = getOutputLocation(dir, S); |
|
1021 |
else |
|
1022 |
paths.setPathForLocation(location, path); |
|
1023 |
} |
|
1024 |
// where |
|
1025 |
private File getOutputDirectory(Iterable<? extends File> path) throws IOException { |
|
1026 |
if (path == null) |
|
1027 |
return null; |
|
1028 |
Iterator<? extends File> pathIter = path.iterator(); |
|
1029 |
if (!pathIter.hasNext()) |
|
1030 |
throw new IllegalArgumentException("empty path for directory"); |
|
1031 |
File dir = pathIter.next(); |
|
1032 |
if (pathIter.hasNext()) |
|
1033 |
throw new IllegalArgumentException("path too long for directory"); |
|
1034 |
if (!dir.exists()) |
|
1035 |
throw new FileNotFoundException(dir + ": does not exist"); |
|
1036 |
else if (!dir.isDirectory()) |
|
1037 |
throw new IOException(dir + ": not a directory"); |
|
1038 |
return dir; |
|
1039 |
} |
|
1040 |
||
1041 |
private File getOutputLocation(File dir, OptionName defaultOptionName) { |
|
1042 |
if (dir != null) |
|
1043 |
return dir; |
|
1044 |
String arg = options.get(defaultOptionName); |
|
1045 |
if (arg == null) |
|
1046 |
return null; |
|
1047 |
return new File(arg); |
|
1048 |
} |
|
1049 |
||
1050 |
public Iterable<? extends File> getLocation(Location location) { |
|
1051 |
nullCheck(location); |
|
1052 |
paths.lazy(); |
|
1053 |
if (location == CLASS_OUTPUT) { |
|
1054 |
return (getClassOutDir() == null ? null : List.of(getClassOutDir())); |
|
1055 |
} else if (location == SOURCE_OUTPUT) { |
|
1056 |
return (getSourceOutDir() == null ? null : List.of(getSourceOutDir())); |
|
1057 |
} else |
|
1058 |
return paths.getPathForLocation(location); |
|
1059 |
} |
|
1060 |
||
1061 |
private File getClassOutDir() { |
|
1062 |
if (classOutDir == uninited) |
|
1063 |
classOutDir = getOutputLocation(null, D); |
|
1064 |
return classOutDir; |
|
1065 |
} |
|
1066 |
||
1067 |
private File getSourceOutDir() { |
|
1068 |
if (sourceOutDir == uninited) |
|
1069 |
sourceOutDir = getOutputLocation(null, S); |
|
1070 |
return sourceOutDir; |
|
1071 |
} |
|
1072 |
||
1073 |
/** |
|
1074 |
* Enforces the specification of a "relative" URI as used in |
|
1075 |
* {@linkplain #getFileForInput(Location,String,URI) |
|
1076 |
* getFileForInput}. This method must follow the rules defined in |
|
1077 |
* that method, do not make any changes without consulting the |
|
1078 |
* specification. |
|
1079 |
*/ |
|
1080 |
protected static boolean isRelativeUri(URI uri) { |
|
1081 |
if (uri.isAbsolute()) |
|
1082 |
return false; |
|
1083 |
String path = uri.normalize().getPath(); |
|
1084 |
if (path.length() == 0 /* isEmpty() is mustang API */) |
|
1085 |
return false; |
|
1086 |
char first = path.charAt(0); |
|
1087 |
return first != '.' && first != '/'; |
|
1088 |
} |
|
1089 |
||
3782 | 1090 |
// Convenience method |
1091 |
protected static boolean isRelativeUri(String u) { |
|
1092 |
try { |
|
1093 |
return isRelativeUri(new URI(u)); |
|
1094 |
} catch (URISyntaxException e) { |
|
1095 |
return false; |
|
1096 |
} |
|
1097 |
} |
|
1098 |
||
10 | 1099 |
/** |
1100 |
* Converts a relative file name to a relative URI. This is |
|
1101 |
* different from File.toURI as this method does not canonicalize |
|
1102 |
* the file before creating the URI. Furthermore, no schema is |
|
1103 |
* used. |
|
1104 |
* @param file a relative file name |
|
1105 |
* @return a relative URI |
|
1106 |
* @throws IllegalArgumentException if the file name is not |
|
1107 |
* relative according to the definition given in {@link |
|
1108 |
* javax.tools.JavaFileManager#getFileForInput} |
|
1109 |
*/ |
|
1110 |
public static String getRelativeName(File file) { |
|
1111 |
if (!file.isAbsolute()) { |
|
1112 |
String result = file.getPath().replace(File.separatorChar, '/'); |
|
3782 | 1113 |
if (isRelativeUri(result)) |
10 | 1114 |
return result; |
1115 |
} |
|
1116 |
throw new IllegalArgumentException("Invalid relative path: " + file); |
|
1117 |
} |
|
1118 |
||
1119 |
private static <T> T nullCheck(T o) { |
|
1120 |
o.getClass(); // null check |
|
1121 |
return o; |
|
1122 |
} |
|
1123 |
||
1124 |
private static <T> Iterable<T> nullCheck(Iterable<T> it) { |
|
1125 |
for (T t : it) |
|
1126 |
t.getClass(); // null check |
|
1127 |
return it; |
|
1128 |
} |
|
1129 |
} |