37 import java.nio.file.FileSystems; |
37 import java.nio.file.FileSystems; |
38 import java.nio.file.FileVisitOption; |
38 import java.nio.file.FileVisitOption; |
39 import java.nio.file.FileVisitResult; |
39 import java.nio.file.FileVisitResult; |
40 import java.nio.file.Path; |
40 import java.nio.file.Path; |
41 import java.nio.file.SimpleFileVisitor; |
41 import java.nio.file.SimpleFileVisitor; |
42 import java.nio.file.attribute.Attributes; |
|
43 import java.nio.file.attribute.BasicFileAttributes; |
42 import java.nio.file.attribute.BasicFileAttributes; |
44 import java.util.ArrayList; |
43 import java.util.ArrayList; |
45 import java.util.Arrays; |
44 import java.util.Arrays; |
46 import java.util.Collection; |
45 import java.util.Collection; |
47 import java.util.Collections; |
46 import java.util.Collections; |
221 if (!pathIter.hasNext()) |
220 if (!pathIter.hasNext()) |
222 throw new IllegalArgumentException("empty path for directory"); |
221 throw new IllegalArgumentException("empty path for directory"); |
223 Path path = pathIter.next(); |
222 Path path = pathIter.next(); |
224 if (pathIter.hasNext()) |
223 if (pathIter.hasNext()) |
225 throw new IllegalArgumentException("path too long for directory"); |
224 throw new IllegalArgumentException("path too long for directory"); |
226 if (!path.exists()) |
225 if (!isDirectory(path)) |
227 throw new FileNotFoundException(path + ": does not exist"); |
|
228 else if (!isDirectory(path)) |
|
229 throw new IOException(path + ": not a directory"); |
226 throw new IOException(path + ": not a directory"); |
230 } |
227 } |
231 |
228 |
232 private void setDefaultForLocation(Location locn) { |
229 private void setDefaultForLocation(Location locn) { |
233 Collection<File> files = null; |
230 Collection<File> files = null; |
324 } |
321 } |
325 |
322 |
326 private void list(Path path, String packageName, final Set<Kind> kinds, |
323 private void list(Path path, String packageName, final Set<Kind> kinds, |
327 boolean recurse, final ListBuffer<JavaFileObject> results) |
324 boolean recurse, final ListBuffer<JavaFileObject> results) |
328 throws IOException { |
325 throws IOException { |
329 if (!path.exists()) |
326 if (!Files.exists(path)) |
330 return; |
327 return; |
331 |
328 |
332 final Path pathDir; |
329 final Path pathDir; |
333 if (isDirectory(path)) |
330 if (isDirectory(path)) |
334 pathDir = path; |
331 pathDir = path; |
339 pathDir = fs.getRootDirectories().iterator().next(); |
336 pathDir = fs.getRootDirectories().iterator().next(); |
340 } |
337 } |
341 String sep = path.getFileSystem().getSeparator(); |
338 String sep = path.getFileSystem().getSeparator(); |
342 Path packageDir = packageName.isEmpty() ? pathDir |
339 Path packageDir = packageName.isEmpty() ? pathDir |
343 : pathDir.resolve(packageName.replace(".", sep)); |
340 : pathDir.resolve(packageName.replace(".", sep)); |
344 if (!packageDir.exists()) |
341 if (!Files.exists(packageDir)) |
345 return; |
342 return; |
346 |
343 |
347 /* Alternate impl of list, superceded by use of Files.walkFileTree */ |
344 /* Alternate impl of list, superceded by use of Files.walkFileTree */ |
348 // Deque<Path> queue = new LinkedList<Path>(); |
345 // Deque<Path> queue = new LinkedList<Path>(); |
349 // queue.add(packageDir); |
346 // queue.add(packageDir); |
351 // Path dir; |
348 // Path dir; |
352 // while ((dir = queue.poll()) != null) { |
349 // while ((dir = queue.poll()) != null) { |
353 // DirectoryStream<Path> ds = dir.newDirectoryStream(); |
350 // DirectoryStream<Path> ds = dir.newDirectoryStream(); |
354 // try { |
351 // try { |
355 // for (Path p: ds) { |
352 // for (Path p: ds) { |
356 // String name = p.getName().toString(); |
353 // String name = p.getFileName().toString(); |
357 // if (isDirectory(p)) { |
354 // if (isDirectory(p)) { |
358 // if (recurse && SourceVersion.isIdentifier(name)) { |
355 // if (recurse && SourceVersion.isIdentifier(name)) { |
359 // queue.add(p); |
356 // queue.add(p); |
360 // } |
357 // } |
361 // } else { |
358 // } else { |
374 Set<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS); |
371 Set<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS); |
375 Files.walkFileTree(packageDir, opts, maxDepth, |
372 Files.walkFileTree(packageDir, opts, maxDepth, |
376 new SimpleFileVisitor<Path>() { |
373 new SimpleFileVisitor<Path>() { |
377 @Override |
374 @Override |
378 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { |
375 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { |
379 Path name = dir.getName(); |
376 Path name = dir.getFileName(); |
380 if (name == null || SourceVersion.isIdentifier(name.toString())) // JSR 292? |
377 if (name == null || SourceVersion.isIdentifier(name.toString())) // JSR 292? |
381 return FileVisitResult.CONTINUE; |
378 return FileVisitResult.CONTINUE; |
382 else |
379 else |
383 return FileVisitResult.SKIP_SUBTREE; |
380 return FileVisitResult.SKIP_SUBTREE; |
384 } |
381 } |
385 |
382 |
386 @Override |
383 @Override |
387 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
384 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
388 if (attrs.isRegularFile() && kinds.contains(getKind(file.getName().toString()))) { |
385 if (attrs.isRegularFile() && kinds.contains(getKind(file.getFileName().toString()))) { |
389 JavaFileObject fe = |
386 JavaFileObject fe = |
390 PathFileObject.createDirectoryPathFileObject( |
387 PathFileObject.createDirectoryPathFileObject( |
391 JavacPathFileManager.this, file, pathDir); |
388 JavacPathFileManager.this, file, pathDir); |
392 results.append(fe); |
389 results.append(fe); |
393 } |
390 } |
429 private JavaFileObject getFileForInput(Location location, String relativePath) |
426 private JavaFileObject getFileForInput(Location location, String relativePath) |
430 throws IOException { |
427 throws IOException { |
431 for (Path p: getLocation(location)) { |
428 for (Path p: getLocation(location)) { |
432 if (isDirectory(p)) { |
429 if (isDirectory(p)) { |
433 Path f = resolve(p, relativePath); |
430 Path f = resolve(p, relativePath); |
434 if (f.exists()) |
431 if (Files.exists(f)) |
435 return PathFileObject.createDirectoryPathFileObject(this, f, p); |
432 return PathFileObject.createDirectoryPathFileObject(this, f, p); |
436 } else { |
433 } else { |
437 FileSystem fs = getFileSystem(p); |
434 FileSystem fs = getFileSystem(p); |
438 if (fs != null) { |
435 if (fs != null) { |
439 Path file = getPath(fs, relativePath); |
436 Path file = getPath(fs, relativePath); |
440 if (file.exists()) |
437 if (Files.exists(file)) |
441 return PathFileObject.createJarPathFileObject(this, file); |
438 return PathFileObject.createJarPathFileObject(this, file); |
442 } |
439 } |
443 } |
440 } |
444 } |
441 } |
445 return null; |
442 return null; |
502 } |
499 } |
503 |
500 |
504 private FileSystem getFileSystem(Path p) throws IOException { |
501 private FileSystem getFileSystem(Path p) throws IOException { |
505 FileSystem fs = fileSystems.get(p); |
502 FileSystem fs = fileSystems.get(p); |
506 if (fs == null) { |
503 if (fs == null) { |
507 fs = FileSystems.newFileSystem(p, Collections.<String,Void>emptyMap(), null); |
504 fs = FileSystems.newFileSystem(p, null); |
508 fileSystems.put(p, fs); |
505 fileSystems.put(p, fs); |
509 } |
506 } |
510 return fs; |
507 return fs; |
511 } |
508 } |
512 |
509 |
528 int lastSep = relativePath.lastIndexOf("/"); |
525 int lastSep = relativePath.lastIndexOf("/"); |
529 return relativePath.substring(lastSep + 1); // safe if "/" not found |
526 return relativePath.substring(lastSep + 1); // safe if "/" not found |
530 } |
527 } |
531 |
528 |
532 private static boolean isDirectory(Path path) throws IOException { |
529 private static boolean isDirectory(Path path) throws IOException { |
533 BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path); |
530 BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class); |
534 return attrs.isDirectory(); |
531 return attrs.isDirectory(); |
535 } |
532 } |
536 |
533 |
537 private static Path getPath(FileSystem fs, String relativePath) { |
534 private static Path getPath(FileSystem fs, String relativePath) { |
538 return fs.getPath(relativePath.replace("/", fs.getSeparator())); |
535 return fs.getPath(relativePath.replace("/", fs.getSeparator())); |