Wed, 19 Oct 2011 15:29:46 -0700
7101146: Paths should more directly managed by BaseFileManager
Reviewed-by: mcimadamore
duke@1 | 1 | /* |
jjg@818 | 2 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 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 |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 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. |
duke@1 | 24 | */ |
duke@1 | 25 | |
jjg@50 | 26 | package com.sun.tools.javac.file; |
jjg@50 | 27 | |
duke@1 | 28 | import java.io.File; |
duke@1 | 29 | import java.io.IOException; |
darcy@497 | 30 | import java.net.MalformedURLException; |
darcy@497 | 31 | import java.net.URL; |
duke@1 | 32 | import java.util.HashMap; |
duke@1 | 33 | import java.util.HashSet; |
duke@1 | 34 | import java.util.Map; |
duke@1 | 35 | import java.util.Set; |
duke@1 | 36 | import java.util.Collection; |
duke@1 | 37 | import java.util.Collections; |
duke@1 | 38 | import java.util.LinkedHashSet; |
darcy@497 | 39 | import java.util.StringTokenizer; |
duke@1 | 40 | import java.util.zip.ZipFile; |
duke@1 | 41 | import javax.tools.JavaFileManager.Location; |
duke@1 | 42 | |
jjg@50 | 43 | import com.sun.tools.javac.code.Lint; |
jjg@151 | 44 | import com.sun.tools.javac.util.ListBuffer; |
jjg@50 | 45 | import com.sun.tools.javac.util.Log; |
jjg@50 | 46 | import com.sun.tools.javac.util.Options; |
jjg@50 | 47 | |
jjg@50 | 48 | import static javax.tools.StandardLocation.*; |
duke@1 | 49 | import static com.sun.tools.javac.main.OptionName.*; |
duke@1 | 50 | |
duke@1 | 51 | /** This class converts command line arguments, environment variables |
duke@1 | 52 | * and system properties (in File.pathSeparator-separated String form) |
duke@1 | 53 | * into a boot class path, user class path, and source path (in |
duke@1 | 54 | * Collection<String> form). |
duke@1 | 55 | * |
jjg@581 | 56 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 57 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 58 | * This code and its internal interfaces are subject to change or |
duke@1 | 59 | * deletion without notice.</b> |
duke@1 | 60 | */ |
duke@1 | 61 | public class Paths { |
duke@1 | 62 | |
duke@1 | 63 | /** The log to use for warning output */ |
duke@1 | 64 | private Log log; |
duke@1 | 65 | |
duke@1 | 66 | /** Collection of command-line options */ |
duke@1 | 67 | private Options options; |
duke@1 | 68 | |
duke@1 | 69 | /** Handler for -Xlint options */ |
duke@1 | 70 | private Lint lint; |
duke@1 | 71 | |
jjg@106 | 72 | /** Access to (possibly cached) file info */ |
jjg@106 | 73 | private FSInfo fsInfo; |
duke@1 | 74 | |
jjg@1111 | 75 | public Paths() { |
duke@1 | 76 | pathsForLocation = new HashMap<Location,Path>(16); |
duke@1 | 77 | } |
duke@1 | 78 | |
jjg@1111 | 79 | public void update(Log log, Options options, Lint lint, FSInfo fsInfo) { |
jjg@1111 | 80 | this.log = log; |
jjg@1111 | 81 | this.options = options; |
jjg@1111 | 82 | this.lint = lint; |
jjg@1111 | 83 | this.fsInfo = fsInfo; |
duke@1 | 84 | } |
duke@1 | 85 | |
duke@1 | 86 | /** Whether to warn about non-existent path elements */ |
duke@1 | 87 | private boolean warn; |
duke@1 | 88 | |
duke@1 | 89 | private Map<Location, Path> pathsForLocation; |
duke@1 | 90 | |
duke@1 | 91 | private boolean inited = false; // TODO? caching bad? |
duke@1 | 92 | |
duke@1 | 93 | /** |
duke@1 | 94 | * rt.jar as found on the default bootclass path. If the user specified a |
duke@1 | 95 | * bootclasspath, null is used. |
duke@1 | 96 | */ |
jjg@818 | 97 | private File defaultBootClassPathRtJar = null; |
duke@1 | 98 | |
jjg@757 | 99 | /** |
jjg@757 | 100 | * Is bootclasspath the default? |
jjg@757 | 101 | */ |
jjg@757 | 102 | private boolean isDefaultBootClassPath; |
jjg@757 | 103 | |
duke@1 | 104 | Path getPathForLocation(Location location) { |
duke@1 | 105 | Path path = pathsForLocation.get(location); |
duke@1 | 106 | if (path == null) |
duke@1 | 107 | setPathForLocation(location, null); |
duke@1 | 108 | return pathsForLocation.get(location); |
duke@1 | 109 | } |
duke@1 | 110 | |
duke@1 | 111 | void setPathForLocation(Location location, Iterable<? extends File> path) { |
duke@1 | 112 | // TODO? if (inited) throw new IllegalStateException |
duke@1 | 113 | // TODO: otherwise reset sourceSearchPath, classSearchPath as needed |
duke@1 | 114 | Path p; |
duke@1 | 115 | if (path == null) { |
duke@1 | 116 | if (location == CLASS_PATH) |
duke@1 | 117 | p = computeUserClassPath(); |
duke@1 | 118 | else if (location == PLATFORM_CLASS_PATH) |
jjg@757 | 119 | p = computeBootClassPath(); // sets isDefaultBootClassPath |
duke@1 | 120 | else if (location == ANNOTATION_PROCESSOR_PATH) |
duke@1 | 121 | p = computeAnnotationProcessorPath(); |
duke@1 | 122 | else if (location == SOURCE_PATH) |
duke@1 | 123 | p = computeSourcePath(); |
duke@1 | 124 | else |
duke@1 | 125 | // no defaults for other paths |
duke@1 | 126 | p = null; |
duke@1 | 127 | } else { |
jjg@818 | 128 | if (location == PLATFORM_CLASS_PATH) { |
jjg@818 | 129 | defaultBootClassPathRtJar = null; |
jjg@757 | 130 | isDefaultBootClassPath = false; |
jjg@818 | 131 | } |
duke@1 | 132 | p = new Path(); |
duke@1 | 133 | for (File f: path) |
duke@1 | 134 | p.addFile(f, warn); // TODO: is use of warn appropriate? |
duke@1 | 135 | } |
duke@1 | 136 | pathsForLocation.put(location, p); |
duke@1 | 137 | } |
duke@1 | 138 | |
jjg@758 | 139 | public boolean isDefaultBootClassPath() { |
jjg@757 | 140 | lazy(); |
jjg@757 | 141 | return isDefaultBootClassPath; |
jjg@757 | 142 | } |
jjg@757 | 143 | |
duke@1 | 144 | protected void lazy() { |
duke@1 | 145 | if (!inited) { |
duke@1 | 146 | warn = lint.isEnabled(Lint.LintCategory.PATH); |
duke@1 | 147 | |
duke@1 | 148 | pathsForLocation.put(PLATFORM_CLASS_PATH, computeBootClassPath()); |
duke@1 | 149 | pathsForLocation.put(CLASS_PATH, computeUserClassPath()); |
duke@1 | 150 | pathsForLocation.put(SOURCE_PATH, computeSourcePath()); |
duke@1 | 151 | |
duke@1 | 152 | inited = true; |
duke@1 | 153 | } |
duke@1 | 154 | } |
duke@1 | 155 | |
duke@1 | 156 | public Collection<File> bootClassPath() { |
duke@1 | 157 | lazy(); |
duke@1 | 158 | return Collections.unmodifiableCollection(getPathForLocation(PLATFORM_CLASS_PATH)); |
duke@1 | 159 | } |
duke@1 | 160 | public Collection<File> userClassPath() { |
duke@1 | 161 | lazy(); |
duke@1 | 162 | return Collections.unmodifiableCollection(getPathForLocation(CLASS_PATH)); |
duke@1 | 163 | } |
duke@1 | 164 | public Collection<File> sourcePath() { |
duke@1 | 165 | lazy(); |
duke@1 | 166 | Path p = getPathForLocation(SOURCE_PATH); |
duke@1 | 167 | return p == null || p.size() == 0 |
duke@1 | 168 | ? null |
duke@1 | 169 | : Collections.unmodifiableCollection(p); |
duke@1 | 170 | } |
duke@1 | 171 | |
jjg@818 | 172 | boolean isDefaultBootClassPathRtJar(File file) { |
jjg@818 | 173 | return file.equals(defaultBootClassPathRtJar); |
duke@1 | 174 | } |
duke@1 | 175 | |
jjg@151 | 176 | /** |
jjg@151 | 177 | * Split a path into its elements. Empty path elements will be ignored. |
jjg@151 | 178 | * @param path The path to be split |
jjg@151 | 179 | * @return The elements of the path |
jjg@151 | 180 | */ |
jjg@151 | 181 | private static Iterable<File> getPathEntries(String path) { |
jjg@151 | 182 | return getPathEntries(path, null); |
jjg@151 | 183 | } |
duke@1 | 184 | |
jjg@151 | 185 | /** |
jjg@151 | 186 | * Split a path into its elements. If emptyPathDefault is not null, all |
jjg@151 | 187 | * empty elements in the path, including empty elements at either end of |
jjg@151 | 188 | * the path, will be replaced with the value of emptyPathDefault. |
jjg@151 | 189 | * @param path The path to be split |
jjg@151 | 190 | * @param emptyPathDefault The value to substitute for empty path elements, |
jjg@151 | 191 | * or null, to ignore empty path elements |
jjg@151 | 192 | * @return The elements of the path |
jjg@151 | 193 | */ |
jjg@151 | 194 | private static Iterable<File> getPathEntries(String path, File emptyPathDefault) { |
jjg@151 | 195 | ListBuffer<File> entries = new ListBuffer<File>(); |
jjg@151 | 196 | int start = 0; |
jjg@151 | 197 | while (start <= path.length()) { |
jjg@151 | 198 | int sep = path.indexOf(File.pathSeparatorChar, start); |
jjg@151 | 199 | if (sep == -1) |
jjg@151 | 200 | sep = path.length(); |
jjg@151 | 201 | if (start < sep) |
jjg@151 | 202 | entries.add(new File(path.substring(start, sep))); |
jjg@151 | 203 | else if (emptyPathDefault != null) |
jjg@151 | 204 | entries.add(emptyPathDefault); |
jjg@151 | 205 | start = sep + 1; |
duke@1 | 206 | } |
jjg@151 | 207 | return entries; |
duke@1 | 208 | } |
duke@1 | 209 | |
duke@1 | 210 | private class Path extends LinkedHashSet<File> { |
duke@1 | 211 | private static final long serialVersionUID = 0; |
duke@1 | 212 | |
duke@1 | 213 | private boolean expandJarClassPaths = false; |
duke@1 | 214 | private Set<File> canonicalValues = new HashSet<File>(); |
duke@1 | 215 | |
duke@1 | 216 | public Path expandJarClassPaths(boolean x) { |
duke@1 | 217 | expandJarClassPaths = x; |
duke@1 | 218 | return this; |
duke@1 | 219 | } |
duke@1 | 220 | |
duke@1 | 221 | /** What to use when path element is the empty string */ |
jjg@151 | 222 | private File emptyPathDefault = null; |
duke@1 | 223 | |
jjg@151 | 224 | public Path emptyPathDefault(File x) { |
duke@1 | 225 | emptyPathDefault = x; |
duke@1 | 226 | return this; |
duke@1 | 227 | } |
duke@1 | 228 | |
duke@1 | 229 | public Path() { super(); } |
duke@1 | 230 | |
duke@1 | 231 | public Path addDirectories(String dirs, boolean warn) { |
jjg@874 | 232 | boolean prev = expandJarClassPaths; |
jjg@874 | 233 | expandJarClassPaths = true; |
jjg@874 | 234 | try { |
jjg@874 | 235 | if (dirs != null) |
jjg@874 | 236 | for (File dir : getPathEntries(dirs)) |
jjg@874 | 237 | addDirectory(dir, warn); |
jjg@874 | 238 | return this; |
jjg@874 | 239 | } finally { |
jjg@874 | 240 | expandJarClassPaths = prev; |
jjg@874 | 241 | } |
duke@1 | 242 | } |
duke@1 | 243 | |
duke@1 | 244 | public Path addDirectories(String dirs) { |
duke@1 | 245 | return addDirectories(dirs, warn); |
duke@1 | 246 | } |
duke@1 | 247 | |
jjg@151 | 248 | private void addDirectory(File dir, boolean warn) { |
jjg@151 | 249 | if (!dir.isDirectory()) { |
duke@1 | 250 | if (warn) |
jjg@612 | 251 | log.warning(Lint.LintCategory.PATH, |
jjg@612 | 252 | "dir.path.element.not.found", dir); |
duke@1 | 253 | return; |
duke@1 | 254 | } |
duke@1 | 255 | |
jjg@151 | 256 | File[] files = dir.listFiles(); |
duke@1 | 257 | if (files == null) |
duke@1 | 258 | return; |
duke@1 | 259 | |
duke@1 | 260 | for (File direntry : files) { |
duke@1 | 261 | if (isArchive(direntry)) |
duke@1 | 262 | addFile(direntry, warn); |
duke@1 | 263 | } |
duke@1 | 264 | } |
duke@1 | 265 | |
duke@1 | 266 | public Path addFiles(String files, boolean warn) { |
jjg@757 | 267 | if (files != null) { |
jjg@151 | 268 | for (File file : getPathEntries(files, emptyPathDefault)) |
duke@1 | 269 | addFile(file, warn); |
jjg@757 | 270 | } |
duke@1 | 271 | return this; |
duke@1 | 272 | } |
duke@1 | 273 | |
duke@1 | 274 | public Path addFiles(String files) { |
duke@1 | 275 | return addFiles(files, warn); |
duke@1 | 276 | } |
duke@1 | 277 | |
duke@1 | 278 | public void addFile(File file, boolean warn) { |
jjh@801 | 279 | if (contains(file)) { |
jjh@801 | 280 | // discard duplicates |
duke@1 | 281 | return; |
duke@1 | 282 | } |
duke@1 | 283 | |
jjg@106 | 284 | if (! fsInfo.exists(file)) { |
duke@1 | 285 | /* No such file or directory exists */ |
jjg@612 | 286 | if (warn) { |
jjg@612 | 287 | log.warning(Lint.LintCategory.PATH, |
jjg@612 | 288 | "path.element.not.found", file); |
jjg@612 | 289 | } |
jjh@801 | 290 | super.add(file); |
jjh@801 | 291 | return; |
jjh@801 | 292 | } |
jjh@801 | 293 | |
jjh@801 | 294 | File canonFile = fsInfo.getCanonicalFile(file); |
jjh@801 | 295 | if (canonicalValues.contains(canonFile)) { |
jjh@801 | 296 | /* Discard duplicates and avoid infinite recursion */ |
jjh@801 | 297 | return; |
jjh@801 | 298 | } |
jjh@801 | 299 | |
jjh@801 | 300 | if (fsInfo.isFile(file)) { |
duke@1 | 301 | /* File is an ordinary file. */ |
duke@1 | 302 | if (!isArchive(file)) { |
duke@1 | 303 | /* Not a recognized extension; open it to see if |
duke@1 | 304 | it looks like a valid zip file. */ |
duke@1 | 305 | try { |
duke@1 | 306 | ZipFile z = new ZipFile(file); |
duke@1 | 307 | z.close(); |
jjg@612 | 308 | if (warn) { |
jjg@612 | 309 | log.warning(Lint.LintCategory.PATH, |
jjg@612 | 310 | "unexpected.archive.file", file); |
jjg@612 | 311 | } |
duke@1 | 312 | } catch (IOException e) { |
duke@1 | 313 | // FIXME: include e.getLocalizedMessage in warning |
jjg@612 | 314 | if (warn) { |
jjg@612 | 315 | log.warning(Lint.LintCategory.PATH, |
jjg@612 | 316 | "invalid.archive.file", file); |
jjg@612 | 317 | } |
duke@1 | 318 | return; |
duke@1 | 319 | } |
duke@1 | 320 | } |
duke@1 | 321 | } |
duke@1 | 322 | |
duke@1 | 323 | /* Now what we have left is either a directory or a file name |
jjh@801 | 324 | conforming to archive naming convention */ |
duke@1 | 325 | super.add(file); |
jjg@106 | 326 | canonicalValues.add(canonFile); |
duke@1 | 327 | |
jjh@801 | 328 | if (expandJarClassPaths && fsInfo.isFile(file)) |
duke@1 | 329 | addJarClassPath(file, warn); |
duke@1 | 330 | } |
duke@1 | 331 | |
duke@1 | 332 | // Adds referenced classpath elements from a jar's Class-Path |
duke@1 | 333 | // Manifest entry. In some future release, we may want to |
duke@1 | 334 | // update this code to recognize URLs rather than simple |
duke@1 | 335 | // filenames, but if we do, we should redo all path-related code. |
duke@1 | 336 | private void addJarClassPath(File jarFile, boolean warn) { |
duke@1 | 337 | try { |
jjg@106 | 338 | for (File f: fsInfo.getJarClassPath(jarFile)) { |
jjg@106 | 339 | addFile(f, warn); |
duke@1 | 340 | } |
duke@1 | 341 | } catch (IOException e) { |
jjg@510 | 342 | log.error("error.reading.file", jarFile, JavacFileManager.getMessage(e)); |
duke@1 | 343 | } |
duke@1 | 344 | } |
duke@1 | 345 | } |
duke@1 | 346 | |
duke@1 | 347 | private Path computeBootClassPath() { |
jjg@818 | 348 | defaultBootClassPathRtJar = null; |
duke@1 | 349 | Path path = new Path(); |
duke@1 | 350 | |
jjg@757 | 351 | String bootclasspathOpt = options.get(BOOTCLASSPATH); |
jjg@757 | 352 | String endorseddirsOpt = options.get(ENDORSEDDIRS); |
jjg@757 | 353 | String extdirsOpt = options.get(EXTDIRS); |
jjg@757 | 354 | String xbootclasspathPrependOpt = options.get(XBOOTCLASSPATH_PREPEND); |
jjg@757 | 355 | String xbootclasspathAppendOpt = options.get(XBOOTCLASSPATH_APPEND); |
duke@1 | 356 | |
jjg@757 | 357 | path.addFiles(xbootclasspathPrependOpt); |
jjg@757 | 358 | |
jjg@757 | 359 | if (endorseddirsOpt != null) |
jjg@757 | 360 | path.addDirectories(endorseddirsOpt); |
duke@1 | 361 | else |
duke@1 | 362 | path.addDirectories(System.getProperty("java.endorsed.dirs"), false); |
duke@1 | 363 | |
jjg@757 | 364 | if (bootclasspathOpt != null) { |
jjg@757 | 365 | path.addFiles(bootclasspathOpt); |
duke@1 | 366 | } else { |
duke@1 | 367 | // Standard system classes for this compiler's release. |
duke@1 | 368 | String files = System.getProperty("sun.boot.class.path"); |
duke@1 | 369 | path.addFiles(files, false); |
duke@1 | 370 | File rt_jar = new File("rt.jar"); |
jjg@151 | 371 | for (File file : getPathEntries(files)) { |
jjg@151 | 372 | if (new File(file.getName()).equals(rt_jar)) |
jjg@818 | 373 | defaultBootClassPathRtJar = file; |
duke@1 | 374 | } |
duke@1 | 375 | } |
duke@1 | 376 | |
jjg@757 | 377 | path.addFiles(xbootclasspathAppendOpt); |
duke@1 | 378 | |
duke@1 | 379 | // Strictly speaking, standard extensions are not bootstrap |
duke@1 | 380 | // classes, but we treat them identically, so we'll pretend |
duke@1 | 381 | // that they are. |
jjg@757 | 382 | if (extdirsOpt != null) |
jjg@757 | 383 | path.addDirectories(extdirsOpt); |
duke@1 | 384 | else |
duke@1 | 385 | path.addDirectories(System.getProperty("java.ext.dirs"), false); |
duke@1 | 386 | |
jjg@757 | 387 | isDefaultBootClassPath = |
jjg@757 | 388 | (xbootclasspathPrependOpt == null) && |
jjg@757 | 389 | (bootclasspathOpt == null) && |
jjg@757 | 390 | (xbootclasspathAppendOpt == null); |
jjg@757 | 391 | |
duke@1 | 392 | return path; |
duke@1 | 393 | } |
duke@1 | 394 | |
duke@1 | 395 | private Path computeUserClassPath() { |
duke@1 | 396 | String cp = options.get(CLASSPATH); |
duke@1 | 397 | |
duke@1 | 398 | // CLASSPATH environment variable when run from `javac'. |
duke@1 | 399 | if (cp == null) cp = System.getProperty("env.class.path"); |
duke@1 | 400 | |
duke@1 | 401 | // If invoked via a java VM (not the javac launcher), use the |
duke@1 | 402 | // platform class path |
duke@1 | 403 | if (cp == null && System.getProperty("application.home") == null) |
duke@1 | 404 | cp = System.getProperty("java.class.path"); |
duke@1 | 405 | |
duke@1 | 406 | // Default to current working directory. |
duke@1 | 407 | if (cp == null) cp = "."; |
duke@1 | 408 | |
duke@1 | 409 | return new Path() |
jjg@151 | 410 | .expandJarClassPaths(true) // Only search user jars for Class-Paths |
jjg@151 | 411 | .emptyPathDefault(new File(".")) // Empty path elt ==> current directory |
duke@1 | 412 | .addFiles(cp); |
duke@1 | 413 | } |
duke@1 | 414 | |
duke@1 | 415 | private Path computeSourcePath() { |
duke@1 | 416 | String sourcePathArg = options.get(SOURCEPATH); |
duke@1 | 417 | if (sourcePathArg == null) |
duke@1 | 418 | return null; |
duke@1 | 419 | |
duke@1 | 420 | return new Path().addFiles(sourcePathArg); |
duke@1 | 421 | } |
duke@1 | 422 | |
duke@1 | 423 | private Path computeAnnotationProcessorPath() { |
duke@1 | 424 | String processorPathArg = options.get(PROCESSORPATH); |
duke@1 | 425 | if (processorPathArg == null) |
duke@1 | 426 | return null; |
duke@1 | 427 | |
duke@1 | 428 | return new Path().addFiles(processorPathArg); |
duke@1 | 429 | } |
duke@1 | 430 | |
duke@1 | 431 | /** The actual effective locations searched for sources */ |
duke@1 | 432 | private Path sourceSearchPath; |
duke@1 | 433 | |
duke@1 | 434 | public Collection<File> sourceSearchPath() { |
duke@1 | 435 | if (sourceSearchPath == null) { |
duke@1 | 436 | lazy(); |
duke@1 | 437 | Path sourcePath = getPathForLocation(SOURCE_PATH); |
duke@1 | 438 | Path userClassPath = getPathForLocation(CLASS_PATH); |
duke@1 | 439 | sourceSearchPath = sourcePath != null ? sourcePath : userClassPath; |
duke@1 | 440 | } |
duke@1 | 441 | return Collections.unmodifiableCollection(sourceSearchPath); |
duke@1 | 442 | } |
duke@1 | 443 | |
duke@1 | 444 | /** The actual effective locations searched for classes */ |
duke@1 | 445 | private Path classSearchPath; |
duke@1 | 446 | |
duke@1 | 447 | public Collection<File> classSearchPath() { |
duke@1 | 448 | if (classSearchPath == null) { |
duke@1 | 449 | lazy(); |
duke@1 | 450 | Path bootClassPath = getPathForLocation(PLATFORM_CLASS_PATH); |
duke@1 | 451 | Path userClassPath = getPathForLocation(CLASS_PATH); |
duke@1 | 452 | classSearchPath = new Path(); |
duke@1 | 453 | classSearchPath.addAll(bootClassPath); |
duke@1 | 454 | classSearchPath.addAll(userClassPath); |
duke@1 | 455 | } |
duke@1 | 456 | return Collections.unmodifiableCollection(classSearchPath); |
duke@1 | 457 | } |
duke@1 | 458 | |
duke@1 | 459 | /** The actual effective locations for non-source, non-class files */ |
duke@1 | 460 | private Path otherSearchPath; |
duke@1 | 461 | |
duke@1 | 462 | Collection<File> otherSearchPath() { |
duke@1 | 463 | if (otherSearchPath == null) { |
duke@1 | 464 | lazy(); |
duke@1 | 465 | Path userClassPath = getPathForLocation(CLASS_PATH); |
duke@1 | 466 | Path sourcePath = getPathForLocation(SOURCE_PATH); |
duke@1 | 467 | if (sourcePath == null) |
duke@1 | 468 | otherSearchPath = userClassPath; |
duke@1 | 469 | else { |
duke@1 | 470 | otherSearchPath = new Path(); |
duke@1 | 471 | otherSearchPath.addAll(userClassPath); |
duke@1 | 472 | otherSearchPath.addAll(sourcePath); |
duke@1 | 473 | } |
duke@1 | 474 | } |
duke@1 | 475 | return Collections.unmodifiableCollection(otherSearchPath); |
duke@1 | 476 | } |
duke@1 | 477 | |
duke@1 | 478 | /** Is this the name of an archive file? */ |
jjg@106 | 479 | private boolean isArchive(File file) { |
duke@1 | 480 | String n = file.getName().toLowerCase(); |
jjg@106 | 481 | return fsInfo.isFile(file) |
duke@1 | 482 | && (n.endsWith(".jar") || n.endsWith(".zip")); |
duke@1 | 483 | } |
darcy@497 | 484 | |
darcy@497 | 485 | /** |
darcy@497 | 486 | * Utility method for converting a search path string to an array |
darcy@497 | 487 | * of directory and JAR file URLs. |
darcy@497 | 488 | * |
darcy@497 | 489 | * Note that this method is called by apt and the DocletInvoker. |
darcy@497 | 490 | * |
darcy@497 | 491 | * @param path the search path string |
darcy@497 | 492 | * @return the resulting array of directory and JAR file URLs |
darcy@497 | 493 | */ |
darcy@497 | 494 | public static URL[] pathToURLs(String path) { |
darcy@497 | 495 | StringTokenizer st = new StringTokenizer(path, File.pathSeparator); |
darcy@497 | 496 | URL[] urls = new URL[st.countTokens()]; |
darcy@497 | 497 | int count = 0; |
darcy@497 | 498 | while (st.hasMoreTokens()) { |
darcy@497 | 499 | URL url = fileToURL(new File(st.nextToken())); |
darcy@497 | 500 | if (url != null) { |
darcy@497 | 501 | urls[count++] = url; |
darcy@497 | 502 | } |
darcy@497 | 503 | } |
darcy@497 | 504 | if (urls.length != count) { |
darcy@497 | 505 | URL[] tmp = new URL[count]; |
darcy@497 | 506 | System.arraycopy(urls, 0, tmp, 0, count); |
darcy@497 | 507 | urls = tmp; |
darcy@497 | 508 | } |
darcy@497 | 509 | return urls; |
darcy@497 | 510 | } |
darcy@497 | 511 | |
darcy@497 | 512 | /** |
darcy@497 | 513 | * Returns the directory or JAR file URL corresponding to the specified |
darcy@497 | 514 | * local file name. |
darcy@497 | 515 | * |
darcy@497 | 516 | * @param file the File object |
darcy@497 | 517 | * @return the resulting directory or JAR file URL, or null if unknown |
darcy@497 | 518 | */ |
darcy@497 | 519 | private static URL fileToURL(File file) { |
darcy@497 | 520 | String name; |
darcy@497 | 521 | try { |
darcy@497 | 522 | name = file.getCanonicalPath(); |
darcy@497 | 523 | } catch (IOException e) { |
darcy@497 | 524 | name = file.getAbsolutePath(); |
darcy@497 | 525 | } |
darcy@497 | 526 | name = name.replace(File.separatorChar, '/'); |
darcy@497 | 527 | if (!name.startsWith("/")) { |
darcy@497 | 528 | name = "/" + name; |
darcy@497 | 529 | } |
darcy@497 | 530 | // If the file does not exist, then assume that it's a directory |
darcy@497 | 531 | if (!file.isFile()) { |
darcy@497 | 532 | name = name + "/"; |
darcy@497 | 533 | } |
darcy@497 | 534 | try { |
darcy@497 | 535 | return new URL("file", "", name); |
darcy@497 | 536 | } catch (MalformedURLException e) { |
darcy@497 | 537 | throw new IllegalArgumentException(file.toString()); |
darcy@497 | 538 | } |
darcy@497 | 539 | } |
duke@1 | 540 | } |