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