src/share/classes/com/sun/tools/javac/file/Locations.java

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

mercurial