src/share/classes/com/sun/tools/javac/nio/JavacPathFileManager.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 757
c44234f680da
child 809
e63b1f8341ce
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

jjg@450 1 /*
ohair@798 2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
jjg@450 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@450 4 *
jjg@450 5 * This code is free software; you can redistribute it and/or modify it
jjg@450 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
jjg@450 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@450 10 *
jjg@450 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@450 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@450 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@450 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@450 15 * accompanied this code).
jjg@450 16 *
jjg@450 17 * You should have received a copy of the GNU General Public License version
jjg@450 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@450 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@450 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
jjg@450 24 */
jjg@450 25
jjg@450 26 package com.sun.tools.javac.nio;
jjg@450 27
jjg@450 28
jjg@450 29 import java.io.File;
jjg@450 30 import java.io.FileNotFoundException;
jjg@450 31 import java.io.IOException;
jjg@450 32 import java.net.MalformedURLException;
jjg@450 33 import java.net.URL;
jjg@450 34 import java.nio.charset.Charset;
jjg@450 35 import java.nio.file.Files;
jjg@450 36 import java.nio.file.FileSystem;
jjg@450 37 import java.nio.file.FileSystems;
jjg@450 38 import java.nio.file.FileVisitOption;
jjg@450 39 import java.nio.file.FileVisitResult;
jjg@450 40 import java.nio.file.Path;
jjg@450 41 import java.nio.file.SimpleFileVisitor;
jjg@450 42 import java.nio.file.attribute.Attributes;
jjg@450 43 import java.nio.file.attribute.BasicFileAttributes;
jjg@450 44 import java.util.ArrayList;
jjg@450 45 import java.util.Arrays;
jjg@450 46 import java.util.Collection;
jjg@450 47 import java.util.Collections;
jjg@450 48 import java.util.EnumSet;
jjg@450 49 import java.util.HashMap;
jjg@450 50 import java.util.Iterator;
jjg@450 51 import java.util.LinkedHashSet;
jjg@450 52 import java.util.Map;
jjg@450 53 import java.util.Set;
jjg@450 54 import javax.lang.model.SourceVersion;
jjg@450 55 import javax.tools.FileObject;
jjg@450 56 import javax.tools.JavaFileManager;
jjg@450 57 import javax.tools.JavaFileObject;
jjg@450 58 import javax.tools.JavaFileObject.Kind;
jjg@450 59 import javax.tools.StandardLocation;
jjg@450 60
jjg@450 61 import static java.nio.file.FileVisitOption.*;
jjg@450 62 import static javax.tools.StandardLocation.*;
jjg@450 63
jjg@450 64 import com.sun.tools.javac.file.Paths;
jjg@450 65 import com.sun.tools.javac.util.BaseFileManager;
jjg@450 66 import com.sun.tools.javac.util.Context;
jjg@450 67 import com.sun.tools.javac.util.List;
jjg@450 68 import com.sun.tools.javac.util.ListBuffer;
jjg@450 69
jjg@450 70 import static com.sun.tools.javac.main.OptionName.*;
jjg@450 71
jjg@450 72
jjg@450 73 // NOTE the imports carefully for this compilation unit.
jjg@450 74 //
jjg@450 75 // Path: java.nio.file.Path -- the new NIO type for which this file manager exists
jjg@450 76 //
jjg@450 77 // Paths: com.sun.tools.javac.file.Paths -- legacy javac type for handling path options
jjg@450 78 // The other Paths (java.nio.file.Paths) is not used
jjg@450 79
jjg@450 80 // NOTE this and related classes depend on new API in JDK 7.
jjg@450 81 // This requires special handling while bootstrapping the JDK build,
jjg@450 82 // when these classes might not yet have been compiled. To workaround
jjg@450 83 // this, the build arranges to make stubs of these classes available
jjg@450 84 // when compiling this and related classes. The set of stub files
jjg@450 85 // is specified in make/build.properties.
jjg@450 86
jjg@450 87 /**
jjg@450 88 * Implementation of PathFileManager: a JavaFileManager based on the use
jjg@450 89 * of java.nio.file.Path.
jjg@450 90 *
jjg@450 91 * <p>Just as a Path is somewhat analagous to a File, so too is this
jjg@450 92 * JavacPathFileManager analogous to JavacFileManager, as it relates to the
jjg@450 93 * support of FileObjects based on File objects (i.e. just RegularFileObject,
jjg@450 94 * not ZipFileObject and its variants.)
jjg@450 95 *
jjg@450 96 * <p>The default values for the standard locations supported by this file
jjg@450 97 * manager are the same as the default values provided by JavacFileManager --
jjg@450 98 * i.e. as determined by the javac.file.Paths class. To override these values,
jjg@450 99 * call {@link #setLocation}.
jjg@450 100 *
jjg@450 101 * <p>To reduce confusion with Path objects, the locations such as "class path",
jjg@450 102 * "source path", etc, are generically referred to here as "search paths".
jjg@450 103 *
jjg@581 104 * <p><b>This is NOT part of any supported API.
jjg@581 105 * If you write code that depends on this, you do so at your own risk.
jjg@450 106 * This code and its internal interfaces are subject to change or
jjg@450 107 * deletion without notice.</b>
jjg@450 108 */
jjg@450 109 public class JavacPathFileManager extends BaseFileManager implements PathFileManager {
jjg@450 110 protected FileSystem defaultFileSystem;
jjg@450 111
jjg@450 112 /**
jjg@450 113 * Create a JavacPathFileManager using a given context, optionally registering
jjg@450 114 * it as the JavaFileManager for that context.
jjg@450 115 */
jjg@450 116 public JavacPathFileManager(Context context, boolean register, Charset charset) {
jjg@450 117 super(charset);
jjg@450 118 if (register)
jjg@450 119 context.put(JavaFileManager.class, this);
jjg@450 120 pathsForLocation = new HashMap<Location, PathsForLocation>();
jjg@450 121 fileSystems = new HashMap<Path,FileSystem>();
jjg@450 122 setContext(context);
jjg@450 123 }
jjg@450 124
jjg@450 125 /**
jjg@450 126 * Set the context for JavacPathFileManager.
jjg@450 127 */
jjg@450 128 @Override
jjg@450 129 protected void setContext(Context context) {
jjg@450 130 super.setContext(context);
jjg@450 131 searchPaths = Paths.instance(context);
jjg@450 132 }
jjg@450 133
jjg@450 134 @Override
jjg@450 135 public FileSystem getDefaultFileSystem() {
jjg@450 136 if (defaultFileSystem == null)
jjg@450 137 defaultFileSystem = FileSystems.getDefault();
jjg@450 138 return defaultFileSystem;
jjg@450 139 }
jjg@450 140
jjg@450 141 @Override
jjg@450 142 public void setDefaultFileSystem(FileSystem fs) {
jjg@450 143 defaultFileSystem = fs;
jjg@450 144 }
jjg@450 145
jjg@450 146 @Override
jjg@450 147 public void flush() throws IOException {
jjg@450 148 contentCache.clear();
jjg@450 149 }
jjg@450 150
jjg@450 151 @Override
jjg@450 152 public void close() throws IOException {
jjg@450 153 for (FileSystem fs: fileSystems.values())
jjg@450 154 fs.close();
jjg@450 155 }
jjg@450 156
jjg@450 157 @Override
jjg@450 158 public ClassLoader getClassLoader(Location location) {
jjg@450 159 nullCheck(location);
jjg@450 160 Iterable<? extends Path> path = getLocation(location);
jjg@450 161 if (path == null)
jjg@450 162 return null;
jjg@450 163 ListBuffer<URL> lb = new ListBuffer<URL>();
jjg@450 164 for (Path p: path) {
jjg@450 165 try {
jjg@450 166 lb.append(p.toUri().toURL());
jjg@450 167 } catch (MalformedURLException e) {
jjg@450 168 throw new AssertionError(e);
jjg@450 169 }
jjg@450 170 }
jjg@450 171
jjg@450 172 return getClassLoader(lb.toArray(new URL[lb.size()]));
jjg@450 173 }
jjg@450 174
jjg@757 175 @Override
jjg@757 176 public boolean isDefaultBootClassPath() {
jjg@757 177 return searchPaths.isDefaultBootClassPath();
jjg@757 178 }
jjg@757 179
jjg@450 180 // <editor-fold defaultstate="collapsed" desc="Location handling">
jjg@450 181
jjg@450 182 public boolean hasLocation(Location location) {
jjg@450 183 return (getLocation(location) != null);
jjg@450 184 }
jjg@450 185
jjg@450 186 public Iterable<? extends Path> getLocation(Location location) {
jjg@450 187 nullCheck(location);
jjg@450 188 lazyInitSearchPaths();
jjg@450 189 PathsForLocation path = pathsForLocation.get(location);
jjg@450 190 if (path == null && !pathsForLocation.containsKey(location)) {
jjg@450 191 setDefaultForLocation(location);
jjg@450 192 path = pathsForLocation.get(location);
jjg@450 193 }
jjg@450 194 return path;
jjg@450 195 }
jjg@450 196
jjg@450 197 private Path getOutputLocation(Location location) {
jjg@450 198 Iterable<? extends Path> paths = getLocation(location);
jjg@450 199 return (paths == null ? null : paths.iterator().next());
jjg@450 200 }
jjg@450 201
jjg@450 202 public void setLocation(Location location, Iterable<? extends Path> searchPath)
jjg@450 203 throws IOException
jjg@450 204 {
jjg@450 205 nullCheck(location);
jjg@450 206 lazyInitSearchPaths();
jjg@450 207 if (searchPath == null) {
jjg@450 208 setDefaultForLocation(location);
jjg@450 209 } else {
jjg@450 210 if (location.isOutputLocation())
jjg@450 211 checkOutputPath(searchPath);
jjg@450 212 PathsForLocation pl = new PathsForLocation();
jjg@450 213 for (Path p: searchPath)
jjg@450 214 pl.add(p); // TODO -Xlint:path warn if path not found
jjg@450 215 pathsForLocation.put(location, pl);
jjg@450 216 }
jjg@450 217 }
jjg@450 218
jjg@450 219 private void checkOutputPath(Iterable<? extends Path> searchPath) throws IOException {
jjg@450 220 Iterator<? extends Path> pathIter = searchPath.iterator();
jjg@450 221 if (!pathIter.hasNext())
jjg@450 222 throw new IllegalArgumentException("empty path for directory");
jjg@450 223 Path path = pathIter.next();
jjg@450 224 if (pathIter.hasNext())
jjg@450 225 throw new IllegalArgumentException("path too long for directory");
jjg@450 226 if (!path.exists())
jjg@450 227 throw new FileNotFoundException(path + ": does not exist");
jjg@450 228 else if (!isDirectory(path))
jjg@450 229 throw new IOException(path + ": not a directory");
jjg@450 230 }
jjg@450 231
jjg@450 232 private void setDefaultForLocation(Location locn) {
jjg@450 233 Collection<File> files = null;
jjg@450 234 if (locn instanceof StandardLocation) {
jjg@450 235 switch ((StandardLocation) locn) {
jjg@450 236 case CLASS_PATH:
jjg@450 237 files = searchPaths.userClassPath();
jjg@450 238 break;
jjg@450 239 case PLATFORM_CLASS_PATH:
jjg@450 240 files = searchPaths.bootClassPath();
jjg@450 241 break;
jjg@450 242 case SOURCE_PATH:
jjg@450 243 files = searchPaths.sourcePath();
jjg@450 244 break;
jjg@450 245 case CLASS_OUTPUT: {
jjg@450 246 String arg = options.get(D);
jjg@450 247 files = (arg == null ? null : Collections.singleton(new File(arg)));
jjg@450 248 break;
jjg@450 249 }
jjg@450 250 case SOURCE_OUTPUT: {
jjg@450 251 String arg = options.get(S);
jjg@450 252 files = (arg == null ? null : Collections.singleton(new File(arg)));
jjg@450 253 break;
jjg@450 254 }
jjg@450 255 }
jjg@450 256 }
jjg@450 257
jjg@450 258 PathsForLocation pl = new PathsForLocation();
jjg@450 259 if (files != null) {
jjg@450 260 for (File f: files)
jjg@450 261 pl.add(f.toPath());
jjg@450 262 }
jjg@450 263 pathsForLocation.put(locn, pl);
jjg@450 264 }
jjg@450 265
jjg@450 266 private void lazyInitSearchPaths() {
jjg@450 267 if (!inited) {
jjg@450 268 setDefaultForLocation(PLATFORM_CLASS_PATH);
jjg@450 269 setDefaultForLocation(CLASS_PATH);
jjg@450 270 setDefaultForLocation(SOURCE_PATH);
jjg@450 271 inited = true;
jjg@450 272 }
jjg@450 273 }
jjg@450 274 // where
jjg@450 275 private boolean inited = false;
jjg@450 276
jjg@450 277 private Map<Location, PathsForLocation> pathsForLocation;
jjg@450 278 private Paths searchPaths;
jjg@450 279
jjg@450 280 private static class PathsForLocation extends LinkedHashSet<Path> {
jjg@450 281 private static final long serialVersionUID = 6788510222394486733L;
jjg@450 282 }
jjg@450 283
jjg@450 284 // </editor-fold>
jjg@450 285
jjg@450 286 // <editor-fold defaultstate="collapsed" desc="FileObject handling">
jjg@450 287
jjg@450 288 @Override
jjg@450 289 public Path getPath(FileObject fo) {
jjg@450 290 nullCheck(fo);
jjg@450 291 if (!(fo instanceof PathFileObject))
jjg@450 292 throw new IllegalArgumentException();
jjg@450 293 return ((PathFileObject) fo).getPath();
jjg@450 294 }
jjg@450 295
jjg@450 296 @Override
jjg@450 297 public boolean isSameFile(FileObject a, FileObject b) {
jjg@450 298 nullCheck(a);
jjg@450 299 nullCheck(b);
jjg@450 300 if (!(a instanceof PathFileObject))
jjg@450 301 throw new IllegalArgumentException("Not supported: " + a);
jjg@450 302 if (!(b instanceof PathFileObject))
jjg@450 303 throw new IllegalArgumentException("Not supported: " + b);
jjg@450 304 return ((PathFileObject) a).isSameFile((PathFileObject) b);
jjg@450 305 }
jjg@450 306
jjg@450 307 @Override
jjg@450 308 public Iterable<JavaFileObject> list(Location location,
jjg@450 309 String packageName, Set<Kind> kinds, boolean recurse)
jjg@450 310 throws IOException {
jjg@450 311 // validatePackageName(packageName);
jjg@450 312 nullCheck(packageName);
jjg@450 313 nullCheck(kinds);
jjg@450 314
jjg@450 315 Iterable<? extends Path> paths = getLocation(location);
jjg@450 316 if (paths == null)
jjg@450 317 return List.nil();
jjg@450 318 ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>();
jjg@450 319
jjg@450 320 for (Path path : paths)
jjg@450 321 list(path, packageName, kinds, recurse, results);
jjg@450 322
jjg@450 323 return results.toList();
jjg@450 324 }
jjg@450 325
jjg@450 326 private void list(Path path, String packageName, final Set<Kind> kinds,
jjg@450 327 boolean recurse, final ListBuffer<JavaFileObject> results)
jjg@450 328 throws IOException {
jjg@450 329 if (!path.exists())
jjg@450 330 return;
jjg@450 331
jjg@450 332 final Path pathDir;
jjg@450 333 if (isDirectory(path))
jjg@450 334 pathDir = path;
jjg@450 335 else {
jjg@450 336 FileSystem fs = getFileSystem(path);
jjg@450 337 if (fs == null)
jjg@450 338 return;
jjg@450 339 pathDir = fs.getRootDirectories().iterator().next();
jjg@450 340 }
jjg@450 341 String sep = path.getFileSystem().getSeparator();
jjg@450 342 Path packageDir = packageName.isEmpty() ? pathDir
jjg@450 343 : pathDir.resolve(packageName.replace(".", sep));
jjg@450 344 if (!packageDir.exists())
jjg@450 345 return;
jjg@450 346
jjg@450 347 /* Alternate impl of list, superceded by use of Files.walkFileTree */
jjg@450 348 // Deque<Path> queue = new LinkedList<Path>();
jjg@450 349 // queue.add(packageDir);
jjg@450 350 //
jjg@450 351 // Path dir;
jjg@450 352 // while ((dir = queue.poll()) != null) {
jjg@450 353 // DirectoryStream<Path> ds = dir.newDirectoryStream();
jjg@450 354 // try {
jjg@450 355 // for (Path p: ds) {
jjg@450 356 // String name = p.getName().toString();
jjg@450 357 // if (isDirectory(p)) {
jjg@450 358 // if (recurse && SourceVersion.isIdentifier(name)) {
jjg@450 359 // queue.add(p);
jjg@450 360 // }
jjg@450 361 // } else {
jjg@450 362 // if (kinds.contains(getKind(name))) {
jjg@450 363 // JavaFileObject fe =
jjg@450 364 // PathFileObject.createDirectoryPathFileObject(this, p, pathDir);
jjg@450 365 // results.append(fe);
jjg@450 366 // }
jjg@450 367 // }
jjg@450 368 // }
jjg@450 369 // } finally {
jjg@450 370 // ds.close();
jjg@450 371 // }
jjg@450 372 // }
jjg@450 373 int maxDepth = (recurse ? Integer.MAX_VALUE : 1);
alanb@701 374 Set<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS);
jjg@450 375 Files.walkFileTree(packageDir, opts, maxDepth,
jjg@450 376 new SimpleFileVisitor<Path>() {
jjg@450 377 @Override
alanb@701 378 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
jjg@450 379 if (SourceVersion.isIdentifier(dir.getName().toString())) // JSR 292?
jjg@450 380 return FileVisitResult.CONTINUE;
jjg@450 381 else
jjg@450 382 return FileVisitResult.SKIP_SUBTREE;
jjg@450 383 }
jjg@450 384
jjg@450 385 @Override
jjg@450 386 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
jjg@450 387 if (attrs.isRegularFile() && kinds.contains(getKind(file.getName().toString()))) {
jjg@450 388 JavaFileObject fe =
jjg@450 389 PathFileObject.createDirectoryPathFileObject(
jjg@450 390 JavacPathFileManager.this, file, pathDir);
jjg@450 391 results.append(fe);
jjg@450 392 }
jjg@450 393 return FileVisitResult.CONTINUE;
jjg@450 394 }
jjg@450 395 });
jjg@450 396 }
jjg@450 397
jjg@450 398 @Override
jjg@450 399 public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(
jjg@450 400 Iterable<? extends Path> paths) {
jjg@450 401 ArrayList<PathFileObject> result;
jjg@450 402 if (paths instanceof Collection<?>)
jjg@450 403 result = new ArrayList<PathFileObject>(((Collection<?>)paths).size());
jjg@450 404 else
jjg@450 405 result = new ArrayList<PathFileObject>();
jjg@450 406 for (Path p: paths)
jjg@450 407 result.add(PathFileObject.createSimplePathFileObject(this, nullCheck(p)));
jjg@450 408 return result;
jjg@450 409 }
jjg@450 410
jjg@450 411 @Override
jjg@450 412 public Iterable<? extends JavaFileObject> getJavaFileObjects(Path... paths) {
jjg@450 413 return getJavaFileObjectsFromPaths(Arrays.asList(nullCheck(paths)));
jjg@450 414 }
jjg@450 415
jjg@450 416 @Override
jjg@450 417 public JavaFileObject getJavaFileForInput(Location location,
jjg@450 418 String className, Kind kind) throws IOException {
jjg@450 419 return getFileForInput(location, getRelativePath(className, kind));
jjg@450 420 }
jjg@450 421
jjg@450 422 @Override
jjg@450 423 public FileObject getFileForInput(Location location,
jjg@450 424 String packageName, String relativeName) throws IOException {
jjg@450 425 return getFileForInput(location, getRelativePath(packageName, relativeName));
jjg@450 426 }
jjg@450 427
jjg@450 428 private JavaFileObject getFileForInput(Location location, String relativePath)
jjg@450 429 throws IOException {
jjg@450 430 for (Path p: getLocation(location)) {
jjg@450 431 if (isDirectory(p)) {
jjg@450 432 Path f = resolve(p, relativePath);
jjg@450 433 if (f.exists())
jjg@450 434 return PathFileObject.createDirectoryPathFileObject(this, f, p);
jjg@450 435 } else {
jjg@450 436 FileSystem fs = getFileSystem(p);
jjg@450 437 if (fs != null) {
jjg@450 438 Path file = getPath(fs, relativePath);
jjg@450 439 if (file.exists())
jjg@450 440 return PathFileObject.createJarPathFileObject(this, file);
jjg@450 441 }
jjg@450 442 }
jjg@450 443 }
jjg@450 444 return null;
jjg@450 445 }
jjg@450 446
jjg@450 447 @Override
jjg@450 448 public JavaFileObject getJavaFileForOutput(Location location,
jjg@450 449 String className, Kind kind, FileObject sibling) throws IOException {
jjg@450 450 return getFileForOutput(location, getRelativePath(className, kind), sibling);
jjg@450 451 }
jjg@450 452
jjg@450 453 @Override
jjg@450 454 public FileObject getFileForOutput(Location location, String packageName,
jjg@450 455 String relativeName, FileObject sibling)
jjg@450 456 throws IOException {
jjg@450 457 return getFileForOutput(location, getRelativePath(packageName, relativeName), sibling);
jjg@450 458 }
jjg@450 459
jjg@450 460 private JavaFileObject getFileForOutput(Location location,
jjg@450 461 String relativePath, FileObject sibling) {
jjg@450 462 Path dir = getOutputLocation(location);
jjg@450 463 if (dir == null) {
jjg@450 464 if (location == CLASS_OUTPUT) {
jjg@450 465 Path siblingDir = null;
jjg@450 466 if (sibling != null && sibling instanceof PathFileObject) {
jjg@450 467 siblingDir = ((PathFileObject) sibling).getPath().getParent();
jjg@450 468 }
jjg@450 469 return PathFileObject.createSiblingPathFileObject(this,
jjg@450 470 siblingDir.resolve(getBaseName(relativePath)),
jjg@450 471 relativePath);
jjg@450 472 } else if (location == SOURCE_OUTPUT) {
jjg@450 473 dir = getOutputLocation(CLASS_OUTPUT);
jjg@450 474 }
jjg@450 475 }
jjg@450 476
jjg@450 477 Path file;
jjg@450 478 if (dir != null) {
jjg@450 479 file = resolve(dir, relativePath);
jjg@450 480 return PathFileObject.createDirectoryPathFileObject(this, file, dir);
jjg@450 481 } else {
jjg@450 482 file = getPath(getDefaultFileSystem(), relativePath);
jjg@450 483 return PathFileObject.createSimplePathFileObject(this, file);
jjg@450 484 }
jjg@450 485
jjg@450 486 }
jjg@450 487
jjg@450 488 @Override
jjg@450 489 public String inferBinaryName(Location location, JavaFileObject fo) {
jjg@450 490 nullCheck(fo);
jjg@450 491 // Need to match the path semantics of list(location, ...)
jjg@450 492 Iterable<? extends Path> paths = getLocation(location);
jjg@450 493 if (paths == null) {
jjg@450 494 return null;
jjg@450 495 }
jjg@450 496
jjg@450 497 if (!(fo instanceof PathFileObject))
jjg@450 498 throw new IllegalArgumentException(fo.getClass().getName());
jjg@450 499
jjg@450 500 return ((PathFileObject) fo).inferBinaryName(paths);
jjg@450 501 }
jjg@450 502
jjg@450 503 private FileSystem getFileSystem(Path p) throws IOException {
jjg@450 504 FileSystem fs = fileSystems.get(p);
jjg@450 505 if (fs == null) {
jjg@450 506 fs = FileSystems.newFileSystem(p, Collections.<String,Void>emptyMap(), null);
jjg@450 507 fileSystems.put(p, fs);
jjg@450 508 }
jjg@450 509 return fs;
jjg@450 510 }
jjg@450 511
jjg@450 512 private Map<Path,FileSystem> fileSystems;
jjg@450 513
jjg@450 514 // </editor-fold>
jjg@450 515
jjg@450 516 // <editor-fold defaultstate="collapsed" desc="Utility methods">
jjg@450 517
jjg@450 518 private static String getRelativePath(String className, Kind kind) {
jjg@450 519 return className.replace(".", "/") + kind.extension;
jjg@450 520 }
jjg@450 521
jjg@450 522 private static String getRelativePath(String packageName, String relativeName) {
jjg@450 523 return packageName.replace(".", "/") + relativeName;
jjg@450 524 }
jjg@450 525
jjg@450 526 private static String getBaseName(String relativePath) {
jjg@450 527 int lastSep = relativePath.lastIndexOf("/");
jjg@450 528 return relativePath.substring(lastSep + 1); // safe if "/" not found
jjg@450 529 }
jjg@450 530
jjg@450 531 private static boolean isDirectory(Path path) throws IOException {
jjg@450 532 BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path);
jjg@450 533 return attrs.isDirectory();
jjg@450 534 }
jjg@450 535
jjg@450 536 private static Path getPath(FileSystem fs, String relativePath) {
jjg@450 537 return fs.getPath(relativePath.replace("/", fs.getSeparator()));
jjg@450 538 }
jjg@450 539
jjg@450 540 private static Path resolve(Path base, String relativePath) {
jjg@450 541 FileSystem fs = base.getFileSystem();
jjg@450 542 Path rp = fs.getPath(relativePath.replace("/", fs.getSeparator()));
jjg@450 543 return base.resolve(rp);
jjg@450 544 }
jjg@450 545
jjg@450 546 // </editor-fold>
jjg@450 547
jjg@450 548 }

mercurial