author | jjg |
Thu, 10 Jun 2010 16:08:01 -0700 | |
changeset 5847 | 1908176fd6e3 |
parent 5520 | 86e4b9a9da40 |
child 6719 | 1ce993f87850 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 2005, 2009, Oracle and/or its affiliates. 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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. |
|
10 | 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; |
|
29 |
import java.io.File; |
|
30 |
import java.io.FileNotFoundException; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.OutputStreamWriter; |
|
33 |
import java.net.MalformedURLException; |
|
34 |
import java.net.URI; |
|
3782 | 35 |
import java.net.URISyntaxException; |
10 | 36 |
import java.net.URL; |
37 |
import java.nio.CharBuffer; |
|
38 |
import java.nio.charset.Charset; |
|
39 |
import java.util.ArrayList; |
|
40 |
import java.util.Arrays; |
|
41 |
import java.util.Collection; |
|
42 |
import java.util.Collections; |
|
43 |
import java.util.EnumSet; |
|
44 |
import java.util.HashMap; |
|
45 |
import java.util.Iterator; |
|
46 |
import java.util.Map; |
|
47 |
import java.util.Set; |
|
48 |
import java.util.zip.ZipFile; |
|
49 |
||
50 |
import javax.lang.model.SourceVersion; |
|
51 |
import javax.tools.FileObject; |
|
52 |
import javax.tools.JavaFileManager; |
|
53 |
import javax.tools.JavaFileObject; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
54 |
import javax.tools.StandardJavaFileManager; |
10 | 55 |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
56 |
import com.sun.tools.javac.file.RelativePath.RelativeFile; |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
57 |
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
|
58 |
import com.sun.tools.javac.main.OptionName; |
4548 | 59 |
import com.sun.tools.javac.util.BaseFileManager; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
60 |
import com.sun.tools.javac.util.Context; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
61 |
import com.sun.tools.javac.util.List; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
62 |
import com.sun.tools.javac.util.ListBuffer; |
10 | 63 |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
64 |
import static javax.tools.StandardLocation.*; |
10 | 65 |
import static com.sun.tools.javac.main.OptionName.*; |
66 |
||
67 |
/** |
|
68 |
* This class provides access to the source, class and other files |
|
69 |
* used by the compiler and related tools. |
|
3380
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1789
diff
changeset
|
70 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
71 |
* <p><b>This is NOT part of any supported API. |
3380
a6c2bcab0fec
6865399: some javac files are missing Sun internal API comment
jjg
parents:
1789
diff
changeset
|
72 |
* 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
|
73 |
* 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
|
74 |
* deletion without notice.</b> |
10 | 75 |
*/ |
4548 | 76 |
public class JavacFileManager extends BaseFileManager implements StandardJavaFileManager { |
10 | 77 |
|
78 |
boolean useZipFileIndex; |
|
79 |
||
80 |
public static char[] toArray(CharBuffer buffer) { |
|
81 |
if (buffer.hasArray()) |
|
82 |
return ((CharBuffer)buffer.compact().flip()).array(); |
|
83 |
else |
|
84 |
return buffer.toString().toCharArray(); |
|
85 |
} |
|
86 |
||
87 |
/** Encapsulates knowledge of paths |
|
88 |
*/ |
|
89 |
private Paths paths; |
|
90 |
||
1208
5072b0dd3d52
6743107: clean up use of static caches in file manager
jjg
parents:
1205
diff
changeset
|
91 |
private FSInfo fsInfo; |
5072b0dd3d52
6743107: clean up use of static caches in file manager
jjg
parents:
1205
diff
changeset
|
92 |
|
10 | 93 |
private final File uninited = new File("U N I N I T E D"); |
94 |
||
95 |
private final Set<JavaFileObject.Kind> sourceOrClass = |
|
96 |
EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS); |
|
97 |
||
98 |
/** The standard output directory, primarily used for classes. |
|
99 |
* Initialized by the "-d" option. |
|
100 |
* If classOutDir = null, files are written into same directory as the sources |
|
101 |
* they were generated from. |
|
102 |
*/ |
|
103 |
private File classOutDir = uninited; |
|
104 |
||
105 |
/** The output directory, used when generating sources while processing annotations. |
|
106 |
* Initialized by the "-s" option. |
|
107 |
*/ |
|
108 |
private File sourceOutDir = uninited; |
|
109 |
||
110 |
protected boolean mmappedIO; |
|
111 |
protected boolean ignoreSymbolFile; |
|
112 |
||
113 |
/** |
|
114 |
* Register a Context.Factory to create a JavacFileManager. |
|
115 |
*/ |
|
116 |
public static void preRegister(final Context context) { |
|
117 |
context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() { |
|
118 |
public JavaFileManager make() { |
|
119 |
return new JavacFileManager(context, true, null); |
|
120 |
} |
|
121 |
}); |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Create a JavacFileManager using a given context, optionally registering |
|
126 |
* it as the JavaFileManager for that context. |
|
127 |
*/ |
|
128 |
public JavacFileManager(Context context, boolean register, Charset charset) { |
|
4548 | 129 |
super(charset); |
10 | 130 |
if (register) |
131 |
context.put(JavaFileManager.class, this); |
|
132 |
setContext(context); |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Set the context for JavacFileManager. |
|
137 |
*/ |
|
4548 | 138 |
@Override |
10 | 139 |
public void setContext(Context context) { |
4548 | 140 |
super.setContext(context); |
10 | 141 |
if (paths == null) { |
142 |
paths = Paths.instance(context); |
|
143 |
} else { |
|
144 |
// Reuse the Paths object as it stores the locations that |
|
145 |
// have been set with setLocation, etc. |
|
146 |
paths.setContext(context); |
|
147 |
} |
|
148 |
||
1208
5072b0dd3d52
6743107: clean up use of static caches in file manager
jjg
parents:
1205
diff
changeset
|
149 |
fsInfo = FSInfo.instance(context); |
10 | 150 |
|
151 |
useZipFileIndex = System.getProperty("useJavaUtilZip") == null;// TODO: options.get("useJavaUtilZip") == null; |
|
152 |
||
153 |
mmappedIO = options.get("mmappedIO") != null; |
|
154 |
ignoreSymbolFile = options.get("ignore.symbol.file") != null; |
|
155 |
} |
|
156 |
||
157 |
public JavaFileObject getFileForInput(String name) { |
|
158 |
return getRegularFile(new File(name)); |
|
159 |
} |
|
160 |
||
161 |
public JavaFileObject getRegularFile(File file) { |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
162 |
return new RegularFileObject(this, file); |
10 | 163 |
} |
164 |
||
165 |
public JavaFileObject getFileForOutput(String classname, |
|
166 |
JavaFileObject.Kind kind, |
|
167 |
JavaFileObject sibling) |
|
168 |
throws IOException |
|
169 |
{ |
|
170 |
return getJavaFileForOutput(CLASS_OUTPUT, classname, kind, sibling); |
|
171 |
} |
|
172 |
||
173 |
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) { |
|
174 |
ListBuffer<File> files = new ListBuffer<File>(); |
|
175 |
for (String name : names) |
|
176 |
files.append(new File(nullCheck(name))); |
|
177 |
return getJavaFileObjectsFromFiles(files.toList()); |
|
178 |
} |
|
179 |
||
180 |
public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) { |
|
181 |
return getJavaFileObjectsFromStrings(Arrays.asList(nullCheck(names))); |
|
182 |
} |
|
183 |
||
184 |
private static boolean isValidName(String name) { |
|
185 |
// Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ), |
|
186 |
// but the set of keywords depends on the source level, and we don't want |
|
187 |
// impls of JavaFileManager to have to be dependent on the source level. |
|
188 |
// Therefore we simply check that the argument is a sequence of identifiers |
|
189 |
// separated by ".". |
|
190 |
for (String s : name.split("\\.", -1)) { |
|
191 |
if (!SourceVersion.isIdentifier(s)) |
|
192 |
return false; |
|
193 |
} |
|
194 |
return true; |
|
195 |
} |
|
196 |
||
197 |
private static void validateClassName(String className) { |
|
198 |
if (!isValidName(className)) |
|
199 |
throw new IllegalArgumentException("Invalid class name: " + className); |
|
200 |
} |
|
201 |
||
202 |
private static void validatePackageName(String packageName) { |
|
203 |
if (packageName.length() > 0 && !isValidName(packageName)) |
|
204 |
throw new IllegalArgumentException("Invalid packageName name: " + packageName); |
|
205 |
} |
|
206 |
||
207 |
public static void testName(String name, |
|
208 |
boolean isValidPackageName, |
|
209 |
boolean isValidClassName) |
|
210 |
{ |
|
211 |
try { |
|
212 |
validatePackageName(name); |
|
213 |
if (!isValidPackageName) |
|
214 |
throw new AssertionError("Invalid package name accepted: " + name); |
|
215 |
printAscii("Valid package name: \"%s\"", name); |
|
216 |
} catch (IllegalArgumentException e) { |
|
217 |
if (isValidPackageName) |
|
218 |
throw new AssertionError("Valid package name rejected: " + name); |
|
219 |
printAscii("Invalid package name: \"%s\"", name); |
|
220 |
} |
|
221 |
try { |
|
222 |
validateClassName(name); |
|
223 |
if (!isValidClassName) |
|
224 |
throw new AssertionError("Invalid class name accepted: " + name); |
|
225 |
printAscii("Valid class name: \"%s\"", name); |
|
226 |
} catch (IllegalArgumentException e) { |
|
227 |
if (isValidClassName) |
|
228 |
throw new AssertionError("Valid class name rejected: " + name); |
|
229 |
printAscii("Invalid class name: \"%s\"", name); |
|
230 |
} |
|
231 |
} |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
232 |
|
10 | 233 |
private static void printAscii(String format, Object... args) { |
234 |
String message; |
|
235 |
try { |
|
236 |
final String ascii = "US-ASCII"; |
|
237 |
message = new String(String.format(null, format, args).getBytes(ascii), ascii); |
|
238 |
} catch (java.io.UnsupportedEncodingException ex) { |
|
239 |
throw new AssertionError(ex); |
|
240 |
} |
|
241 |
System.out.println(message); |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* Insert all files in subdirectory `subdirectory' of `directory' which end |
|
246 |
* in one of the extensions in `extensions' into packageSym. |
|
247 |
*/ |
|
248 |
private void listDirectory(File directory, |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
249 |
RelativeDirectory subdirectory, |
10 | 250 |
Set<JavaFileObject.Kind> fileKinds, |
251 |
boolean recurse, |
|
252 |
ListBuffer<JavaFileObject> l) { |
|
253 |
Archive archive = archives.get(directory); |
|
254 |
||
1208
5072b0dd3d52
6743107: clean up use of static caches in file manager
jjg
parents:
1205
diff
changeset
|
255 |
boolean isFile = fsInfo.isFile(directory); |
10 | 256 |
|
257 |
if (archive != null || isFile) { |
|
258 |
if (archive == null) { |
|
259 |
try { |
|
260 |
archive = openArchive(directory); |
|
261 |
} catch (IOException ex) { |
|
262 |
log.error("error.reading.file", |
|
5007
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
263 |
directory, getMessage(ex)); |
10 | 264 |
return; |
265 |
} |
|
266 |
} |
|
267 |
||
268 |
List<String> files = archive.getFiles(subdirectory); |
|
269 |
if (files != null) { |
|
270 |
for (String file; !files.isEmpty(); files = files.tail) { |
|
271 |
file = files.head; |
|
272 |
if (isValidFile(file, fileKinds)) { |
|
273 |
l.append(archive.getFileObject(subdirectory, file)); |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
if (recurse) { |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
278 |
for (RelativeDirectory s: archive.getSubdirectories()) { |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
279 |
if (subdirectory.contains(s)) { |
10 | 280 |
// Because the archive map is a flat list of directories, |
281 |
// the enclosing loop will pick up all child subdirectories. |
|
282 |
// Therefore, there is no need to recurse deeper. |
|
283 |
listDirectory(directory, s, fileKinds, false, l); |
|
284 |
} |
|
285 |
} |
|
286 |
} |
|
287 |
} else { |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
288 |
File d = subdirectory.getFile(directory); |
10 | 289 |
if (!caseMapCheck(d, subdirectory)) |
290 |
return; |
|
291 |
||
292 |
File[] files = d.listFiles(); |
|
293 |
if (files == null) |
|
294 |
return; |
|
295 |
||
296 |
for (File f: files) { |
|
297 |
String fname = f.getName(); |
|
298 |
if (f.isDirectory()) { |
|
299 |
if (recurse && SourceVersion.isIdentifier(fname)) { |
|
300 |
listDirectory(directory, |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
301 |
new RelativeDirectory(subdirectory, fname), |
10 | 302 |
fileKinds, |
303 |
recurse, |
|
304 |
l); |
|
305 |
} |
|
306 |
} else { |
|
307 |
if (isValidFile(fname, fileKinds)) { |
|
308 |
JavaFileObject fe = |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
309 |
new RegularFileObject(this, fname, new File(d, fname)); |
10 | 310 |
l.append(fe); |
311 |
} |
|
312 |
} |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
private boolean isValidFile(String s, Set<JavaFileObject.Kind> fileKinds) { |
|
4548 | 318 |
JavaFileObject.Kind kind = getKind(s); |
10 | 319 |
return fileKinds.contains(kind); |
320 |
} |
|
321 |
||
322 |
private static final boolean fileSystemIsCaseSensitive = |
|
323 |
File.separatorChar == '/'; |
|
324 |
||
325 |
/** Hack to make Windows case sensitive. Test whether given path |
|
326 |
* ends in a string of characters with the same case as given name. |
|
327 |
* Ignore file separators in both path and name. |
|
328 |
*/ |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
329 |
private boolean caseMapCheck(File f, RelativePath name) { |
10 | 330 |
if (fileSystemIsCaseSensitive) return true; |
331 |
// Note that getCanonicalPath() returns the case-sensitive |
|
332 |
// spelled file name. |
|
333 |
String path; |
|
334 |
try { |
|
335 |
path = f.getCanonicalPath(); |
|
336 |
} catch (IOException ex) { |
|
337 |
return false; |
|
338 |
} |
|
339 |
char[] pcs = path.toCharArray(); |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
340 |
char[] ncs = name.path.toCharArray(); |
10 | 341 |
int i = pcs.length - 1; |
342 |
int j = ncs.length - 1; |
|
343 |
while (i >= 0 && j >= 0) { |
|
344 |
while (i >= 0 && pcs[i] == File.separatorChar) i--; |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
345 |
while (j >= 0 && ncs[j] == '/') j--; |
10 | 346 |
if (i >= 0 && j >= 0) { |
347 |
if (pcs[i] != ncs[j]) return false; |
|
348 |
i--; |
|
349 |
j--; |
|
350 |
} |
|
351 |
} |
|
352 |
return j < 0; |
|
353 |
} |
|
354 |
||
355 |
/** |
|
356 |
* An archive provides a flat directory structure of a ZipFile by |
|
357 |
* mapping directory names to lists of files (basenames). |
|
358 |
*/ |
|
359 |
public interface Archive { |
|
360 |
void close() throws IOException; |
|
361 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
362 |
boolean contains(RelativePath name); |
10 | 363 |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
364 |
JavaFileObject getFileObject(RelativeDirectory subdirectory, String file); |
10 | 365 |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
366 |
List<String> getFiles(RelativeDirectory subdirectory); |
10 | 367 |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
368 |
Set<RelativeDirectory> getSubdirectories(); |
10 | 369 |
} |
370 |
||
371 |
public class MissingArchive implements Archive { |
|
372 |
final File zipFileName; |
|
373 |
public MissingArchive(File name) { |
|
374 |
zipFileName = name; |
|
375 |
} |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
376 |
public boolean contains(RelativePath name) { |
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
377 |
return false; |
10 | 378 |
} |
379 |
||
380 |
public void close() { |
|
381 |
} |
|
382 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
383 |
public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) { |
10 | 384 |
return null; |
385 |
} |
|
386 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
387 |
public List<String> getFiles(RelativeDirectory subdirectory) { |
10 | 388 |
return List.nil(); |
389 |
} |
|
390 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
391 |
public Set<RelativeDirectory> getSubdirectories() { |
10 | 392 |
return Collections.emptySet(); |
393 |
} |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
394 |
|
3782 | 395 |
@Override |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
396 |
public String toString() { |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
397 |
return "MissingArchive[" + zipFileName + "]"; |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
398 |
} |
10 | 399 |
} |
400 |
||
401 |
/** A directory of zip files already opened. |
|
402 |
*/ |
|
403 |
Map<File, Archive> archives = new HashMap<File,Archive>(); |
|
404 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
405 |
private static final String[] symbolFileLocation = { "lib", "ct.sym" }; |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
406 |
private static final RelativeDirectory symbolFilePrefix |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
407 |
= new RelativeDirectory("META-INF/sym/rt.jar/"); |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
408 |
|
10 | 409 |
/** Open a new zip file directory. |
410 |
*/ |
|
411 |
protected Archive openArchive(File zipFileName) throws IOException { |
|
412 |
Archive archive = archives.get(zipFileName); |
|
413 |
if (archive == null) { |
|
414 |
File origZipFileName = zipFileName; |
|
415 |
if (!ignoreSymbolFile && paths.isBootClassPathRtJar(zipFileName)) { |
|
416 |
File file = zipFileName.getParentFile().getParentFile(); // ${java.home} |
|
417 |
if (new File(file.getName()).equals(new File("jre"))) |
|
418 |
file = file.getParentFile(); |
|
419 |
// file == ${jdk.home} |
|
420 |
for (String name : symbolFileLocation) |
|
421 |
file = new File(file, name); |
|
422 |
// file == ${jdk.home}/lib/ct.sym |
|
423 |
if (file.exists()) |
|
424 |
zipFileName = file; |
|
425 |
} |
|
426 |
||
427 |
try { |
|
428 |
||
429 |
ZipFile zdir = null; |
|
430 |
||
431 |
boolean usePreindexedCache = false; |
|
432 |
String preindexCacheLocation = null; |
|
433 |
||
434 |
if (!useZipFileIndex) { |
|
435 |
zdir = new ZipFile(zipFileName); |
|
436 |
} |
|
437 |
else { |
|
438 |
usePreindexedCache = options.get("usezipindex") != null; |
|
439 |
preindexCacheLocation = options.get("java.io.tmpdir"); |
|
440 |
String optCacheLoc = options.get("cachezipindexdir"); |
|
441 |
||
442 |
if (optCacheLoc != null && optCacheLoc.length() != 0) { |
|
443 |
if (optCacheLoc.startsWith("\"")) { |
|
444 |
if (optCacheLoc.endsWith("\"")) { |
|
445 |
optCacheLoc = optCacheLoc.substring(1, optCacheLoc.length() - 1); |
|
446 |
} |
|
447 |
else { |
|
448 |
optCacheLoc = optCacheLoc.substring(1); |
|
449 |
} |
|
450 |
} |
|
451 |
||
452 |
File cacheDir = new File(optCacheLoc); |
|
453 |
if (cacheDir.exists() && cacheDir.canWrite()) { |
|
454 |
preindexCacheLocation = optCacheLoc; |
|
455 |
if (!preindexCacheLocation.endsWith("/") && |
|
456 |
!preindexCacheLocation.endsWith(File.separator)) { |
|
457 |
preindexCacheLocation += File.separator; |
|
458 |
} |
|
459 |
} |
|
460 |
} |
|
461 |
} |
|
462 |
||
463 |
if (origZipFileName == zipFileName) { |
|
464 |
if (!useZipFileIndex) { |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
465 |
archive = new ZipArchive(this, zdir); |
10 | 466 |
} else { |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
467 |
archive = new ZipFileIndexArchive(this, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
468 |
ZipFileIndex.getZipFileIndex(zipFileName, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
469 |
null, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
470 |
usePreindexedCache, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
471 |
preindexCacheLocation, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
472 |
options.get("writezipindexfiles") != null)); |
10 | 473 |
} |
474 |
} |
|
475 |
else { |
|
476 |
if (!useZipFileIndex) { |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
477 |
archive = new SymbolArchive(this, origZipFileName, zdir, symbolFilePrefix); |
10 | 478 |
} |
479 |
else { |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
480 |
archive = new ZipFileIndexArchive(this, |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
481 |
ZipFileIndex.getZipFileIndex(zipFileName, |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
482 |
symbolFilePrefix, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
483 |
usePreindexedCache, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
484 |
preindexCacheLocation, |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
485 |
options.get("writezipindexfiles") != null)); |
10 | 486 |
} |
487 |
} |
|
488 |
} catch (FileNotFoundException ex) { |
|
489 |
archive = new MissingArchive(zipFileName); |
|
490 |
} catch (IOException ex) { |
|
809
2106a64c0a38
6625520: javac handles missing entries on classpath badly
jjg
parents:
731
diff
changeset
|
491 |
if (zipFileName.exists()) |
5007
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
492 |
log.error("error.reading.file", zipFileName, getMessage(ex)); |
10 | 493 |
archive = new MissingArchive(zipFileName); |
494 |
} |
|
495 |
||
496 |
archives.put(origZipFileName, archive); |
|
497 |
} |
|
498 |
return archive; |
|
499 |
} |
|
500 |
||
501 |
/** Flush any output resources. |
|
502 |
*/ |
|
503 |
public void flush() { |
|
504 |
contentCache.clear(); |
|
505 |
} |
|
506 |
||
507 |
/** |
|
508 |
* Close the JavaFileManager, releasing resources. |
|
509 |
*/ |
|
510 |
public void close() { |
|
511 |
for (Iterator<Archive> i = archives.values().iterator(); i.hasNext(); ) { |
|
512 |
Archive a = i.next(); |
|
513 |
i.remove(); |
|
514 |
try { |
|
515 |
a.close(); |
|
516 |
} catch (IOException e) { |
|
517 |
} |
|
518 |
} |
|
519 |
} |
|
520 |
||
521 |
private String defaultEncodingName; |
|
522 |
private String getDefaultEncodingName() { |
|
523 |
if (defaultEncodingName == null) { |
|
524 |
defaultEncodingName = |
|
525 |
new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding(); |
|
526 |
} |
|
527 |
return defaultEncodingName; |
|
528 |
} |
|
529 |
||
530 |
public ClassLoader getClassLoader(Location location) { |
|
531 |
nullCheck(location); |
|
532 |
Iterable<? extends File> path = getLocation(location); |
|
533 |
if (path == null) |
|
534 |
return null; |
|
535 |
ListBuffer<URL> lb = new ListBuffer<URL>(); |
|
536 |
for (File f: path) { |
|
537 |
try { |
|
538 |
lb.append(f.toURI().toURL()); |
|
539 |
} catch (MalformedURLException e) { |
|
540 |
throw new AssertionError(e); |
|
541 |
} |
|
542 |
} |
|
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3380
diff
changeset
|
543 |
|
4548 | 544 |
return getClassLoader(lb.toArray(new URL[lb.size()])); |
10 | 545 |
} |
546 |
||
547 |
public Iterable<JavaFileObject> list(Location location, |
|
548 |
String packageName, |
|
549 |
Set<JavaFileObject.Kind> kinds, |
|
550 |
boolean recurse) |
|
551 |
throws IOException |
|
552 |
{ |
|
553 |
// validatePackageName(packageName); |
|
554 |
nullCheck(packageName); |
|
555 |
nullCheck(kinds); |
|
556 |
||
557 |
Iterable<? extends File> path = getLocation(location); |
|
558 |
if (path == null) |
|
559 |
return List.nil(); |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
560 |
RelativeDirectory subdirectory = RelativeDirectory.forPackage(packageName); |
10 | 561 |
ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>(); |
562 |
||
563 |
for (File directory : path) |
|
564 |
listDirectory(directory, subdirectory, kinds, recurse, results); |
|
565 |
||
566 |
return results.toList(); |
|
567 |
} |
|
568 |
||
569 |
public String inferBinaryName(Location location, JavaFileObject file) { |
|
570 |
file.getClass(); // null check |
|
571 |
location.getClass(); // null check |
|
572 |
// Need to match the path semantics of list(location, ...) |
|
573 |
Iterable<? extends File> path = getLocation(location); |
|
574 |
if (path == null) { |
|
575 |
return null; |
|
576 |
} |
|
577 |
||
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
578 |
if (file instanceof BaseFileObject) { |
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
579 |
return ((BaseFileObject) file).inferBinaryName(path); |
10 | 580 |
} else |
581 |
throw new IllegalArgumentException(file.getClass().getName()); |
|
582 |
} |
|
583 |
||
584 |
public boolean isSameFile(FileObject a, FileObject b) { |
|
585 |
nullCheck(a); |
|
586 |
nullCheck(b); |
|
587 |
if (!(a instanceof BaseFileObject)) |
|
588 |
throw new IllegalArgumentException("Not supported: " + a); |
|
589 |
if (!(b instanceof BaseFileObject)) |
|
590 |
throw new IllegalArgumentException("Not supported: " + b); |
|
591 |
return a.equals(b); |
|
592 |
} |
|
593 |
||
594 |
public boolean hasLocation(Location location) { |
|
595 |
return getLocation(location) != null; |
|
596 |
} |
|
597 |
||
598 |
public JavaFileObject getJavaFileForInput(Location location, |
|
599 |
String className, |
|
600 |
JavaFileObject.Kind kind) |
|
601 |
throws IOException |
|
602 |
{ |
|
603 |
nullCheck(location); |
|
604 |
// validateClassName(className); |
|
605 |
nullCheck(className); |
|
606 |
nullCheck(kind); |
|
607 |
if (!sourceOrClass.contains(kind)) |
|
608 |
throw new IllegalArgumentException("Invalid kind " + kind); |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
609 |
return getFileForInput(location, RelativeFile.forClass(className, kind)); |
10 | 610 |
} |
611 |
||
612 |
public FileObject getFileForInput(Location location, |
|
613 |
String packageName, |
|
614 |
String relativeName) |
|
615 |
throws IOException |
|
616 |
{ |
|
617 |
nullCheck(location); |
|
618 |
// validatePackageName(packageName); |
|
619 |
nullCheck(packageName); |
|
3782 | 620 |
if (!isRelativeUri(relativeName)) |
10 | 621 |
throw new IllegalArgumentException("Invalid relative name: " + relativeName); |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
622 |
RelativeFile name = packageName.length() == 0 |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
623 |
? new RelativeFile(relativeName) |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
624 |
: new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName); |
10 | 625 |
return getFileForInput(location, name); |
626 |
} |
|
627 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
628 |
private JavaFileObject getFileForInput(Location location, RelativeFile name) throws IOException { |
10 | 629 |
Iterable<? extends File> path = getLocation(location); |
630 |
if (path == null) |
|
631 |
return null; |
|
632 |
||
633 |
for (File dir: path) { |
|
634 |
if (dir.isDirectory()) { |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
635 |
File f = name.getFile(dir); |
10 | 636 |
if (f.exists()) |
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
637 |
return new RegularFileObject(this, f); |
10 | 638 |
} else { |
639 |
Archive a = openArchive(dir); |
|
640 |
if (a.contains(name)) { |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
641 |
return a.getFileObject(name.dirname(), name.basename()); |
10 | 642 |
} |
643 |
||
644 |
} |
|
645 |
} |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
646 |
|
10 | 647 |
return null; |
648 |
} |
|
649 |
||
650 |
public JavaFileObject getJavaFileForOutput(Location location, |
|
651 |
String className, |
|
652 |
JavaFileObject.Kind kind, |
|
653 |
FileObject sibling) |
|
654 |
throws IOException |
|
655 |
{ |
|
656 |
nullCheck(location); |
|
657 |
// validateClassName(className); |
|
658 |
nullCheck(className); |
|
659 |
nullCheck(kind); |
|
660 |
if (!sourceOrClass.contains(kind)) |
|
661 |
throw new IllegalArgumentException("Invalid kind " + kind); |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
662 |
return getFileForOutput(location, RelativeFile.forClass(className, kind), sibling); |
10 | 663 |
} |
664 |
||
665 |
public FileObject getFileForOutput(Location location, |
|
666 |
String packageName, |
|
667 |
String relativeName, |
|
668 |
FileObject sibling) |
|
669 |
throws IOException |
|
670 |
{ |
|
671 |
nullCheck(location); |
|
672 |
// validatePackageName(packageName); |
|
673 |
nullCheck(packageName); |
|
3782 | 674 |
if (!isRelativeUri(relativeName)) |
10 | 675 |
throw new IllegalArgumentException("relativeName is invalid"); |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
676 |
RelativeFile name = packageName.length() == 0 |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
677 |
? new RelativeFile(relativeName) |
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
678 |
: new RelativeFile(RelativeDirectory.forPackage(packageName), relativeName); |
10 | 679 |
return getFileForOutput(location, name, sibling); |
680 |
} |
|
681 |
||
682 |
private JavaFileObject getFileForOutput(Location location, |
|
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
683 |
RelativeFile fileName, |
10 | 684 |
FileObject sibling) |
685 |
throws IOException |
|
686 |
{ |
|
687 |
File dir; |
|
688 |
if (location == CLASS_OUTPUT) { |
|
689 |
if (getClassOutDir() != null) { |
|
690 |
dir = getClassOutDir(); |
|
691 |
} else { |
|
692 |
File siblingDir = null; |
|
693 |
if (sibling != null && sibling instanceof RegularFileObject) { |
|
4073
9788f4549740
6838467: JSR199 FileObjects don't obey general contract of equals.
jjg
parents:
3998
diff
changeset
|
694 |
siblingDir = ((RegularFileObject)sibling).file.getParentFile(); |
10 | 695 |
} |
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
696 |
return new RegularFileObject(this, new File(siblingDir, fileName.basename())); |
10 | 697 |
} |
698 |
} else if (location == SOURCE_OUTPUT) { |
|
699 |
dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir()); |
|
700 |
} else { |
|
701 |
Iterable<? extends File> path = paths.getPathForLocation(location); |
|
702 |
dir = null; |
|
703 |
for (File f: path) { |
|
704 |
dir = f; |
|
705 |
break; |
|
706 |
} |
|
707 |
} |
|
708 |
||
1205
b316e32eb90c
6508981: cleanup file separator handling in JavacFileManager
jjg
parents:
815
diff
changeset
|
709 |
File file = fileName.getFile(dir); // null-safe |
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
710 |
return new RegularFileObject(this, file); |
10 | 711 |
|
712 |
} |
|
713 |
||
714 |
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles( |
|
715 |
Iterable<? extends File> files) |
|
716 |
{ |
|
717 |
ArrayList<RegularFileObject> result; |
|
1789
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1208
diff
changeset
|
718 |
if (files instanceof Collection<?>) |
7ac8c0815000
6765045: Remove rawtypes warnings from langtools
mcimadamore
parents:
1208
diff
changeset
|
719 |
result = new ArrayList<RegularFileObject>(((Collection<?>)files).size()); |
10 | 720 |
else |
721 |
result = new ArrayList<RegularFileObject>(); |
|
722 |
for (File f: files) |
|
810
e4b6a6d206e6
6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents:
809
diff
changeset
|
723 |
result.add(new RegularFileObject(this, nullCheck(f))); |
10 | 724 |
return result; |
725 |
} |
|
726 |
||
727 |
public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) { |
|
728 |
return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files))); |
|
729 |
} |
|
730 |
||
731 |
public void setLocation(Location location, |
|
732 |
Iterable<? extends File> path) |
|
733 |
throws IOException |
|
734 |
{ |
|
735 |
nullCheck(location); |
|
736 |
paths.lazy(); |
|
737 |
||
738 |
final File dir = location.isOutputLocation() ? getOutputDirectory(path) : null; |
|
739 |
||
740 |
if (location == CLASS_OUTPUT) |
|
741 |
classOutDir = getOutputLocation(dir, D); |
|
742 |
else if (location == SOURCE_OUTPUT) |
|
743 |
sourceOutDir = getOutputLocation(dir, S); |
|
744 |
else |
|
745 |
paths.setPathForLocation(location, path); |
|
746 |
} |
|
747 |
// where |
|
748 |
private File getOutputDirectory(Iterable<? extends File> path) throws IOException { |
|
749 |
if (path == null) |
|
750 |
return null; |
|
751 |
Iterator<? extends File> pathIter = path.iterator(); |
|
752 |
if (!pathIter.hasNext()) |
|
753 |
throw new IllegalArgumentException("empty path for directory"); |
|
754 |
File dir = pathIter.next(); |
|
755 |
if (pathIter.hasNext()) |
|
756 |
throw new IllegalArgumentException("path too long for directory"); |
|
757 |
if (!dir.exists()) |
|
758 |
throw new FileNotFoundException(dir + ": does not exist"); |
|
759 |
else if (!dir.isDirectory()) |
|
760 |
throw new IOException(dir + ": not a directory"); |
|
761 |
return dir; |
|
762 |
} |
|
763 |
||
764 |
private File getOutputLocation(File dir, OptionName defaultOptionName) { |
|
765 |
if (dir != null) |
|
766 |
return dir; |
|
767 |
String arg = options.get(defaultOptionName); |
|
768 |
if (arg == null) |
|
769 |
return null; |
|
770 |
return new File(arg); |
|
771 |
} |
|
772 |
||
773 |
public Iterable<? extends File> getLocation(Location location) { |
|
774 |
nullCheck(location); |
|
775 |
paths.lazy(); |
|
776 |
if (location == CLASS_OUTPUT) { |
|
777 |
return (getClassOutDir() == null ? null : List.of(getClassOutDir())); |
|
778 |
} else if (location == SOURCE_OUTPUT) { |
|
779 |
return (getSourceOutDir() == null ? null : List.of(getSourceOutDir())); |
|
780 |
} else |
|
781 |
return paths.getPathForLocation(location); |
|
782 |
} |
|
783 |
||
784 |
private File getClassOutDir() { |
|
785 |
if (classOutDir == uninited) |
|
786 |
classOutDir = getOutputLocation(null, D); |
|
787 |
return classOutDir; |
|
788 |
} |
|
789 |
||
790 |
private File getSourceOutDir() { |
|
791 |
if (sourceOutDir == uninited) |
|
792 |
sourceOutDir = getOutputLocation(null, S); |
|
793 |
return sourceOutDir; |
|
794 |
} |
|
795 |
||
796 |
/** |
|
797 |
* Enforces the specification of a "relative" URI as used in |
|
798 |
* {@linkplain #getFileForInput(Location,String,URI) |
|
799 |
* getFileForInput}. This method must follow the rules defined in |
|
800 |
* that method, do not make any changes without consulting the |
|
801 |
* specification. |
|
802 |
*/ |
|
803 |
protected static boolean isRelativeUri(URI uri) { |
|
804 |
if (uri.isAbsolute()) |
|
805 |
return false; |
|
806 |
String path = uri.normalize().getPath(); |
|
807 |
if (path.length() == 0 /* isEmpty() is mustang API */) |
|
808 |
return false; |
|
809 |
char first = path.charAt(0); |
|
810 |
return first != '.' && first != '/'; |
|
811 |
} |
|
812 |
||
3782 | 813 |
// Convenience method |
814 |
protected static boolean isRelativeUri(String u) { |
|
815 |
try { |
|
816 |
return isRelativeUri(new URI(u)); |
|
817 |
} catch (URISyntaxException e) { |
|
818 |
return false; |
|
819 |
} |
|
820 |
} |
|
821 |
||
10 | 822 |
/** |
823 |
* Converts a relative file name to a relative URI. This is |
|
824 |
* different from File.toURI as this method does not canonicalize |
|
825 |
* the file before creating the URI. Furthermore, no schema is |
|
826 |
* used. |
|
827 |
* @param file a relative file name |
|
828 |
* @return a relative URI |
|
829 |
* @throws IllegalArgumentException if the file name is not |
|
830 |
* relative according to the definition given in {@link |
|
831 |
* javax.tools.JavaFileManager#getFileForInput} |
|
832 |
*/ |
|
833 |
public static String getRelativeName(File file) { |
|
834 |
if (!file.isAbsolute()) { |
|
835 |
String result = file.getPath().replace(File.separatorChar, '/'); |
|
3782 | 836 |
if (isRelativeUri(result)) |
10 | 837 |
return result; |
838 |
} |
|
839 |
throw new IllegalArgumentException("Invalid relative path: " + file); |
|
840 |
} |
|
5007
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
841 |
|
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
842 |
/** |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
843 |
* Get a detail message from an IOException. |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
844 |
* Most, but not all, instances of IOException provide a non-null result |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
845 |
* for getLocalizedMessage(). But some instances return null: in these |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
846 |
* cases, fallover to getMessage(), and if even that is null, return the |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
847 |
* name of the exception itself. |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
848 |
* @param e an IOException |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
849 |
* @return a string to include in a compiler diagnostic |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
850 |
*/ |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
851 |
public static String getMessage(IOException e) { |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
852 |
String s = e.getLocalizedMessage(); |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
853 |
if (s != null) |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
854 |
return s; |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
855 |
s = e.getMessage(); |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
856 |
if (s != null) |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
857 |
return s; |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
858 |
return e.toString(); |
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4548
diff
changeset
|
859 |
} |
10 | 860 |
} |