1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/file/Locations.java Tue Oct 25 10:48:05 2011 -0700 1.3 @@ -0,0 +1,769 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.file; 1.30 + 1.31 +import java.io.FileNotFoundException; 1.32 +import java.util.Iterator; 1.33 +import java.io.File; 1.34 +import java.io.IOException; 1.35 +import java.net.MalformedURLException; 1.36 +import java.net.URL; 1.37 +import java.util.Arrays; 1.38 +import java.util.Collection; 1.39 +import java.util.Collections; 1.40 +import java.util.EnumMap; 1.41 +import java.util.EnumSet; 1.42 +import java.util.HashMap; 1.43 +import java.util.HashSet; 1.44 +import java.util.LinkedHashSet; 1.45 +import java.util.Map; 1.46 +import java.util.Set; 1.47 +import java.util.StringTokenizer; 1.48 +import java.util.zip.ZipFile; 1.49 +import javax.tools.JavaFileManager.Location; 1.50 +import javax.tools.StandardLocation; 1.51 + 1.52 +import com.sun.tools.javac.code.Lint; 1.53 +import com.sun.tools.javac.main.OptionName; 1.54 +import com.sun.tools.javac.util.ListBuffer; 1.55 +import com.sun.tools.javac.util.Log; 1.56 +import com.sun.tools.javac.util.Options; 1.57 + 1.58 +import javax.tools.JavaFileManager; 1.59 +import static javax.tools.StandardLocation.*; 1.60 +import static com.sun.tools.javac.main.OptionName.*; 1.61 + 1.62 +/** This class converts command line arguments, environment variables 1.63 + * and system properties (in File.pathSeparator-separated String form) 1.64 + * into a boot class path, user class path, and source path (in 1.65 + * Collection<String> form). 1.66 + * 1.67 + * <p><b>This is NOT part of any supported API. 1.68 + * If you write code that depends on this, you do so at your own risk. 1.69 + * This code and its internal interfaces are subject to change or 1.70 + * deletion without notice.</b> 1.71 + */ 1.72 +public class Locations { 1.73 + 1.74 + /** The log to use for warning output */ 1.75 + private Log log; 1.76 + 1.77 + /** Collection of command-line options */ 1.78 + private Options options; 1.79 + 1.80 + /** Handler for -Xlint options */ 1.81 + private Lint lint; 1.82 + 1.83 + /** Access to (possibly cached) file info */ 1.84 + private FSInfo fsInfo; 1.85 + 1.86 + /** Whether to warn about non-existent path elements */ 1.87 + private boolean warn; 1.88 + 1.89 + // TODO: remove need for this 1.90 + private boolean inited = false; // TODO? caching bad? 1.91 + 1.92 + public Locations() { 1.93 + initHandlers(); 1.94 + } 1.95 + 1.96 + public void update(Log log, Options options, Lint lint, FSInfo fsInfo) { 1.97 + this.log = log; 1.98 + this.options = options; 1.99 + this.lint = lint; 1.100 + this.fsInfo = fsInfo; 1.101 + } 1.102 + 1.103 + public Collection<File> bootClassPath() { 1.104 + return getLocation(PLATFORM_CLASS_PATH); 1.105 + } 1.106 + 1.107 + public boolean isDefaultBootClassPath() { 1.108 + BootClassPathLocationHandler h = 1.109 + (BootClassPathLocationHandler) getHandler(PLATFORM_CLASS_PATH); 1.110 + return h.isDefault(); 1.111 + } 1.112 + 1.113 + boolean isDefaultBootClassPathRtJar(File file) { 1.114 + BootClassPathLocationHandler h = 1.115 + (BootClassPathLocationHandler) getHandler(PLATFORM_CLASS_PATH); 1.116 + return h.isDefaultRtJar(file); 1.117 + } 1.118 + 1.119 + public Collection<File> userClassPath() { 1.120 + return getLocation(CLASS_PATH); 1.121 + } 1.122 + 1.123 + public Collection<File> sourcePath() { 1.124 + Collection<File> p = getLocation(SOURCE_PATH); 1.125 + // TODO: this should be handled by the LocationHandler 1.126 + return p == null || p.isEmpty() ? null : p; 1.127 + } 1.128 + 1.129 + /** 1.130 + * Split a path into its elements. Empty path elements will be ignored. 1.131 + * @param path The path to be split 1.132 + * @return The elements of the path 1.133 + */ 1.134 + private static Iterable<File> getPathEntries(String path) { 1.135 + return getPathEntries(path, null); 1.136 + } 1.137 + 1.138 + /** 1.139 + * Split a path into its elements. If emptyPathDefault is not null, all 1.140 + * empty elements in the path, including empty elements at either end of 1.141 + * the path, will be replaced with the value of emptyPathDefault. 1.142 + * @param path The path to be split 1.143 + * @param emptyPathDefault The value to substitute for empty path elements, 1.144 + * or null, to ignore empty path elements 1.145 + * @return The elements of the path 1.146 + */ 1.147 + private static Iterable<File> getPathEntries(String path, File emptyPathDefault) { 1.148 + ListBuffer<File> entries = new ListBuffer<File>(); 1.149 + int start = 0; 1.150 + while (start <= path.length()) { 1.151 + int sep = path.indexOf(File.pathSeparatorChar, start); 1.152 + if (sep == -1) 1.153 + sep = path.length(); 1.154 + if (start < sep) 1.155 + entries.add(new File(path.substring(start, sep))); 1.156 + else if (emptyPathDefault != null) 1.157 + entries.add(emptyPathDefault); 1.158 + start = sep + 1; 1.159 + } 1.160 + return entries; 1.161 + } 1.162 + 1.163 + /** 1.164 + * Utility class to help evaluate a path option. 1.165 + * Duplicate entries are ignored, jar class paths can be expanded. 1.166 + */ 1.167 + private class Path extends LinkedHashSet<File> { 1.168 + private static final long serialVersionUID = 0; 1.169 + 1.170 + private boolean expandJarClassPaths = false; 1.171 + private Set<File> canonicalValues = new HashSet<File>(); 1.172 + 1.173 + public Path expandJarClassPaths(boolean x) { 1.174 + expandJarClassPaths = x; 1.175 + return this; 1.176 + } 1.177 + 1.178 + /** What to use when path element is the empty string */ 1.179 + private File emptyPathDefault = null; 1.180 + 1.181 + public Path emptyPathDefault(File x) { 1.182 + emptyPathDefault = x; 1.183 + return this; 1.184 + } 1.185 + 1.186 + public Path() { super(); } 1.187 + 1.188 + public Path addDirectories(String dirs, boolean warn) { 1.189 + boolean prev = expandJarClassPaths; 1.190 + expandJarClassPaths = true; 1.191 + try { 1.192 + if (dirs != null) 1.193 + for (File dir : getPathEntries(dirs)) 1.194 + addDirectory(dir, warn); 1.195 + return this; 1.196 + } finally { 1.197 + expandJarClassPaths = prev; 1.198 + } 1.199 + } 1.200 + 1.201 + public Path addDirectories(String dirs) { 1.202 + return addDirectories(dirs, warn); 1.203 + } 1.204 + 1.205 + private void addDirectory(File dir, boolean warn) { 1.206 + if (!dir.isDirectory()) { 1.207 + if (warn) 1.208 + log.warning(Lint.LintCategory.PATH, 1.209 + "dir.path.element.not.found", dir); 1.210 + return; 1.211 + } 1.212 + 1.213 + File[] files = dir.listFiles(); 1.214 + if (files == null) 1.215 + return; 1.216 + 1.217 + for (File direntry : files) { 1.218 + if (isArchive(direntry)) 1.219 + addFile(direntry, warn); 1.220 + } 1.221 + } 1.222 + 1.223 + public Path addFiles(String files, boolean warn) { 1.224 + if (files != null) { 1.225 + addFiles(getPathEntries(files, emptyPathDefault), warn); 1.226 + } 1.227 + return this; 1.228 + } 1.229 + 1.230 + public Path addFiles(String files) { 1.231 + return addFiles(files, warn); 1.232 + } 1.233 + 1.234 + public Path addFiles(Iterable<? extends File> files, boolean warn) { 1.235 + if (files != null) { 1.236 + for (File file: files) 1.237 + addFile(file, warn); 1.238 + } 1.239 + return this; 1.240 + } 1.241 + 1.242 + public Path addFiles(Iterable<? extends File> files) { 1.243 + return addFiles(files, warn); 1.244 + } 1.245 + 1.246 + public void addFile(File file, boolean warn) { 1.247 + if (contains(file)) { 1.248 + // discard duplicates 1.249 + return; 1.250 + } 1.251 + 1.252 + if (! fsInfo.exists(file)) { 1.253 + /* No such file or directory exists */ 1.254 + if (warn) { 1.255 + log.warning(Lint.LintCategory.PATH, 1.256 + "path.element.not.found", file); 1.257 + } 1.258 + super.add(file); 1.259 + return; 1.260 + } 1.261 + 1.262 + File canonFile = fsInfo.getCanonicalFile(file); 1.263 + if (canonicalValues.contains(canonFile)) { 1.264 + /* Discard duplicates and avoid infinite recursion */ 1.265 + return; 1.266 + } 1.267 + 1.268 + if (fsInfo.isFile(file)) { 1.269 + /* File is an ordinary file. */ 1.270 + if (!isArchive(file)) { 1.271 + /* Not a recognized extension; open it to see if 1.272 + it looks like a valid zip file. */ 1.273 + try { 1.274 + ZipFile z = new ZipFile(file); 1.275 + z.close(); 1.276 + if (warn) { 1.277 + log.warning(Lint.LintCategory.PATH, 1.278 + "unexpected.archive.file", file); 1.279 + } 1.280 + } catch (IOException e) { 1.281 + // FIXME: include e.getLocalizedMessage in warning 1.282 + if (warn) { 1.283 + log.warning(Lint.LintCategory.PATH, 1.284 + "invalid.archive.file", file); 1.285 + } 1.286 + return; 1.287 + } 1.288 + } 1.289 + } 1.290 + 1.291 + /* Now what we have left is either a directory or a file name 1.292 + conforming to archive naming convention */ 1.293 + super.add(file); 1.294 + canonicalValues.add(canonFile); 1.295 + 1.296 + if (expandJarClassPaths && fsInfo.isFile(file)) 1.297 + addJarClassPath(file, warn); 1.298 + } 1.299 + 1.300 + // Adds referenced classpath elements from a jar's Class-Path 1.301 + // Manifest entry. In some future release, we may want to 1.302 + // update this code to recognize URLs rather than simple 1.303 + // filenames, but if we do, we should redo all path-related code. 1.304 + private void addJarClassPath(File jarFile, boolean warn) { 1.305 + try { 1.306 + for (File f: fsInfo.getJarClassPath(jarFile)) { 1.307 + addFile(f, warn); 1.308 + } 1.309 + } catch (IOException e) { 1.310 + log.error("error.reading.file", jarFile, JavacFileManager.getMessage(e)); 1.311 + } 1.312 + } 1.313 + } 1.314 + 1.315 + /** 1.316 + * Base class for handling support for the representation of Locations. 1.317 + * Implementations are responsible for handling the interactions between 1.318 + * the command line options for a location, and API access via setLocation. 1.319 + * @see #initHandlers 1.320 + * @see #getHandler 1.321 + */ 1.322 + protected abstract class LocationHandler { 1.323 + final Location location; 1.324 + final Set<OptionName> options; 1.325 + 1.326 + /** 1.327 + * Create a handler. The location and options provide a way to map 1.328 + * from a location or an option to the corresponding handler. 1.329 + * @see #initHandlers 1.330 + */ 1.331 + protected LocationHandler(Location location, OptionName... options) { 1.332 + this.location = location; 1.333 + this.options = EnumSet.copyOf(Arrays.asList(options)); 1.334 + } 1.335 + 1.336 + // TODO: TEMPORARY, while Options still used for command line options 1.337 + void update(Options optionTable) { 1.338 + for (OptionName o: options) { 1.339 + String v = optionTable.get(o); 1.340 + if (v != null) { 1.341 + handleOption(o, v); 1.342 + } 1.343 + } 1.344 + } 1.345 + 1.346 + /** @see JavaFileManager#handleOption. */ 1.347 + abstract boolean handleOption(OptionName option, String value); 1.348 + /** @see JavaFileManager#getLocation. */ 1.349 + abstract Collection<File> getLocation(); 1.350 + /** @see JavaFileManager#setLocation. */ 1.351 + abstract void setLocation(Iterable<? extends File> files) throws IOException; 1.352 + } 1.353 + 1.354 + /** 1.355 + * General purpose implementation for output locations, 1.356 + * such as -d/CLASS_OUTPUT and -s/SOURCE_OUTPUT. 1.357 + * All options are treated as equivalent (i.e. aliases.) 1.358 + * The value is a single file, possibly null. 1.359 + */ 1.360 + private class OutputLocationHandler extends LocationHandler { 1.361 + private File outputDir; 1.362 + 1.363 + OutputLocationHandler(Location location, OptionName... options) { 1.364 + super(location, options); 1.365 + } 1.366 + 1.367 + @Override 1.368 + boolean handleOption(OptionName option, String value) { 1.369 + if (!options.contains(option)) 1.370 + return false; 1.371 + 1.372 + // TODO: could/should validate outputDir exists and is a directory 1.373 + // need to decide how best to report issue for benefit of 1.374 + // direct API call on JavaFileManager.handleOption(specifies IAE) 1.375 + // vs. command line decoding. 1.376 + outputDir = new File(value); 1.377 + return true; 1.378 + } 1.379 + 1.380 + @Override 1.381 + Collection<File> getLocation() { 1.382 + return (outputDir == null) ? null : Collections.singleton(outputDir); 1.383 + } 1.384 + 1.385 + @Override 1.386 + void setLocation(Iterable<? extends File> files) throws IOException { 1.387 + if (files == null) { 1.388 + outputDir = null; 1.389 + } else { 1.390 + Iterator<? extends File> pathIter = files.iterator(); 1.391 + if (!pathIter.hasNext()) 1.392 + throw new IllegalArgumentException("empty path for directory"); 1.393 + File dir = pathIter.next(); 1.394 + if (pathIter.hasNext()) 1.395 + throw new IllegalArgumentException("path too long for directory"); 1.396 + if (!dir.exists()) 1.397 + throw new FileNotFoundException(dir + ": does not exist"); 1.398 + else if (!dir.isDirectory()) 1.399 + throw new IOException(dir + ": not a directory"); 1.400 + outputDir = dir; 1.401 + } 1.402 + } 1.403 + } 1.404 + 1.405 + /** 1.406 + * General purpose implementation for search path locations, 1.407 + * such as -sourcepath/SOURCE_PATH and -processorPath/ANNOTATION_PROCESS_PATH. 1.408 + * All options are treated as equivalent (i.e. aliases.) 1.409 + * The value is an ordered set of files and/or directories. 1.410 + */ 1.411 + private class SimpleLocationHandler extends LocationHandler { 1.412 + protected Collection<File> searchPath; 1.413 + 1.414 + SimpleLocationHandler(Location location, OptionName... options) { 1.415 + super(location, options); 1.416 + } 1.417 + 1.418 + @Override 1.419 + boolean handleOption(OptionName option, String value) { 1.420 + if (!options.contains(option)) 1.421 + return false; 1.422 + searchPath = value == null ? null : 1.423 + Collections.unmodifiableCollection(computePath(value)); 1.424 + return true; 1.425 + } 1.426 + 1.427 + protected Path computePath(String value) { 1.428 + return new Path().addFiles(value); 1.429 + } 1.430 + 1.431 + @Override 1.432 + Collection<File> getLocation() { 1.433 + return searchPath; 1.434 + } 1.435 + 1.436 + @Override 1.437 + void setLocation(Iterable<? extends File> files) { 1.438 + Path p; 1.439 + if (files == null) { 1.440 + p = computePath(null); 1.441 + } else { 1.442 + p = new Path().addFiles(files); 1.443 + } 1.444 + searchPath = Collections.unmodifiableCollection(p); 1.445 + } 1.446 + } 1.447 + 1.448 + /** 1.449 + * Subtype of SimpleLocationHandler for -classpath/CLASS_PATH. 1.450 + * If no value is given, a default is provided, based on system properties 1.451 + * and other values. 1.452 + */ 1.453 + private class ClassPathLocationHandler extends SimpleLocationHandler { 1.454 + ClassPathLocationHandler() { 1.455 + super(StandardLocation.CLASS_PATH, 1.456 + OptionName.CLASSPATH, OptionName.CP); 1.457 + } 1.458 + 1.459 + @Override 1.460 + Collection<File> getLocation() { 1.461 + lazy(); 1.462 + return searchPath; 1.463 + } 1.464 + 1.465 + @Override 1.466 + protected Path computePath(String value) { 1.467 + String cp = value; 1.468 + 1.469 + // CLASSPATH environment variable when run from `javac'. 1.470 + if (cp == null) cp = System.getProperty("env.class.path"); 1.471 + 1.472 + // If invoked via a java VM (not the javac launcher), use the 1.473 + // platform class path 1.474 + if (cp == null && System.getProperty("application.home") == null) 1.475 + cp = System.getProperty("java.class.path"); 1.476 + 1.477 + // Default to current working directory. 1.478 + if (cp == null) cp = "."; 1.479 + 1.480 + return new Path() 1.481 + .expandJarClassPaths(true) // Only search user jars for Class-Paths 1.482 + .emptyPathDefault(new File(".")) // Empty path elt ==> current directory 1.483 + .addFiles(cp); 1.484 + } 1.485 + 1.486 + private void lazy() { 1.487 + if (searchPath == null) 1.488 + setLocation(null); 1.489 + } 1.490 + } 1.491 + 1.492 + /** 1.493 + * Custom subtype of LocationHandler for PLATFORM_CLASS_PATH. 1.494 + * Various options are supported for different components of the 1.495 + * platform class path. 1.496 + * Setting a value with setLocation overrides all existing option values. 1.497 + * Setting any option overrides any value set with setLocation, and reverts 1.498 + * to using default values for options that have not been set. 1.499 + * Setting -bootclasspath or -Xbootclasspath overrides any existing 1.500 + * value for -Xbootclasspath/p: and -Xbootclasspath/a:. 1.501 + */ 1.502 + private class BootClassPathLocationHandler extends LocationHandler { 1.503 + private Collection<File> searchPath; 1.504 + final Map<OptionName, String> optionValues = new EnumMap<OptionName,String>(OptionName.class); 1.505 + 1.506 + /** 1.507 + * rt.jar as found on the default bootclasspath. 1.508 + * If the user specified a bootclasspath, null is used. 1.509 + */ 1.510 + private File defaultBootClassPathRtJar = null; 1.511 + 1.512 + /** 1.513 + * Is bootclasspath the default? 1.514 + */ 1.515 + private boolean isDefaultBootClassPath; 1.516 + 1.517 + BootClassPathLocationHandler() { 1.518 + super(StandardLocation.PLATFORM_CLASS_PATH, 1.519 + OptionName.BOOTCLASSPATH, OptionName.XBOOTCLASSPATH, 1.520 + OptionName.XBOOTCLASSPATH_PREPEND, 1.521 + OptionName.XBOOTCLASSPATH_APPEND, 1.522 + OptionName.ENDORSEDDIRS, OptionName.DJAVA_ENDORSED_DIRS, 1.523 + OptionName.EXTDIRS, OptionName.DJAVA_EXT_DIRS); 1.524 + } 1.525 + 1.526 + boolean isDefault() { 1.527 + lazy(); 1.528 + return isDefaultBootClassPath; 1.529 + } 1.530 + 1.531 + boolean isDefaultRtJar(File file) { 1.532 + lazy(); 1.533 + return file.equals(defaultBootClassPathRtJar); 1.534 + } 1.535 + 1.536 + @Override 1.537 + boolean handleOption(OptionName option, String value) { 1.538 + if (!options.contains(option)) 1.539 + return false; 1.540 + 1.541 + option = canonicalize(option); 1.542 + optionValues.put(option, value); 1.543 + if (option == BOOTCLASSPATH) { 1.544 + optionValues.remove(XBOOTCLASSPATH_PREPEND); 1.545 + optionValues.remove(XBOOTCLASSPATH_APPEND); 1.546 + } 1.547 + searchPath = null; // reset to "uninitialized" 1.548 + return true; 1.549 + } 1.550 + // where 1.551 + // TODO: would be better if option aliasing was handled at a higher 1.552 + // level 1.553 + private OptionName canonicalize(OptionName option) { 1.554 + switch (option) { 1.555 + case XBOOTCLASSPATH: 1.556 + return OptionName.BOOTCLASSPATH; 1.557 + case DJAVA_ENDORSED_DIRS: 1.558 + return OptionName.ENDORSEDDIRS; 1.559 + case DJAVA_EXT_DIRS: 1.560 + return OptionName.EXTDIRS; 1.561 + default: 1.562 + return option; 1.563 + } 1.564 + } 1.565 + 1.566 + @Override 1.567 + Collection<File> getLocation() { 1.568 + lazy(); 1.569 + return searchPath; 1.570 + } 1.571 + 1.572 + @Override 1.573 + void setLocation(Iterable<? extends File> files) { 1.574 + if (files == null) { 1.575 + searchPath = null; // reset to "uninitialized" 1.576 + } else { 1.577 + defaultBootClassPathRtJar = null; 1.578 + isDefaultBootClassPath = false; 1.579 + Path p = new Path().addFiles(files, false); 1.580 + searchPath = Collections.unmodifiableCollection(p); 1.581 + optionValues.clear(); 1.582 + } 1.583 + } 1.584 + 1.585 + Path computePath() { 1.586 + defaultBootClassPathRtJar = null; 1.587 + Path path = new Path(); 1.588 + 1.589 + String bootclasspathOpt = optionValues.get(BOOTCLASSPATH); 1.590 + String endorseddirsOpt = optionValues.get(ENDORSEDDIRS); 1.591 + String extdirsOpt = optionValues.get(EXTDIRS); 1.592 + String xbootclasspathPrependOpt = optionValues.get(XBOOTCLASSPATH_PREPEND); 1.593 + String xbootclasspathAppendOpt = optionValues.get(XBOOTCLASSPATH_APPEND); 1.594 + 1.595 + path.addFiles(xbootclasspathPrependOpt); 1.596 + 1.597 + if (endorseddirsOpt != null) 1.598 + path.addDirectories(endorseddirsOpt); 1.599 + else 1.600 + path.addDirectories(System.getProperty("java.endorsed.dirs"), false); 1.601 + 1.602 + if (bootclasspathOpt != null) { 1.603 + path.addFiles(bootclasspathOpt); 1.604 + } else { 1.605 + // Standard system classes for this compiler's release. 1.606 + String files = System.getProperty("sun.boot.class.path"); 1.607 + path.addFiles(files, false); 1.608 + File rt_jar = new File("rt.jar"); 1.609 + for (File file : getPathEntries(files)) { 1.610 + if (new File(file.getName()).equals(rt_jar)) 1.611 + defaultBootClassPathRtJar = file; 1.612 + } 1.613 + } 1.614 + 1.615 + path.addFiles(xbootclasspathAppendOpt); 1.616 + 1.617 + // Strictly speaking, standard extensions are not bootstrap 1.618 + // classes, but we treat them identically, so we'll pretend 1.619 + // that they are. 1.620 + if (extdirsOpt != null) 1.621 + path.addDirectories(extdirsOpt); 1.622 + else 1.623 + path.addDirectories(System.getProperty("java.ext.dirs"), false); 1.624 + 1.625 + isDefaultBootClassPath = 1.626 + (xbootclasspathPrependOpt == null) && 1.627 + (bootclasspathOpt == null) && 1.628 + (xbootclasspathAppendOpt == null); 1.629 + 1.630 + return path; 1.631 + } 1.632 + 1.633 + private void lazy() { 1.634 + if (searchPath == null) 1.635 + searchPath = Collections.unmodifiableCollection(computePath()); 1.636 + } 1.637 + } 1.638 + 1.639 + Map<Location, LocationHandler> handlersForLocation; 1.640 + Map<OptionName, LocationHandler> handlersForOption; 1.641 + 1.642 + void initHandlers() { 1.643 + handlersForLocation = new HashMap<Location, LocationHandler>(); 1.644 + handlersForOption = new EnumMap<OptionName, LocationHandler>(OptionName.class); 1.645 + 1.646 + LocationHandler[] handlers = { 1.647 + new BootClassPathLocationHandler(), 1.648 + new ClassPathLocationHandler(), 1.649 + new SimpleLocationHandler(StandardLocation.SOURCE_PATH, OptionName.SOURCEPATH), 1.650 + new SimpleLocationHandler(StandardLocation.ANNOTATION_PROCESSOR_PATH, OptionName.PROCESSORPATH), 1.651 + new OutputLocationHandler((StandardLocation.CLASS_OUTPUT), OptionName.D), 1.652 + new OutputLocationHandler((StandardLocation.SOURCE_OUTPUT), OptionName.S) 1.653 + }; 1.654 + 1.655 + for (LocationHandler h: handlers) { 1.656 + handlersForLocation.put(h.location, h); 1.657 + for (OptionName o: h.options) 1.658 + handlersForOption.put(o, h); 1.659 + } 1.660 + } 1.661 + 1.662 + boolean handleOption(OptionName option, String value) { 1.663 + LocationHandler h = handlersForOption.get(option); 1.664 + return (h == null ? false : h.handleOption(option, value)); 1.665 + } 1.666 + 1.667 + Collection<File> getLocation(Location location) { 1.668 + LocationHandler h = getHandler(location); 1.669 + return (h == null ? null : h.getLocation()); 1.670 + } 1.671 + 1.672 + File getOutputLocation(Location location) { 1.673 + if (!location.isOutputLocation()) 1.674 + throw new IllegalArgumentException(); 1.675 + LocationHandler h = getHandler(location); 1.676 + return ((OutputLocationHandler) h).outputDir; 1.677 + } 1.678 + 1.679 + void setLocation(Location location, Iterable<? extends File> files) throws IOException { 1.680 + LocationHandler h = getHandler(location); 1.681 + if (h == null) { 1.682 + if (location.isOutputLocation()) 1.683 + h = new OutputLocationHandler(location); 1.684 + else 1.685 + h = new SimpleLocationHandler(location); 1.686 + handlersForLocation.put(location, h); 1.687 + } 1.688 + h.setLocation(files); 1.689 + } 1.690 + 1.691 + protected LocationHandler getHandler(Location location) { 1.692 + location.getClass(); // null check 1.693 + lazy(); 1.694 + return handlersForLocation.get(location); 1.695 + } 1.696 + 1.697 +// TOGO 1.698 + protected void lazy() { 1.699 + if (!inited) { 1.700 + warn = lint.isEnabled(Lint.LintCategory.PATH); 1.701 + 1.702 + for (LocationHandler h: handlersForLocation.values()) { 1.703 + h.update(options); 1.704 + } 1.705 + 1.706 + inited = true; 1.707 + } 1.708 + } 1.709 + 1.710 + /** Is this the name of an archive file? */ 1.711 + private boolean isArchive(File file) { 1.712 + String n = file.getName().toLowerCase(); 1.713 + return fsInfo.isFile(file) 1.714 + && (n.endsWith(".jar") || n.endsWith(".zip")); 1.715 + } 1.716 + 1.717 + /** 1.718 + * Utility method for converting a search path string to an array 1.719 + * of directory and JAR file URLs. 1.720 + * 1.721 + * Note that this method is called by apt and the DocletInvoker. 1.722 + * 1.723 + * @param path the search path string 1.724 + * @return the resulting array of directory and JAR file URLs 1.725 + */ 1.726 + public static URL[] pathToURLs(String path) { 1.727 + StringTokenizer st = new StringTokenizer(path, File.pathSeparator); 1.728 + URL[] urls = new URL[st.countTokens()]; 1.729 + int count = 0; 1.730 + while (st.hasMoreTokens()) { 1.731 + URL url = fileToURL(new File(st.nextToken())); 1.732 + if (url != null) { 1.733 + urls[count++] = url; 1.734 + } 1.735 + } 1.736 + if (urls.length != count) { 1.737 + URL[] tmp = new URL[count]; 1.738 + System.arraycopy(urls, 0, tmp, 0, count); 1.739 + urls = tmp; 1.740 + } 1.741 + return urls; 1.742 + } 1.743 + 1.744 + /** 1.745 + * Returns the directory or JAR file URL corresponding to the specified 1.746 + * local file name. 1.747 + * 1.748 + * @param file the File object 1.749 + * @return the resulting directory or JAR file URL, or null if unknown 1.750 + */ 1.751 + private static URL fileToURL(File file) { 1.752 + String name; 1.753 + try { 1.754 + name = file.getCanonicalPath(); 1.755 + } catch (IOException e) { 1.756 + name = file.getAbsolutePath(); 1.757 + } 1.758 + name = name.replace(File.separatorChar, '/'); 1.759 + if (!name.startsWith("/")) { 1.760 + name = "/" + name; 1.761 + } 1.762 + // If the file does not exist, then assume that it's a directory 1.763 + if (!file.isFile()) { 1.764 + name = name + "/"; 1.765 + } 1.766 + try { 1.767 + return new URL("file", "", name); 1.768 + } catch (MalformedURLException e) { 1.769 + throw new IllegalArgumentException(file.toString()); 1.770 + } 1.771 + } 1.772 +}