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

Wed, 06 Apr 2011 20:33:44 -0700

author
ohair
date
Wed, 06 Apr 2011 20:33:44 -0700
changeset 962
0ff2bbd38f10
parent 847
babf86a1ac92
child 1111
d2cbb77469ed
permissions
-rw-r--r--

7033660: Update copyright year to 2011 on any files changed in 2011
Reviewed-by: dholmes

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

mercurial