author | jjg |
Tue, 17 Jun 2008 10:44:32 -0700 | |
changeset 809 | 2106a64c0a38 |
parent 731 | 1dd22bdb9ca5 |
child 810 | e4b6a6d206e6 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
2 |
* Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
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 |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
28 |
import java.io.ByteArrayInputStream; |
10 | 29 |
import java.io.ByteArrayOutputStream; |
30 |
import java.io.File; |
|
31 |
import java.io.FileInputStream; |
|
32 |
import java.io.FileNotFoundException; |
|
33 |
import java.io.FileOutputStream; |
|
34 |
import java.io.IOException; |
|
35 |
import java.io.InputStream; |
|
36 |
import java.io.OutputStream; |
|
37 |
import java.io.OutputStreamWriter; |
|
38 |
import java.io.Writer; |
|
39 |
import java.lang.ref.SoftReference; |
|
40 |
import java.net.MalformedURLException; |
|
41 |
import java.net.URI; |
|
42 |
import java.net.URISyntaxException; |
|
43 |
import java.net.URL; |
|
44 |
import java.net.URLClassLoader; |
|
45 |
import java.nio.ByteBuffer; |
|
46 |
import java.nio.CharBuffer; |
|
47 |
import java.nio.channels.FileChannel; |
|
48 |
import java.nio.charset.Charset; |
|
49 |
import java.nio.charset.CharsetDecoder; |
|
50 |
import java.nio.charset.CoderResult; |
|
51 |
import java.nio.charset.CodingErrorAction; |
|
52 |
import java.nio.charset.IllegalCharsetNameException; |
|
53 |
import java.nio.charset.UnsupportedCharsetException; |
|
54 |
import java.util.ArrayList; |
|
55 |
import java.util.Arrays; |
|
56 |
import java.util.Collection; |
|
57 |
import java.util.Collections; |
|
58 |
import java.util.EnumSet; |
|
59 |
import java.util.Enumeration; |
|
60 |
import java.util.HashMap; |
|
61 |
import java.util.Iterator; |
|
62 |
import java.util.Map; |
|
63 |
import java.util.Set; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
64 |
import java.util.concurrent.ConcurrentHashMap; |
10 | 65 |
import java.util.zip.ZipEntry; |
66 |
import java.util.zip.ZipFile; |
|
67 |
||
68 |
import javax.lang.model.SourceVersion; |
|
69 |
import javax.tools.FileObject; |
|
70 |
import javax.tools.JavaFileManager; |
|
71 |
import javax.tools.JavaFileObject; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
72 |
import javax.tools.StandardJavaFileManager; |
10 | 73 |
|
74 |
import com.sun.tools.javac.code.Source; |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
75 |
import com.sun.tools.javac.main.JavacOption; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
76 |
import com.sun.tools.javac.main.OptionName; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
77 |
import com.sun.tools.javac.main.RecognizedOptions; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
78 |
import com.sun.tools.javac.util.Context; |
10 | 79 |
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
80 |
import com.sun.tools.javac.util.List; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
81 |
import com.sun.tools.javac.util.ListBuffer; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
82 |
import com.sun.tools.javac.util.Log; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
83 |
import com.sun.tools.javac.util.Options; |
10 | 84 |
|
85 |
import static com.sun.tools.javac.main.OptionName.*; |
|
86 |
import static javax.tools.StandardLocation.*; |
|
87 |
||
88 |
/** |
|
89 |
* This class provides access to the source, class and other files |
|
90 |
* used by the compiler and related tools. |
|
91 |
*/ |
|
92 |
public class JavacFileManager implements StandardJavaFileManager { |
|
93 |
||
94 |
private static final String[] symbolFileLocation = { "lib", "ct.sym" }; |
|
95 |
private static final String symbolFilePrefix = "META-INF/sym/rt.jar/"; |
|
96 |
||
97 |
boolean useZipFileIndex; |
|
98 |
||
99 |
private static int symbolFilePrefixLength = 0; |
|
100 |
static { |
|
101 |
try { |
|
102 |
symbolFilePrefixLength = symbolFilePrefix.getBytes("UTF-8").length; |
|
103 |
} catch (java.io.UnsupportedEncodingException uee) { |
|
104 |
// Can't happen...UTF-8 is always supported. |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
private static boolean CHECK_ZIP_TIMESTAMP = false; |
|
109 |
private static Map<File, Boolean> isDirectory = new ConcurrentHashMap<File, Boolean>(); |
|
110 |
||
111 |
||
112 |
public static char[] toArray(CharBuffer buffer) { |
|
113 |
if (buffer.hasArray()) |
|
114 |
return ((CharBuffer)buffer.compact().flip()).array(); |
|
115 |
else |
|
116 |
return buffer.toString().toCharArray(); |
|
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* The log to be used for error reporting. |
|
121 |
*/ |
|
122 |
protected Log log; |
|
123 |
||
124 |
/** Encapsulates knowledge of paths |
|
125 |
*/ |
|
126 |
private Paths paths; |
|
127 |
||
128 |
private Options options; |
|
129 |
||
130 |
private final File uninited = new File("U N I N I T E D"); |
|
131 |
||
132 |
private final Set<JavaFileObject.Kind> sourceOrClass = |
|
133 |
EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS); |
|
134 |
||
135 |
/** The standard output directory, primarily used for classes. |
|
136 |
* Initialized by the "-d" option. |
|
137 |
* If classOutDir = null, files are written into same directory as the sources |
|
138 |
* they were generated from. |
|
139 |
*/ |
|
140 |
private File classOutDir = uninited; |
|
141 |
||
142 |
/** The output directory, used when generating sources while processing annotations. |
|
143 |
* Initialized by the "-s" option. |
|
144 |
*/ |
|
145 |
private File sourceOutDir = uninited; |
|
146 |
||
147 |
protected boolean mmappedIO; |
|
148 |
protected boolean ignoreSymbolFile; |
|
149 |
||
150 |
/** |
|
151 |
* User provided charset (through javax.tools). |
|
152 |
*/ |
|
153 |
protected Charset charset; |
|
154 |
||
155 |
/** |
|
156 |
* Register a Context.Factory to create a JavacFileManager. |
|
157 |
*/ |
|
158 |
public static void preRegister(final Context context) { |
|
159 |
context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() { |
|
160 |
public JavaFileManager make() { |
|
161 |
return new JavacFileManager(context, true, null); |
|
162 |
} |
|
163 |
}); |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Create a JavacFileManager using a given context, optionally registering |
|
168 |
* it as the JavaFileManager for that context. |
|
169 |
*/ |
|
170 |
public JavacFileManager(Context context, boolean register, Charset charset) { |
|
171 |
if (register) |
|
172 |
context.put(JavaFileManager.class, this); |
|
173 |
byteBufferCache = new ByteBufferCache(); |
|
174 |
this.charset = charset; |
|
175 |
setContext(context); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Set the context for JavacFileManager. |
|
180 |
*/ |
|
181 |
public void setContext(Context context) { |
|
182 |
log = Log.instance(context); |
|
183 |
if (paths == null) { |
|
184 |
paths = Paths.instance(context); |
|
185 |
} else { |
|
186 |
// Reuse the Paths object as it stores the locations that |
|
187 |
// have been set with setLocation, etc. |
|
188 |
paths.setContext(context); |
|
189 |
} |
|
190 |
||
191 |
options = Options.instance(context); |
|
192 |
||
193 |
useZipFileIndex = System.getProperty("useJavaUtilZip") == null;// TODO: options.get("useJavaUtilZip") == null; |
|
194 |
CHECK_ZIP_TIMESTAMP = System.getProperty("checkZipIndexTimestamp") != null;// TODO: options.get("checkZipIndexTimestamp") != null; |
|
195 |
||
196 |
mmappedIO = options.get("mmappedIO") != null; |
|
197 |
ignoreSymbolFile = options.get("ignore.symbol.file") != null; |
|
198 |
} |
|
199 |
||
200 |
public JavaFileObject getFileForInput(String name) { |
|
201 |
return getRegularFile(new File(name)); |
|
202 |
} |
|
203 |
||
204 |
public JavaFileObject getRegularFile(File file) { |
|
205 |
return new RegularFileObject(file); |
|
206 |
} |
|
207 |
||
208 |
public JavaFileObject getFileForOutput(String classname, |
|
209 |
JavaFileObject.Kind kind, |
|
210 |
JavaFileObject sibling) |
|
211 |
throws IOException |
|
212 |
{ |
|
213 |
return getJavaFileForOutput(CLASS_OUTPUT, classname, kind, sibling); |
|
214 |
} |
|
215 |
||
216 |
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) { |
|
217 |
ListBuffer<File> files = new ListBuffer<File>(); |
|
218 |
for (String name : names) |
|
219 |
files.append(new File(nullCheck(name))); |
|
220 |
return getJavaFileObjectsFromFiles(files.toList()); |
|
221 |
} |
|
222 |
||
223 |
public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) { |
|
224 |
return getJavaFileObjectsFromStrings(Arrays.asList(nullCheck(names))); |
|
225 |
} |
|
226 |
||
227 |
protected JavaFileObject.Kind getKind(String extension) { |
|
228 |
if (extension.equals(JavaFileObject.Kind.CLASS.extension)) |
|
229 |
return JavaFileObject.Kind.CLASS; |
|
230 |
else if (extension.equals(JavaFileObject.Kind.SOURCE.extension)) |
|
231 |
return JavaFileObject.Kind.SOURCE; |
|
232 |
else if (extension.equals(JavaFileObject.Kind.HTML.extension)) |
|
233 |
return JavaFileObject.Kind.HTML; |
|
234 |
else |
|
235 |
return JavaFileObject.Kind.OTHER; |
|
236 |
} |
|
237 |
||
238 |
private static boolean isValidName(String name) { |
|
239 |
// Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ), |
|
240 |
// but the set of keywords depends on the source level, and we don't want |
|
241 |
// impls of JavaFileManager to have to be dependent on the source level. |
|
242 |
// Therefore we simply check that the argument is a sequence of identifiers |
|
243 |
// separated by ".". |
|
244 |
for (String s : name.split("\\.", -1)) { |
|
245 |
if (!SourceVersion.isIdentifier(s)) |
|
246 |
return false; |
|
247 |
} |
|
248 |
return true; |
|
249 |
} |
|
250 |
||
251 |
private static void validateClassName(String className) { |
|
252 |
if (!isValidName(className)) |
|
253 |
throw new IllegalArgumentException("Invalid class name: " + className); |
|
254 |
} |
|
255 |
||
256 |
private static void validatePackageName(String packageName) { |
|
257 |
if (packageName.length() > 0 && !isValidName(packageName)) |
|
258 |
throw new IllegalArgumentException("Invalid packageName name: " + packageName); |
|
259 |
} |
|
260 |
||
261 |
public static void testName(String name, |
|
262 |
boolean isValidPackageName, |
|
263 |
boolean isValidClassName) |
|
264 |
{ |
|
265 |
try { |
|
266 |
validatePackageName(name); |
|
267 |
if (!isValidPackageName) |
|
268 |
throw new AssertionError("Invalid package name accepted: " + name); |
|
269 |
printAscii("Valid package name: \"%s\"", name); |
|
270 |
} catch (IllegalArgumentException e) { |
|
271 |
if (isValidPackageName) |
|
272 |
throw new AssertionError("Valid package name rejected: " + name); |
|
273 |
printAscii("Invalid package name: \"%s\"", name); |
|
274 |
} |
|
275 |
try { |
|
276 |
validateClassName(name); |
|
277 |
if (!isValidClassName) |
|
278 |
throw new AssertionError("Invalid class name accepted: " + name); |
|
279 |
printAscii("Valid class name: \"%s\"", name); |
|
280 |
} catch (IllegalArgumentException e) { |
|
281 |
if (isValidClassName) |
|
282 |
throw new AssertionError("Valid class name rejected: " + name); |
|
283 |
printAscii("Invalid class name: \"%s\"", name); |
|
284 |
} |
|
285 |
} |
|
286 |
private static void printAscii(String format, Object... args) { |
|
287 |
String message; |
|
288 |
try { |
|
289 |
final String ascii = "US-ASCII"; |
|
290 |
message = new String(String.format(null, format, args).getBytes(ascii), ascii); |
|
291 |
} catch (java.io.UnsupportedEncodingException ex) { |
|
292 |
throw new AssertionError(ex); |
|
293 |
} |
|
294 |
System.out.println(message); |
|
295 |
} |
|
296 |
||
297 |
/** Return external representation of name, |
|
298 |
* converting '.' to File.separatorChar. |
|
299 |
*/ |
|
300 |
private static String externalizeFileName(CharSequence name) { |
|
301 |
return name.toString().replace('.', File.separatorChar); |
|
302 |
} |
|
303 |
||
304 |
private static String externalizeFileName(CharSequence n, JavaFileObject.Kind kind) { |
|
305 |
return externalizeFileName(n) + kind.extension; |
|
306 |
} |
|
307 |
||
308 |
private static String baseName(String fileName) { |
|
309 |
return fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1); |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* Insert all files in subdirectory `subdirectory' of `directory' which end |
|
314 |
* in one of the extensions in `extensions' into packageSym. |
|
315 |
*/ |
|
316 |
private void listDirectory(File directory, |
|
317 |
String subdirectory, |
|
318 |
Set<JavaFileObject.Kind> fileKinds, |
|
319 |
boolean recurse, |
|
320 |
ListBuffer<JavaFileObject> l) { |
|
321 |
Archive archive = archives.get(directory); |
|
322 |
||
323 |
boolean isFile = false; |
|
324 |
if (CHECK_ZIP_TIMESTAMP) { |
|
325 |
Boolean isf = isDirectory.get(directory); |
|
326 |
if (isf == null) { |
|
327 |
isFile = directory.isFile(); |
|
328 |
isDirectory.put(directory, isFile); |
|
329 |
} |
|
330 |
else { |
|
331 |
isFile = directory.isFile(); |
|
332 |
} |
|
333 |
} |
|
334 |
else { |
|
335 |
isFile = directory.isFile(); |
|
336 |
} |
|
337 |
||
338 |
if (archive != null || isFile) { |
|
339 |
if (archive == null) { |
|
340 |
try { |
|
341 |
archive = openArchive(directory); |
|
342 |
} catch (IOException ex) { |
|
343 |
log.error("error.reading.file", |
|
344 |
directory, ex.getLocalizedMessage()); |
|
345 |
return; |
|
346 |
} |
|
347 |
} |
|
348 |
if (subdirectory.length() != 0) { |
|
349 |
if (!useZipFileIndex) { |
|
350 |
subdirectory = subdirectory.replace('\\', '/'); |
|
351 |
if (!subdirectory.endsWith("/")) subdirectory = subdirectory + "/"; |
|
352 |
} |
|
353 |
else { |
|
354 |
if (File.separatorChar == '/') { |
|
355 |
subdirectory = subdirectory.replace('\\', '/'); |
|
356 |
} |
|
357 |
else { |
|
358 |
subdirectory = subdirectory.replace('/', '\\'); |
|
359 |
} |
|
360 |
||
361 |
if (!subdirectory.endsWith(File.separator)) subdirectory = subdirectory + File.separator; |
|
362 |
} |
|
363 |
} |
|
364 |
||
365 |
List<String> files = archive.getFiles(subdirectory); |
|
366 |
if (files != null) { |
|
367 |
for (String file; !files.isEmpty(); files = files.tail) { |
|
368 |
file = files.head; |
|
369 |
if (isValidFile(file, fileKinds)) { |
|
370 |
l.append(archive.getFileObject(subdirectory, file)); |
|
371 |
} |
|
372 |
} |
|
373 |
} |
|
374 |
if (recurse) { |
|
375 |
for (String s: archive.getSubdirectories()) { |
|
376 |
if (s.startsWith(subdirectory) && !s.equals(subdirectory)) { |
|
377 |
// Because the archive map is a flat list of directories, |
|
378 |
// the enclosing loop will pick up all child subdirectories. |
|
379 |
// Therefore, there is no need to recurse deeper. |
|
380 |
listDirectory(directory, s, fileKinds, false, l); |
|
381 |
} |
|
382 |
} |
|
383 |
} |
|
384 |
} else { |
|
385 |
File d = subdirectory.length() != 0 |
|
386 |
? new File(directory, subdirectory) |
|
387 |
: directory; |
|
388 |
if (!caseMapCheck(d, subdirectory)) |
|
389 |
return; |
|
390 |
||
391 |
File[] files = d.listFiles(); |
|
392 |
if (files == null) |
|
393 |
return; |
|
394 |
||
395 |
for (File f: files) { |
|
396 |
String fname = f.getName(); |
|
397 |
if (f.isDirectory()) { |
|
398 |
if (recurse && SourceVersion.isIdentifier(fname)) { |
|
399 |
listDirectory(directory, |
|
400 |
subdirectory + File.separator + fname, |
|
401 |
fileKinds, |
|
402 |
recurse, |
|
403 |
l); |
|
404 |
} |
|
405 |
} else { |
|
406 |
if (isValidFile(fname, fileKinds)) { |
|
407 |
JavaFileObject fe = |
|
408 |
new RegularFileObject(fname, new File(d, fname)); |
|
409 |
l.append(fe); |
|
410 |
} |
|
411 |
} |
|
412 |
} |
|
413 |
} |
|
414 |
} |
|
415 |
||
416 |
private boolean isValidFile(String s, Set<JavaFileObject.Kind> fileKinds) { |
|
417 |
int lastDot = s.lastIndexOf("."); |
|
418 |
String extn = (lastDot == -1 ? s : s.substring(lastDot)); |
|
419 |
JavaFileObject.Kind kind = getKind(extn); |
|
420 |
return fileKinds.contains(kind); |
|
421 |
} |
|
422 |
||
423 |
private static final boolean fileSystemIsCaseSensitive = |
|
424 |
File.separatorChar == '/'; |
|
425 |
||
426 |
/** Hack to make Windows case sensitive. Test whether given path |
|
427 |
* ends in a string of characters with the same case as given name. |
|
428 |
* Ignore file separators in both path and name. |
|
429 |
*/ |
|
430 |
private boolean caseMapCheck(File f, String name) { |
|
431 |
if (fileSystemIsCaseSensitive) return true; |
|
432 |
// Note that getCanonicalPath() returns the case-sensitive |
|
433 |
// spelled file name. |
|
434 |
String path; |
|
435 |
try { |
|
436 |
path = f.getCanonicalPath(); |
|
437 |
} catch (IOException ex) { |
|
438 |
return false; |
|
439 |
} |
|
440 |
char[] pcs = path.toCharArray(); |
|
441 |
char[] ncs = name.toCharArray(); |
|
442 |
int i = pcs.length - 1; |
|
443 |
int j = ncs.length - 1; |
|
444 |
while (i >= 0 && j >= 0) { |
|
445 |
while (i >= 0 && pcs[i] == File.separatorChar) i--; |
|
446 |
while (j >= 0 && ncs[j] == File.separatorChar) j--; |
|
447 |
if (i >= 0 && j >= 0) { |
|
448 |
if (pcs[i] != ncs[j]) return false; |
|
449 |
i--; |
|
450 |
j--; |
|
451 |
} |
|
452 |
} |
|
453 |
return j < 0; |
|
454 |
} |
|
455 |
||
456 |
/** |
|
457 |
* An archive provides a flat directory structure of a ZipFile by |
|
458 |
* mapping directory names to lists of files (basenames). |
|
459 |
*/ |
|
460 |
public interface Archive { |
|
461 |
void close() throws IOException; |
|
462 |
||
463 |
boolean contains(String name); |
|
464 |
||
465 |
JavaFileObject getFileObject(String subdirectory, String file); |
|
466 |
||
467 |
List<String> getFiles(String subdirectory); |
|
468 |
||
469 |
Set<String> getSubdirectories(); |
|
470 |
} |
|
471 |
||
472 |
public class ZipArchive implements Archive { |
|
473 |
protected final Map<String,List<String>> map; |
|
474 |
protected final ZipFile zdir; |
|
475 |
public ZipArchive(ZipFile zdir) throws IOException { |
|
476 |
this.zdir = zdir; |
|
477 |
this.map = new HashMap<String,List<String>>(); |
|
478 |
for (Enumeration<? extends ZipEntry> e = zdir.entries(); e.hasMoreElements(); ) { |
|
479 |
ZipEntry entry; |
|
480 |
try { |
|
481 |
entry = e.nextElement(); |
|
482 |
} catch (InternalError ex) { |
|
483 |
IOException io = new IOException(); |
|
484 |
io.initCause(ex); // convenience constructors added in Mustang :-( |
|
485 |
throw io; |
|
486 |
} |
|
487 |
addZipEntry(entry); |
|
488 |
} |
|
489 |
} |
|
490 |
||
491 |
void addZipEntry(ZipEntry entry) { |
|
492 |
String name = entry.getName(); |
|
493 |
int i = name.lastIndexOf('/'); |
|
494 |
String dirname = name.substring(0, i+1); |
|
495 |
String basename = name.substring(i+1); |
|
496 |
if (basename.length() == 0) |
|
497 |
return; |
|
498 |
List<String> list = map.get(dirname); |
|
499 |
if (list == null) |
|
500 |
list = List.nil(); |
|
501 |
list = list.prepend(basename); |
|
502 |
map.put(dirname, list); |
|
503 |
} |
|
504 |
||
505 |
public boolean contains(String name) { |
|
506 |
int i = name.lastIndexOf('/'); |
|
507 |
String dirname = name.substring(0, i+1); |
|
508 |
String basename = name.substring(i+1); |
|
509 |
if (basename.length() == 0) |
|
510 |
return false; |
|
511 |
List<String> list = map.get(dirname); |
|
512 |
return (list != null && list.contains(basename)); |
|
513 |
} |
|
514 |
||
515 |
public List<String> getFiles(String subdirectory) { |
|
516 |
return map.get(subdirectory); |
|
517 |
} |
|
518 |
||
519 |
public JavaFileObject getFileObject(String subdirectory, String file) { |
|
520 |
ZipEntry ze = zdir.getEntry(subdirectory + file); |
|
521 |
return new ZipFileObject(file, zdir, ze); |
|
522 |
} |
|
523 |
||
524 |
public Set<String> getSubdirectories() { |
|
525 |
return map.keySet(); |
|
526 |
} |
|
527 |
||
528 |
public void close() throws IOException { |
|
529 |
zdir.close(); |
|
530 |
} |
|
531 |
} |
|
532 |
||
533 |
public class SymbolArchive extends ZipArchive { |
|
534 |
final File origFile; |
|
535 |
public SymbolArchive(File orig, ZipFile zdir) throws IOException { |
|
536 |
super(zdir); |
|
537 |
this.origFile = orig; |
|
538 |
} |
|
539 |
||
540 |
@Override |
|
541 |
void addZipEntry(ZipEntry entry) { |
|
542 |
// called from super constructor, may not refer to origFile. |
|
543 |
String name = entry.getName(); |
|
544 |
if (!name.startsWith(symbolFilePrefix)) |
|
545 |
return; |
|
546 |
name = name.substring(symbolFilePrefix.length()); |
|
547 |
int i = name.lastIndexOf('/'); |
|
548 |
String dirname = name.substring(0, i+1); |
|
549 |
String basename = name.substring(i+1); |
|
550 |
if (basename.length() == 0) |
|
551 |
return; |
|
552 |
List<String> list = map.get(dirname); |
|
553 |
if (list == null) |
|
554 |
list = List.nil(); |
|
555 |
list = list.prepend(basename); |
|
556 |
map.put(dirname, list); |
|
557 |
} |
|
558 |
||
559 |
@Override |
|
560 |
public JavaFileObject getFileObject(String subdirectory, String file) { |
|
561 |
return super.getFileObject(symbolFilePrefix + subdirectory, file); |
|
562 |
} |
|
563 |
} |
|
564 |
||
565 |
public class MissingArchive implements Archive { |
|
566 |
final File zipFileName; |
|
567 |
public MissingArchive(File name) { |
|
568 |
zipFileName = name; |
|
569 |
} |
|
570 |
public boolean contains(String name) { |
|
571 |
return false; |
|
572 |
} |
|
573 |
||
574 |
public void close() { |
|
575 |
} |
|
576 |
||
577 |
public JavaFileObject getFileObject(String subdirectory, String file) { |
|
578 |
return null; |
|
579 |
} |
|
580 |
||
581 |
public List<String> getFiles(String subdirectory) { |
|
582 |
return List.nil(); |
|
583 |
} |
|
584 |
||
585 |
public Set<String> getSubdirectories() { |
|
586 |
return Collections.emptySet(); |
|
587 |
} |
|
588 |
} |
|
589 |
||
590 |
/** A directory of zip files already opened. |
|
591 |
*/ |
|
592 |
Map<File, Archive> archives = new HashMap<File,Archive>(); |
|
593 |
||
594 |
/** Open a new zip file directory. |
|
595 |
*/ |
|
596 |
protected Archive openArchive(File zipFileName) throws IOException { |
|
597 |
Archive archive = archives.get(zipFileName); |
|
598 |
if (archive == null) { |
|
599 |
File origZipFileName = zipFileName; |
|
600 |
if (!ignoreSymbolFile && paths.isBootClassPathRtJar(zipFileName)) { |
|
601 |
File file = zipFileName.getParentFile().getParentFile(); // ${java.home} |
|
602 |
if (new File(file.getName()).equals(new File("jre"))) |
|
603 |
file = file.getParentFile(); |
|
604 |
// file == ${jdk.home} |
|
605 |
for (String name : symbolFileLocation) |
|
606 |
file = new File(file, name); |
|
607 |
// file == ${jdk.home}/lib/ct.sym |
|
608 |
if (file.exists()) |
|
609 |
zipFileName = file; |
|
610 |
} |
|
611 |
||
612 |
try { |
|
613 |
||
614 |
ZipFile zdir = null; |
|
615 |
||
616 |
boolean usePreindexedCache = false; |
|
617 |
String preindexCacheLocation = null; |
|
618 |
||
619 |
if (!useZipFileIndex) { |
|
620 |
zdir = new ZipFile(zipFileName); |
|
621 |
} |
|
622 |
else { |
|
623 |
usePreindexedCache = options.get("usezipindex") != null; |
|
624 |
preindexCacheLocation = options.get("java.io.tmpdir"); |
|
625 |
String optCacheLoc = options.get("cachezipindexdir"); |
|
626 |
||
627 |
if (optCacheLoc != null && optCacheLoc.length() != 0) { |
|
628 |
if (optCacheLoc.startsWith("\"")) { |
|
629 |
if (optCacheLoc.endsWith("\"")) { |
|
630 |
optCacheLoc = optCacheLoc.substring(1, optCacheLoc.length() - 1); |
|
631 |
} |
|
632 |
else { |
|
633 |
optCacheLoc = optCacheLoc.substring(1); |
|
634 |
} |
|
635 |
} |
|
636 |
||
637 |
File cacheDir = new File(optCacheLoc); |
|
638 |
if (cacheDir.exists() && cacheDir.canWrite()) { |
|
639 |
preindexCacheLocation = optCacheLoc; |
|
640 |
if (!preindexCacheLocation.endsWith("/") && |
|
641 |
!preindexCacheLocation.endsWith(File.separator)) { |
|
642 |
preindexCacheLocation += File.separator; |
|
643 |
} |
|
644 |
} |
|
645 |
} |
|
646 |
} |
|
647 |
||
648 |
if (origZipFileName == zipFileName) { |
|
649 |
if (!useZipFileIndex) { |
|
650 |
archive = new ZipArchive(zdir); |
|
651 |
} else { |
|
652 |
archive = new ZipFileIndexArchive(this, ZipFileIndex.getZipFileIndex(zipFileName, 0, |
|
653 |
usePreindexedCache, preindexCacheLocation, options.get("writezipindexfiles") != null)); |
|
654 |
} |
|
655 |
} |
|
656 |
else { |
|
657 |
if (!useZipFileIndex) { |
|
658 |
archive = new SymbolArchive(origZipFileName, zdir); |
|
659 |
} |
|
660 |
else { |
|
661 |
archive = new ZipFileIndexArchive(this, ZipFileIndex.getZipFileIndex(zipFileName, symbolFilePrefixLength, |
|
662 |
usePreindexedCache, preindexCacheLocation, options.get("writezipindexfiles") != null)); |
|
663 |
} |
|
664 |
} |
|
665 |
} catch (FileNotFoundException ex) { |
|
666 |
archive = new MissingArchive(zipFileName); |
|
667 |
} catch (IOException ex) { |
|
809
2106a64c0a38
6625520: javac handles missing entries on classpath badly
jjg
parents:
731
diff
changeset
|
668 |
if (zipFileName.exists()) |
2106a64c0a38
6625520: javac handles missing entries on classpath badly
jjg
parents:
731
diff
changeset
|
669 |
log.error("error.reading.file", zipFileName, ex.getLocalizedMessage()); |
10 | 670 |
archive = new MissingArchive(zipFileName); |
671 |
} |
|
672 |
||
673 |
archives.put(origZipFileName, archive); |
|
674 |
} |
|
675 |
return archive; |
|
676 |
} |
|
677 |
||
678 |
/** Flush any output resources. |
|
679 |
*/ |
|
680 |
public void flush() { |
|
681 |
contentCache.clear(); |
|
682 |
} |
|
683 |
||
684 |
/** |
|
685 |
* Close the JavaFileManager, releasing resources. |
|
686 |
*/ |
|
687 |
public void close() { |
|
688 |
for (Iterator<Archive> i = archives.values().iterator(); i.hasNext(); ) { |
|
689 |
Archive a = i.next(); |
|
690 |
i.remove(); |
|
691 |
try { |
|
692 |
a.close(); |
|
693 |
} catch (IOException e) { |
|
694 |
} |
|
695 |
} |
|
696 |
} |
|
697 |
||
698 |
private Map<JavaFileObject, SoftReference<CharBuffer>> contentCache = new HashMap<JavaFileObject, SoftReference<CharBuffer>>(); |
|
699 |
||
700 |
private String defaultEncodingName; |
|
701 |
private String getDefaultEncodingName() { |
|
702 |
if (defaultEncodingName == null) { |
|
703 |
defaultEncodingName = |
|
704 |
new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding(); |
|
705 |
} |
|
706 |
return defaultEncodingName; |
|
707 |
} |
|
708 |
||
709 |
protected String getEncodingName() { |
|
710 |
String encName = options.get(OptionName.ENCODING); |
|
711 |
if (encName == null) |
|
712 |
return getDefaultEncodingName(); |
|
713 |
else |
|
714 |
return encName; |
|
715 |
} |
|
716 |
||
717 |
protected Source getSource() { |
|
718 |
String sourceName = options.get(OptionName.SOURCE); |
|
719 |
Source source = null; |
|
720 |
if (sourceName != null) |
|
721 |
source = Source.lookup(sourceName); |
|
722 |
return (source != null ? source : Source.DEFAULT); |
|
723 |
} |
|
724 |
||
725 |
/** |
|
726 |
* Make a byte buffer from an input stream. |
|
727 |
*/ |
|
728 |
private ByteBuffer makeByteBuffer(InputStream in) |
|
729 |
throws IOException { |
|
730 |
int limit = in.available(); |
|
731 |
if (mmappedIO && in instanceof FileInputStream) { |
|
732 |
// Experimental memory mapped I/O |
|
733 |
FileInputStream fin = (FileInputStream)in; |
|
734 |
return fin.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, limit); |
|
735 |
} |
|
736 |
if (limit < 1024) limit = 1024; |
|
737 |
ByteBuffer result = byteBufferCache.get(limit); |
|
738 |
int position = 0; |
|
739 |
while (in.available() != 0) { |
|
740 |
if (position >= limit) |
|
741 |
// expand buffer |
|
742 |
result = ByteBuffer. |
|
743 |
allocate(limit <<= 1). |
|
744 |
put((ByteBuffer)result.flip()); |
|
745 |
int count = in.read(result.array(), |
|
746 |
position, |
|
747 |
limit - position); |
|
748 |
if (count < 0) break; |
|
749 |
result.position(position += count); |
|
750 |
} |
|
751 |
return (ByteBuffer)result.flip(); |
|
752 |
} |
|
753 |
||
754 |
/** |
|
755 |
* A single-element cache of direct byte buffers. |
|
756 |
*/ |
|
757 |
private static class ByteBufferCache { |
|
758 |
private ByteBuffer cached; |
|
759 |
ByteBuffer get(int capacity) { |
|
760 |
if (capacity < 20480) capacity = 20480; |
|
761 |
ByteBuffer result = |
|
762 |
(cached != null && cached.capacity() >= capacity) |
|
763 |
? (ByteBuffer)cached.clear() |
|
764 |
: ByteBuffer.allocate(capacity + capacity>>1); |
|
765 |
cached = null; |
|
766 |
return result; |
|
767 |
} |
|
768 |
void put(ByteBuffer x) { |
|
769 |
cached = x; |
|
770 |
} |
|
771 |
} |
|
772 |
private final ByteBufferCache byteBufferCache; |
|
773 |
||
774 |
private CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) { |
|
775 |
Charset charset = (this.charset == null) |
|
776 |
? Charset.forName(encodingName) |
|
777 |
: this.charset; |
|
778 |
CharsetDecoder decoder = charset.newDecoder(); |
|
779 |
||
780 |
CodingErrorAction action; |
|
781 |
if (ignoreEncodingErrors) |
|
782 |
action = CodingErrorAction.REPLACE; |
|
783 |
else |
|
784 |
action = CodingErrorAction.REPORT; |
|
785 |
||
786 |
return decoder |
|
787 |
.onMalformedInput(action) |
|
788 |
.onUnmappableCharacter(action); |
|
789 |
} |
|
790 |
||
791 |
/** |
|
792 |
* Decode a ByteBuffer into a CharBuffer. |
|
793 |
*/ |
|
794 |
private CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) { |
|
795 |
String encodingName = getEncodingName(); |
|
796 |
CharsetDecoder decoder; |
|
797 |
try { |
|
798 |
decoder = getDecoder(encodingName, ignoreEncodingErrors); |
|
799 |
} catch (IllegalCharsetNameException e) { |
|
800 |
log.error("unsupported.encoding", encodingName); |
|
801 |
return (CharBuffer)CharBuffer.allocate(1).flip(); |
|
802 |
} catch (UnsupportedCharsetException e) { |
|
803 |
log.error("unsupported.encoding", encodingName); |
|
804 |
return (CharBuffer)CharBuffer.allocate(1).flip(); |
|
805 |
} |
|
806 |
||
807 |
// slightly overestimate the buffer size to avoid reallocation. |
|
808 |
float factor = |
|
809 |
decoder.averageCharsPerByte() * 0.8f + |
|
810 |
decoder.maxCharsPerByte() * 0.2f; |
|
811 |
CharBuffer dest = CharBuffer. |
|
812 |
allocate(10 + (int)(inbuf.remaining()*factor)); |
|
813 |
||
814 |
while (true) { |
|
815 |
CoderResult result = decoder.decode(inbuf, dest, true); |
|
816 |
dest.flip(); |
|
817 |
||
818 |
if (result.isUnderflow()) { // done reading |
|
819 |
// make sure there is at least one extra character |
|
820 |
if (dest.limit() == dest.capacity()) { |
|
821 |
dest = CharBuffer.allocate(dest.capacity()+1).put(dest); |
|
822 |
dest.flip(); |
|
823 |
} |
|
824 |
return dest; |
|
825 |
} else if (result.isOverflow()) { // buffer too small; expand |
|
826 |
int newCapacity = |
|
827 |
10 + dest.capacity() + |
|
828 |
(int)(inbuf.remaining()*decoder.maxCharsPerByte()); |
|
829 |
dest = CharBuffer.allocate(newCapacity).put(dest); |
|
830 |
} else if (result.isMalformed() || result.isUnmappable()) { |
|
831 |
// bad character in input |
|
832 |
||
833 |
// report coding error (warn only pre 1.5) |
|
834 |
if (!getSource().allowEncodingErrors()) { |
|
835 |
log.error(new SimpleDiagnosticPosition(dest.limit()), |
|
836 |
"illegal.char.for.encoding", |
|
837 |
charset == null ? encodingName : charset.name()); |
|
838 |
} else { |
|
839 |
log.warning(new SimpleDiagnosticPosition(dest.limit()), |
|
840 |
"illegal.char.for.encoding", |
|
841 |
charset == null ? encodingName : charset.name()); |
|
842 |
} |
|
843 |
||
844 |
// skip past the coding error |
|
845 |
inbuf.position(inbuf.position() + result.length()); |
|
846 |
||
847 |
// undo the flip() to prepare the output buffer |
|
848 |
// for more translation |
|
849 |
dest.position(dest.limit()); |
|
850 |
dest.limit(dest.capacity()); |
|
851 |
dest.put((char)0xfffd); // backward compatible |
|
852 |
} else { |
|
853 |
throw new AssertionError(result); |
|
854 |
} |
|
855 |
} |
|
856 |
// unreached |
|
857 |
} |
|
858 |
||
859 |
public ClassLoader getClassLoader(Location location) { |
|
860 |
nullCheck(location); |
|
861 |
Iterable<? extends File> path = getLocation(location); |
|
862 |
if (path == null) |
|
863 |
return null; |
|
864 |
ListBuffer<URL> lb = new ListBuffer<URL>(); |
|
865 |
for (File f: path) { |
|
866 |
try { |
|
867 |
lb.append(f.toURI().toURL()); |
|
868 |
} catch (MalformedURLException e) { |
|
869 |
throw new AssertionError(e); |
|
870 |
} |
|
871 |
} |
|
872 |
return new URLClassLoader(lb.toArray(new URL[lb.size()]), |
|
873 |
getClass().getClassLoader()); |
|
874 |
} |
|
875 |
||
876 |
public Iterable<JavaFileObject> list(Location location, |
|
877 |
String packageName, |
|
878 |
Set<JavaFileObject.Kind> kinds, |
|
879 |
boolean recurse) |
|
880 |
throws IOException |
|
881 |
{ |
|
882 |
// validatePackageName(packageName); |
|
883 |
nullCheck(packageName); |
|
884 |
nullCheck(kinds); |
|
885 |
||
886 |
Iterable<? extends File> path = getLocation(location); |
|
887 |
if (path == null) |
|
888 |
return List.nil(); |
|
889 |
String subdirectory = externalizeFileName(packageName); |
|
890 |
ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>(); |
|
891 |
||
892 |
for (File directory : path) |
|
893 |
listDirectory(directory, subdirectory, kinds, recurse, results); |
|
894 |
||
895 |
return results.toList(); |
|
896 |
} |
|
897 |
||
898 |
public String inferBinaryName(Location location, JavaFileObject file) { |
|
899 |
file.getClass(); // null check |
|
900 |
location.getClass(); // null check |
|
901 |
// Need to match the path semantics of list(location, ...) |
|
902 |
Iterable<? extends File> path = getLocation(location); |
|
903 |
if (path == null) { |
|
904 |
//System.err.println("Path for " + location + " is null"); |
|
905 |
return null; |
|
906 |
} |
|
907 |
//System.err.println("Path for " + location + " is " + path); |
|
908 |
||
909 |
if (file instanceof RegularFileObject) { |
|
910 |
RegularFileObject r = (RegularFileObject) file; |
|
911 |
String rPath = r.getPath(); |
|
912 |
//System.err.println("RegularFileObject " + file + " " +r.getPath()); |
|
913 |
for (File dir: path) { |
|
914 |
//System.err.println("dir: " + dir); |
|
915 |
String dPath = dir.getPath(); |
|
916 |
if (!dPath.endsWith(File.separator)) |
|
917 |
dPath += File.separator; |
|
918 |
if (rPath.regionMatches(true, 0, dPath, 0, dPath.length()) |
|
919 |
&& new File(rPath.substring(0, dPath.length())).equals(new File(dPath))) { |
|
920 |
String relativeName = rPath.substring(dPath.length()); |
|
921 |
return removeExtension(relativeName).replace(File.separatorChar, '.'); |
|
922 |
} |
|
923 |
} |
|
924 |
} else if (file instanceof ZipFileObject) { |
|
925 |
ZipFileObject z = (ZipFileObject) file; |
|
926 |
String entryName = z.getZipEntryName(); |
|
927 |
if (entryName.startsWith(symbolFilePrefix)) |
|
928 |
entryName = entryName.substring(symbolFilePrefix.length()); |
|
929 |
return removeExtension(entryName).replace('/', '.'); |
|
930 |
} else if (file instanceof ZipFileIndexFileObject) { |
|
931 |
ZipFileIndexFileObject z = (ZipFileIndexFileObject) file; |
|
932 |
String entryName = z.getZipEntryName(); |
|
933 |
if (entryName.startsWith(symbolFilePrefix)) |
|
934 |
entryName = entryName.substring(symbolFilePrefix.length()); |
|
935 |
return removeExtension(entryName).replace(File.separatorChar, '.'); |
|
936 |
} else |
|
937 |
throw new IllegalArgumentException(file.getClass().getName()); |
|
938 |
// System.err.println("inferBinaryName failed for " + file); |
|
939 |
return null; |
|
940 |
} |
|
941 |
// where |
|
942 |
private static String removeExtension(String fileName) { |
|
943 |
int lastDot = fileName.lastIndexOf("."); |
|
944 |
return (lastDot == -1 ? fileName : fileName.substring(0, lastDot)); |
|
945 |
} |
|
946 |
||
947 |
public boolean isSameFile(FileObject a, FileObject b) { |
|
948 |
nullCheck(a); |
|
949 |
nullCheck(b); |
|
950 |
if (!(a instanceof BaseFileObject)) |
|
951 |
throw new IllegalArgumentException("Not supported: " + a); |
|
952 |
if (!(b instanceof BaseFileObject)) |
|
953 |
throw new IllegalArgumentException("Not supported: " + b); |
|
954 |
return a.equals(b); |
|
955 |
} |
|
956 |
||
957 |
public boolean handleOption(String current, Iterator<String> remaining) { |
|
958 |
for (JavacOption o: javacFileManagerOptions) { |
|
959 |
if (o.matches(current)) { |
|
960 |
if (o.hasArg()) { |
|
961 |
if (remaining.hasNext()) { |
|
962 |
if (!o.process(options, current, remaining.next())) |
|
963 |
return true; |
|
964 |
} |
|
965 |
} else { |
|
966 |
if (!o.process(options, current)) |
|
967 |
return true; |
|
968 |
} |
|
969 |
// operand missing, or process returned false |
|
970 |
throw new IllegalArgumentException(current); |
|
971 |
} |
|
972 |
} |
|
973 |
||
974 |
return false; |
|
975 |
} |
|
976 |
// where |
|
977 |
private static JavacOption[] javacFileManagerOptions = |
|
978 |
RecognizedOptions.getJavacFileManagerOptions( |
|
979 |
new RecognizedOptions.GrumpyHelper()); |
|
980 |
||
981 |
public int isSupportedOption(String option) { |
|
982 |
for (JavacOption o : javacFileManagerOptions) { |
|
983 |
if (o.matches(option)) |
|
984 |
return o.hasArg() ? 1 : 0; |
|
985 |
} |
|
986 |
return -1; |
|
987 |
} |
|
988 |
||
989 |
public boolean hasLocation(Location location) { |
|
990 |
return getLocation(location) != null; |
|
991 |
} |
|
992 |
||
993 |
public JavaFileObject getJavaFileForInput(Location location, |
|
994 |
String className, |
|
995 |
JavaFileObject.Kind kind) |
|
996 |
throws IOException |
|
997 |
{ |
|
998 |
nullCheck(location); |
|
999 |
// validateClassName(className); |
|
1000 |
nullCheck(className); |
|
1001 |
nullCheck(kind); |
|
1002 |
if (!sourceOrClass.contains(kind)) |
|
1003 |
throw new IllegalArgumentException("Invalid kind " + kind); |
|
1004 |
return getFileForInput(location, externalizeFileName(className, kind)); |
|
1005 |
} |
|
1006 |
||
1007 |
public FileObject getFileForInput(Location location, |
|
1008 |
String packageName, |
|
1009 |
String relativeName) |
|
1010 |
throws IOException |
|
1011 |
{ |
|
1012 |
nullCheck(location); |
|
1013 |
// validatePackageName(packageName); |
|
1014 |
nullCheck(packageName); |
|
1015 |
if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701 |
|
1016 |
throw new IllegalArgumentException("Invalid relative name: " + relativeName); |
|
1017 |
String name = packageName.length() == 0 |
|
1018 |
? relativeName |
|
1019 |
: new File(externalizeFileName(packageName), relativeName).getPath(); |
|
1020 |
return getFileForInput(location, name); |
|
1021 |
} |
|
1022 |
||
1023 |
private JavaFileObject getFileForInput(Location location, String name) throws IOException { |
|
1024 |
Iterable<? extends File> path = getLocation(location); |
|
1025 |
if (path == null) |
|
1026 |
return null; |
|
1027 |
||
1028 |
for (File dir: path) { |
|
1029 |
if (dir.isDirectory()) { |
|
1030 |
File f = new File(dir, name.replace('/', File.separatorChar)); |
|
1031 |
if (f.exists()) |
|
1032 |
return new RegularFileObject(f); |
|
1033 |
} else { |
|
1034 |
Archive a = openArchive(dir); |
|
1035 |
if (a.contains(name)) { |
|
1036 |
int i = name.lastIndexOf('/'); |
|
1037 |
String dirname = name.substring(0, i+1); |
|
1038 |
String basename = name.substring(i+1); |
|
1039 |
return a.getFileObject(dirname, basename); |
|
1040 |
} |
|
1041 |
||
1042 |
} |
|
1043 |
} |
|
1044 |
return null; |
|
1045 |
||
1046 |
} |
|
1047 |
||
1048 |
public JavaFileObject getJavaFileForOutput(Location location, |
|
1049 |
String className, |
|
1050 |
JavaFileObject.Kind kind, |
|
1051 |
FileObject sibling) |
|
1052 |
throws IOException |
|
1053 |
{ |
|
1054 |
nullCheck(location); |
|
1055 |
// validateClassName(className); |
|
1056 |
nullCheck(className); |
|
1057 |
nullCheck(kind); |
|
1058 |
if (!sourceOrClass.contains(kind)) |
|
1059 |
throw new IllegalArgumentException("Invalid kind " + kind); |
|
1060 |
return getFileForOutput(location, externalizeFileName(className, kind), sibling); |
|
1061 |
} |
|
1062 |
||
1063 |
public FileObject getFileForOutput(Location location, |
|
1064 |
String packageName, |
|
1065 |
String relativeName, |
|
1066 |
FileObject sibling) |
|
1067 |
throws IOException |
|
1068 |
{ |
|
1069 |
nullCheck(location); |
|
1070 |
// validatePackageName(packageName); |
|
1071 |
nullCheck(packageName); |
|
1072 |
if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701 |
|
1073 |
throw new IllegalArgumentException("relativeName is invalid"); |
|
1074 |
String name = packageName.length() == 0 |
|
1075 |
? relativeName |
|
1076 |
: new File(externalizeFileName(packageName), relativeName).getPath(); |
|
1077 |
return getFileForOutput(location, name, sibling); |
|
1078 |
} |
|
1079 |
||
1080 |
private JavaFileObject getFileForOutput(Location location, |
|
1081 |
String fileName, |
|
1082 |
FileObject sibling) |
|
1083 |
throws IOException |
|
1084 |
{ |
|
1085 |
File dir; |
|
1086 |
if (location == CLASS_OUTPUT) { |
|
1087 |
if (getClassOutDir() != null) { |
|
1088 |
dir = getClassOutDir(); |
|
1089 |
} else { |
|
1090 |
File siblingDir = null; |
|
1091 |
if (sibling != null && sibling instanceof RegularFileObject) { |
|
1092 |
siblingDir = ((RegularFileObject)sibling).f.getParentFile(); |
|
1093 |
} |
|
1094 |
return new RegularFileObject(new File(siblingDir, baseName(fileName))); |
|
1095 |
} |
|
1096 |
} else if (location == SOURCE_OUTPUT) { |
|
1097 |
dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir()); |
|
1098 |
} else { |
|
1099 |
Iterable<? extends File> path = paths.getPathForLocation(location); |
|
1100 |
dir = null; |
|
1101 |
for (File f: path) { |
|
1102 |
dir = f; |
|
1103 |
break; |
|
1104 |
} |
|
1105 |
} |
|
1106 |
||
1107 |
File file = (dir == null ? new File(fileName) : new File(dir, fileName)); |
|
1108 |
return new RegularFileObject(file); |
|
1109 |
||
1110 |
} |
|
1111 |
||
1112 |
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles( |
|
1113 |
Iterable<? extends File> files) |
|
1114 |
{ |
|
1115 |
ArrayList<RegularFileObject> result; |
|
1116 |
if (files instanceof Collection) |
|
1117 |
result = new ArrayList<RegularFileObject>(((Collection)files).size()); |
|
1118 |
else |
|
1119 |
result = new ArrayList<RegularFileObject>(); |
|
1120 |
for (File f: files) |
|
1121 |
result.add(new RegularFileObject(nullCheck(f))); |
|
1122 |
return result; |
|
1123 |
} |
|
1124 |
||
1125 |
public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) { |
|
1126 |
return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files))); |
|
1127 |
} |
|
1128 |
||
1129 |
public void setLocation(Location location, |
|
1130 |
Iterable<? extends File> path) |
|
1131 |
throws IOException |
|
1132 |
{ |
|
1133 |
nullCheck(location); |
|
1134 |
paths.lazy(); |
|
1135 |
||
1136 |
final File dir = location.isOutputLocation() ? getOutputDirectory(path) : null; |
|
1137 |
||
1138 |
if (location == CLASS_OUTPUT) |
|
1139 |
classOutDir = getOutputLocation(dir, D); |
|
1140 |
else if (location == SOURCE_OUTPUT) |
|
1141 |
sourceOutDir = getOutputLocation(dir, S); |
|
1142 |
else |
|
1143 |
paths.setPathForLocation(location, path); |
|
1144 |
} |
|
1145 |
// where |
|
1146 |
private File getOutputDirectory(Iterable<? extends File> path) throws IOException { |
|
1147 |
if (path == null) |
|
1148 |
return null; |
|
1149 |
Iterator<? extends File> pathIter = path.iterator(); |
|
1150 |
if (!pathIter.hasNext()) |
|
1151 |
throw new IllegalArgumentException("empty path for directory"); |
|
1152 |
File dir = pathIter.next(); |
|
1153 |
if (pathIter.hasNext()) |
|
1154 |
throw new IllegalArgumentException("path too long for directory"); |
|
1155 |
if (!dir.exists()) |
|
1156 |
throw new FileNotFoundException(dir + ": does not exist"); |
|
1157 |
else if (!dir.isDirectory()) |
|
1158 |
throw new IOException(dir + ": not a directory"); |
|
1159 |
return dir; |
|
1160 |
} |
|
1161 |
||
1162 |
private File getOutputLocation(File dir, OptionName defaultOptionName) { |
|
1163 |
if (dir != null) |
|
1164 |
return dir; |
|
1165 |
String arg = options.get(defaultOptionName); |
|
1166 |
if (arg == null) |
|
1167 |
return null; |
|
1168 |
return new File(arg); |
|
1169 |
} |
|
1170 |
||
1171 |
public Iterable<? extends File> getLocation(Location location) { |
|
1172 |
nullCheck(location); |
|
1173 |
paths.lazy(); |
|
1174 |
if (location == CLASS_OUTPUT) { |
|
1175 |
return (getClassOutDir() == null ? null : List.of(getClassOutDir())); |
|
1176 |
} else if (location == SOURCE_OUTPUT) { |
|
1177 |
return (getSourceOutDir() == null ? null : List.of(getSourceOutDir())); |
|
1178 |
} else |
|
1179 |
return paths.getPathForLocation(location); |
|
1180 |
} |
|
1181 |
||
1182 |
private File getClassOutDir() { |
|
1183 |
if (classOutDir == uninited) |
|
1184 |
classOutDir = getOutputLocation(null, D); |
|
1185 |
return classOutDir; |
|
1186 |
} |
|
1187 |
||
1188 |
private File getSourceOutDir() { |
|
1189 |
if (sourceOutDir == uninited) |
|
1190 |
sourceOutDir = getOutputLocation(null, S); |
|
1191 |
return sourceOutDir; |
|
1192 |
} |
|
1193 |
||
1194 |
/** |
|
1195 |
* Enforces the specification of a "relative" URI as used in |
|
1196 |
* {@linkplain #getFileForInput(Location,String,URI) |
|
1197 |
* getFileForInput}. This method must follow the rules defined in |
|
1198 |
* that method, do not make any changes without consulting the |
|
1199 |
* specification. |
|
1200 |
*/ |
|
1201 |
protected static boolean isRelativeUri(URI uri) { |
|
1202 |
if (uri.isAbsolute()) |
|
1203 |
return false; |
|
1204 |
String path = uri.normalize().getPath(); |
|
1205 |
if (path.length() == 0 /* isEmpty() is mustang API */) |
|
1206 |
return false; |
|
1207 |
char first = path.charAt(0); |
|
1208 |
return first != '.' && first != '/'; |
|
1209 |
} |
|
1210 |
||
1211 |
/** |
|
1212 |
* Converts a relative file name to a relative URI. This is |
|
1213 |
* different from File.toURI as this method does not canonicalize |
|
1214 |
* the file before creating the URI. Furthermore, no schema is |
|
1215 |
* used. |
|
1216 |
* @param file a relative file name |
|
1217 |
* @return a relative URI |
|
1218 |
* @throws IllegalArgumentException if the file name is not |
|
1219 |
* relative according to the definition given in {@link |
|
1220 |
* javax.tools.JavaFileManager#getFileForInput} |
|
1221 |
*/ |
|
1222 |
public static String getRelativeName(File file) { |
|
1223 |
if (!file.isAbsolute()) { |
|
1224 |
String result = file.getPath().replace(File.separatorChar, '/'); |
|
1225 |
if (JavacFileManager.isRelativeUri(URI.create(result))) // FIXME 6419701 |
|
1226 |
return result; |
|
1227 |
} |
|
1228 |
throw new IllegalArgumentException("Invalid relative path: " + file); |
|
1229 |
} |
|
1230 |
||
1231 |
@SuppressWarnings("deprecation") // bug 6410637 |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
1232 |
public static String getJavacFileName(FileObject file) { |
10 | 1233 |
if (file instanceof BaseFileObject) |
1234 |
return ((BaseFileObject)file).getPath(); |
|
1235 |
URI uri = file.toUri(); |
|
1236 |
String scheme = uri.getScheme(); |
|
1237 |
if (scheme == null || scheme.equals("file") || scheme.equals("jar")) |
|
1238 |
return uri.getPath(); |
|
1239 |
else |
|
1240 |
return uri.toString(); |
|
1241 |
} |
|
1242 |
||
1243 |
@SuppressWarnings("deprecation") // bug 6410637 |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
657
diff
changeset
|
1244 |
public static String getJavacBaseFileName(FileObject file) { |
10 | 1245 |
if (file instanceof BaseFileObject) |
1246 |
return ((BaseFileObject)file).getName(); |
|
1247 |
URI uri = file.toUri(); |
|
1248 |
String scheme = uri.getScheme(); |
|
1249 |
if (scheme == null || scheme.equals("file") || scheme.equals("jar")) { |
|
1250 |
String path = uri.getPath(); |
|
1251 |
if (path == null) |
|
1252 |
return null; |
|
1253 |
if (scheme != null && scheme.equals("jar")) |
|
1254 |
path = path.substring(path.lastIndexOf('!') + 1); |
|
1255 |
return path.substring(path.lastIndexOf('/') + 1); |
|
1256 |
} else { |
|
1257 |
return uri.toString(); |
|
1258 |
} |
|
1259 |
} |
|
1260 |
||
1261 |
private static <T> T nullCheck(T o) { |
|
1262 |
o.getClass(); // null check |
|
1263 |
return o; |
|
1264 |
} |
|
1265 |
||
1266 |
private static <T> Iterable<T> nullCheck(Iterable<T> it) { |
|
1267 |
for (T t : it) |
|
1268 |
t.getClass(); // null check |
|
1269 |
return it; |
|
1270 |
} |
|
1271 |
||
1272 |
/** |
|
1273 |
* A subclass of JavaFileObject representing regular files. |
|
1274 |
*/ |
|
1275 |
private class RegularFileObject extends BaseFileObject { |
|
1276 |
/** Have the parent directories been created? |
|
1277 |
*/ |
|
1278 |
private boolean hasParents=false; |
|
1279 |
||
1280 |
/** The file's name. |
|
1281 |
*/ |
|
1282 |
private String name; |
|
1283 |
||
1284 |
/** The underlying file. |
|
1285 |
*/ |
|
1286 |
final File f; |
|
1287 |
||
1288 |
public RegularFileObject(File f) { |
|
1289 |
this(f.getName(), f); |
|
1290 |
} |
|
1291 |
||
1292 |
public RegularFileObject(String name, File f) { |
|
1293 |
if (f.isDirectory()) |
|
1294 |
throw new IllegalArgumentException("directories not supported"); |
|
1295 |
this.name = name; |
|
1296 |
this.f = f; |
|
1297 |
} |
|
1298 |
||
1299 |
public InputStream openInputStream() throws IOException { |
|
1300 |
return new FileInputStream(f); |
|
1301 |
} |
|
1302 |
||
1303 |
protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { |
|
1304 |
return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors); |
|
1305 |
} |
|
1306 |
||
1307 |
public OutputStream openOutputStream() throws IOException { |
|
1308 |
ensureParentDirectoriesExist(); |
|
1309 |
return new FileOutputStream(f); |
|
1310 |
} |
|
1311 |
||
1312 |
public Writer openWriter() throws IOException { |
|
1313 |
ensureParentDirectoriesExist(); |
|
1314 |
return new OutputStreamWriter(new FileOutputStream(f), getEncodingName()); |
|
1315 |
} |
|
1316 |
||
1317 |
private void ensureParentDirectoriesExist() throws IOException { |
|
1318 |
if (!hasParents) { |
|
1319 |
File parent = f.getParentFile(); |
|
1320 |
if (parent != null && !parent.exists()) { |
|
1321 |
if (!parent.mkdirs()) { |
|
1322 |
// if the mkdirs failed, it may be because another process concurrently |
|
1323 |
// created the directory, so check if the directory got created |
|
1324 |
// anyway before throwing an exception |
|
1325 |
if (!parent.exists() || !parent.isDirectory()) |
|
1326 |
throw new IOException("could not create parent directories"); |
|
1327 |
} |
|
1328 |
} |
|
1329 |
hasParents = true; |
|
1330 |
} |
|
1331 |
} |
|
1332 |
||
1333 |
/** @deprecated see bug 6410637 */ |
|
1334 |
@Deprecated |
|
1335 |
public String getName() { |
|
1336 |
return name; |
|
1337 |
} |
|
1338 |
||
1339 |
public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) { |
|
1340 |
cn.getClass(); // null check |
|
1341 |
if (kind == Kind.OTHER && getKind() != kind) |
|
1342 |
return false; |
|
1343 |
String n = cn + kind.extension; |
|
1344 |
if (name.equals(n)) |
|
1345 |
return true; |
|
1346 |
if (name.equalsIgnoreCase(n)) { |
|
1347 |
try { |
|
1348 |
// allow for Windows |
|
1349 |
return (f.getCanonicalFile().getName().equals(n)); |
|
1350 |
} catch (IOException e) { |
|
1351 |
} |
|
1352 |
} |
|
1353 |
return false; |
|
1354 |
} |
|
1355 |
||
1356 |
/** @deprecated see bug 6410637 */ |
|
1357 |
@Deprecated |
|
1358 |
public String getPath() { |
|
1359 |
return f.getPath(); |
|
1360 |
} |
|
1361 |
||
1362 |
public long getLastModified() { |
|
1363 |
return f.lastModified(); |
|
1364 |
} |
|
1365 |
||
1366 |
public boolean delete() { |
|
1367 |
return f.delete(); |
|
1368 |
} |
|
1369 |
||
1370 |
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { |
|
1371 |
SoftReference<CharBuffer> r = contentCache.get(this); |
|
1372 |
CharBuffer cb = (r == null ? null : r.get()); |
|
1373 |
if (cb == null) { |
|
1374 |
InputStream in = new FileInputStream(f); |
|
1375 |
try { |
|
1376 |
ByteBuffer bb = makeByteBuffer(in); |
|
1377 |
JavaFileObject prev = log.useSource(this); |
|
1378 |
try { |
|
1379 |
cb = decode(bb, ignoreEncodingErrors); |
|
1380 |
} finally { |
|
1381 |
log.useSource(prev); |
|
1382 |
} |
|
1383 |
byteBufferCache.put(bb); // save for next time |
|
1384 |
if (!ignoreEncodingErrors) |
|
1385 |
contentCache.put(this, new SoftReference<CharBuffer>(cb)); |
|
1386 |
} finally { |
|
1387 |
in.close(); |
|
1388 |
} |
|
1389 |
} |
|
1390 |
return cb; |
|
1391 |
} |
|
1392 |
||
1393 |
@Override |
|
1394 |
public boolean equals(Object other) { |
|
1395 |
if (!(other instanceof RegularFileObject)) |
|
1396 |
return false; |
|
1397 |
RegularFileObject o = (RegularFileObject) other; |
|
1398 |
try { |
|
1399 |
return f.equals(o.f) |
|
1400 |
|| f.getCanonicalFile().equals(o.f.getCanonicalFile()); |
|
1401 |
} catch (IOException e) { |
|
1402 |
return false; |
|
1403 |
} |
|
1404 |
} |
|
1405 |
||
1406 |
@Override |
|
1407 |
public int hashCode() { |
|
1408 |
return f.hashCode(); |
|
1409 |
} |
|
1410 |
||
1411 |
public URI toUri() { |
|
1412 |
try { |
|
1413 |
// Do no use File.toURI to avoid file system access |
|
1414 |
String path = f.getAbsolutePath().replace(File.separatorChar, '/'); |
|
1415 |
return new URI("file://" + path).normalize(); |
|
1416 |
} catch (URISyntaxException ex) { |
|
1417 |
return f.toURI(); |
|
1418 |
} |
|
1419 |
} |
|
1420 |
||
1421 |
} |
|
1422 |
||
1423 |
/** |
|
1424 |
* A subclass of JavaFileObject representing zip entries. |
|
1425 |
*/ |
|
1426 |
public class ZipFileObject extends BaseFileObject { |
|
1427 |
||
1428 |
/** The entry's name. |
|
1429 |
*/ |
|
1430 |
private String name; |
|
1431 |
||
1432 |
/** The zipfile containing the entry. |
|
1433 |
*/ |
|
1434 |
ZipFile zdir; |
|
1435 |
||
1436 |
/** The underlying zip entry object. |
|
1437 |
*/ |
|
1438 |
ZipEntry entry; |
|
1439 |
||
1440 |
public ZipFileObject(String name, ZipFile zdir, ZipEntry entry) { |
|
1441 |
this.name = name; |
|
1442 |
this.zdir = zdir; |
|
1443 |
this.entry = entry; |
|
1444 |
} |
|
1445 |
||
1446 |
public InputStream openInputStream() throws IOException { |
|
1447 |
return zdir.getInputStream(entry); |
|
1448 |
} |
|
1449 |
||
1450 |
public OutputStream openOutputStream() throws IOException { |
|
1451 |
throw new UnsupportedOperationException(); |
|
1452 |
} |
|
1453 |
||
1454 |
protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { |
|
1455 |
return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors); |
|
1456 |
} |
|
1457 |
||
1458 |
public Writer openWriter() throws IOException { |
|
1459 |
throw new UnsupportedOperationException(); |
|
1460 |
} |
|
1461 |
||
1462 |
/** @deprecated see bug 6410637 */ |
|
1463 |
@Deprecated |
|
1464 |
public String getName() { |
|
1465 |
return name; |
|
1466 |
} |
|
1467 |
||
1468 |
public boolean isNameCompatible(String cn, JavaFileObject.Kind k) { |
|
1469 |
cn.getClass(); // null check |
|
1470 |
if (k == Kind.OTHER && getKind() != k) |
|
1471 |
return false; |
|
1472 |
return name.equals(cn + k.extension); |
|
1473 |
} |
|
1474 |
||
1475 |
/** @deprecated see bug 6410637 */ |
|
1476 |
@Deprecated |
|
1477 |
public String getPath() { |
|
1478 |
return zdir.getName() + "(" + entry + ")"; |
|
1479 |
} |
|
1480 |
||
1481 |
public long getLastModified() { |
|
1482 |
return entry.getTime(); |
|
1483 |
} |
|
1484 |
||
1485 |
public boolean delete() { |
|
1486 |
throw new UnsupportedOperationException(); |
|
1487 |
} |
|
1488 |
||
1489 |
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { |
|
1490 |
SoftReference<CharBuffer> r = contentCache.get(this); |
|
1491 |
CharBuffer cb = (r == null ? null : r.get()); |
|
1492 |
if (cb == null) { |
|
1493 |
InputStream in = zdir.getInputStream(entry); |
|
1494 |
try { |
|
1495 |
ByteBuffer bb = makeByteBuffer(in); |
|
1496 |
JavaFileObject prev = log.useSource(this); |
|
1497 |
try { |
|
1498 |
cb = decode(bb, ignoreEncodingErrors); |
|
1499 |
} finally { |
|
1500 |
log.useSource(prev); |
|
1501 |
} |
|
1502 |
byteBufferCache.put(bb); // save for next time |
|
1503 |
if (!ignoreEncodingErrors) |
|
1504 |
contentCache.put(this, new SoftReference<CharBuffer>(cb)); |
|
1505 |
} finally { |
|
1506 |
in.close(); |
|
1507 |
} |
|
1508 |
} |
|
1509 |
return cb; |
|
1510 |
} |
|
1511 |
||
1512 |
@Override |
|
1513 |
public boolean equals(Object other) { |
|
1514 |
if (!(other instanceof ZipFileObject)) |
|
1515 |
return false; |
|
1516 |
ZipFileObject o = (ZipFileObject) other; |
|
1517 |
return zdir.equals(o.zdir) || name.equals(o.name); |
|
1518 |
} |
|
1519 |
||
1520 |
@Override |
|
1521 |
public int hashCode() { |
|
1522 |
return zdir.hashCode() + name.hashCode(); |
|
1523 |
} |
|
1524 |
||
1525 |
public String getZipName() { |
|
1526 |
return zdir.getName(); |
|
1527 |
} |
|
1528 |
||
1529 |
public String getZipEntryName() { |
|
1530 |
return entry.getName(); |
|
1531 |
} |
|
1532 |
||
1533 |
public URI toUri() { |
|
1534 |
String zipName = new File(getZipName()).toURI().normalize().getPath(); |
|
1535 |
String entryName = getZipEntryName(); |
|
1536 |
return URI.create("jar:" + zipName + "!" + entryName); |
|
1537 |
} |
|
1538 |
||
1539 |
} |
|
1540 |
||
1541 |
/** |
|
1542 |
* A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.zip.ZipFileIndex implementation. |
|
1543 |
*/ |
|
1544 |
public class ZipFileIndexFileObject extends BaseFileObject { |
|
1545 |
||
1546 |
/** The entry's name. |
|
1547 |
*/ |
|
1548 |
private String name; |
|
1549 |
||
1550 |
/** The zipfile containing the entry. |
|
1551 |
*/ |
|
1552 |
ZipFileIndex zfIndex; |
|
1553 |
||
1554 |
/** The underlying zip entry object. |
|
1555 |
*/ |
|
1556 |
ZipFileIndexEntry entry; |
|
1557 |
||
1558 |
/** The InputStream for this zip entry (file.) |
|
1559 |
*/ |
|
1560 |
InputStream inputStream = null; |
|
1561 |
||
1562 |
/** The name of the zip file where this entry resides. |
|
1563 |
*/ |
|
1564 |
String zipName; |
|
1565 |
||
1566 |
JavacFileManager defFileManager = null; |
|
1567 |
||
1568 |
public ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndexEntry entry, String zipFileName) { |
|
1569 |
super(); |
|
1570 |
this.name = entry.getFileName(); |
|
1571 |
this.zfIndex = zfIndex; |
|
1572 |
this.entry = entry; |
|
1573 |
this.zipName = zipFileName; |
|
1574 |
defFileManager = fileManager; |
|
1575 |
} |
|
1576 |
||
1577 |
public InputStream openInputStream() throws IOException { |
|
1578 |
||
1579 |
if (inputStream == null) { |
|
1580 |
inputStream = new ByteArrayInputStream(read()); |
|
1581 |
} |
|
1582 |
return inputStream; |
|
1583 |
} |
|
1584 |
||
1585 |
protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { |
|
1586 |
return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors); |
|
1587 |
} |
|
1588 |
||
1589 |
public OutputStream openOutputStream() throws IOException { |
|
1590 |
throw new UnsupportedOperationException(); |
|
1591 |
} |
|
1592 |
||
1593 |
public Writer openWriter() throws IOException { |
|
1594 |
throw new UnsupportedOperationException(); |
|
1595 |
} |
|
1596 |
||
1597 |
/** @deprecated see bug 6410637 */ |
|
1598 |
@Deprecated |
|
1599 |
public String getName() { |
|
1600 |
return name; |
|
1601 |
} |
|
1602 |
||
1603 |
public boolean isNameCompatible(String cn, JavaFileObject.Kind k) { |
|
1604 |
cn.getClass(); // null check |
|
1605 |
if (k == Kind.OTHER && getKind() != k) |
|
1606 |
return false; |
|
1607 |
return name.equals(cn + k.extension); |
|
1608 |
} |
|
1609 |
||
1610 |
/** @deprecated see bug 6410637 */ |
|
1611 |
@Deprecated |
|
1612 |
public String getPath() { |
|
657
99cc2b9325f2
6705935: javac reports path name of entry in ZipFileIndex incorectly
jjg
parents:
10
diff
changeset
|
1613 |
return zipName + "(" + entry.getName() + ")"; |
10 | 1614 |
} |
1615 |
||
1616 |
public long getLastModified() { |
|
1617 |
return entry.getLastModified(); |
|
1618 |
} |
|
1619 |
||
1620 |
public boolean delete() { |
|
1621 |
throw new UnsupportedOperationException(); |
|
1622 |
} |
|
1623 |
||
1624 |
@Override |
|
1625 |
public boolean equals(Object other) { |
|
1626 |
if (!(other instanceof ZipFileIndexFileObject)) |
|
1627 |
return false; |
|
1628 |
ZipFileIndexFileObject o = (ZipFileIndexFileObject) other; |
|
1629 |
return entry.equals(o.entry); |
|
1630 |
} |
|
1631 |
||
1632 |
@Override |
|
1633 |
public int hashCode() { |
|
1634 |
return zipName.hashCode() + (name.hashCode() << 10); |
|
1635 |
} |
|
1636 |
||
1637 |
public String getZipName() { |
|
1638 |
return zipName; |
|
1639 |
} |
|
1640 |
||
1641 |
public String getZipEntryName() { |
|
1642 |
return entry.getName(); |
|
1643 |
} |
|
1644 |
||
1645 |
public URI toUri() { |
|
1646 |
String zipName = new File(getZipName()).toURI().normalize().getPath(); |
|
1647 |
String entryName = getZipEntryName(); |
|
1648 |
if (File.separatorChar != '/') { |
|
1649 |
entryName = entryName.replace(File.separatorChar, '/'); |
|
1650 |
} |
|
1651 |
return URI.create("jar:" + zipName + "!" + entryName); |
|
1652 |
} |
|
1653 |
||
1654 |
private byte[] read() throws IOException { |
|
1655 |
if (entry == null) { |
|
1656 |
entry = zfIndex.getZipIndexEntry(name); |
|
1657 |
if (entry == null) |
|
1658 |
throw new FileNotFoundException(); |
|
1659 |
} |
|
1660 |
return zfIndex.read(entry); |
|
1661 |
} |
|
1662 |
||
1663 |
public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { |
|
1664 |
SoftReference<CharBuffer> r = defFileManager.contentCache.get(this); |
|
1665 |
CharBuffer cb = (r == null ? null : r.get()); |
|
1666 |
if (cb == null) { |
|
1667 |
InputStream in = new ByteArrayInputStream(zfIndex.read(entry)); |
|
1668 |
try { |
|
1669 |
ByteBuffer bb = makeByteBuffer(in); |
|
1670 |
JavaFileObject prev = log.useSource(this); |
|
1671 |
try { |
|
1672 |
cb = decode(bb, ignoreEncodingErrors); |
|
1673 |
} finally { |
|
1674 |
log.useSource(prev); |
|
1675 |
} |
|
1676 |
byteBufferCache.put(bb); // save for next time |
|
1677 |
if (!ignoreEncodingErrors) |
|
1678 |
defFileManager.contentCache.put(this, new SoftReference<CharBuffer>(cb)); |
|
1679 |
} finally { |
|
1680 |
in.close(); |
|
1681 |
} |
|
1682 |
} |
|
1683 |
return cb; |
|
1684 |
} |
|
1685 |
} |
|
1686 |
||
1687 |
public class ZipFileIndexArchive implements Archive { |
|
1688 |
private final ZipFileIndex zfIndex; |
|
1689 |
private JavacFileManager fileManager; |
|
1690 |
||
1691 |
public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException { |
|
1692 |
this.fileManager = fileManager; |
|
1693 |
this.zfIndex = zdir; |
|
1694 |
} |
|
1695 |
||
1696 |
public boolean contains(String name) { |
|
1697 |
return zfIndex.contains(name); |
|
1698 |
} |
|
1699 |
||
1700 |
public com.sun.tools.javac.util.List<String> getFiles(String subdirectory) { |
|
1701 |
return zfIndex.getFiles(((subdirectory.endsWith("/") || subdirectory.endsWith("\\"))? subdirectory.substring(0, subdirectory.length() - 1) : subdirectory)); |
|
1702 |
} |
|
1703 |
||
1704 |
public JavaFileObject getFileObject(String subdirectory, String file) { |
|
1705 |
String fullZipFileName = subdirectory + file; |
|
1706 |
ZipFileIndexEntry entry = zfIndex.getZipIndexEntry(fullZipFileName); |
|
1707 |
JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath()); |
|
1708 |
return ret; |
|
1709 |
} |
|
1710 |
||
1711 |
public Set<String> getSubdirectories() { |
|
1712 |
return zfIndex.getAllDirectories(); |
|
1713 |
} |
|
1714 |
||
1715 |
public void close() throws IOException { |
|
1716 |
zfIndex.close(); |
|
1717 |
} |
|
1718 |
} |
|
1719 |
} |