langtools/src/share/classes/com/sun/tools/javac/file/Locations.java
author jjh
Tue, 25 Oct 2011 19:18:32 -0700
changeset 10819 e1a8df45d8b0
parent 10818 e95eb04c68cc
child 11314 b612aaca08d0
permissions -rw-r--r--
7104905: Java SE build fails on call to CreateSymbols Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
8034
d3b29ef9c062 6430241: Hard to disable symbol file feature through API
jjg
parents: 7847
diff changeset
     2
 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5007
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5007
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5007
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5007
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5007
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 171
diff changeset
    26
package com.sun.tools.javac.file;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 171
diff changeset
    27
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    28
import java.io.FileNotFoundException;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    29
import java.util.Iterator;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
4937
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
    32
import java.net.MalformedURLException;
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
    33
import java.net.URL;
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    34
import java.util.Arrays;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    35
import java.util.Collection;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    36
import java.util.Collections;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    37
import java.util.EnumMap;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    38
import java.util.EnumSet;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.util.HashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.util.HashSet;
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    41
import java.util.LinkedHashSet;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import java.util.Set;
4937
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
    44
import java.util.StringTokenizer;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import java.util.zip.ZipFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import javax.tools.JavaFileManager.Location;
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    47
import javax.tools.StandardLocation;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 171
diff changeset
    49
import com.sun.tools.javac.code.Lint;
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    50
import com.sun.tools.javac.main.OptionName;
1486
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
    51
import com.sun.tools.javac.util.ListBuffer;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 171
diff changeset
    52
import com.sun.tools.javac.util.Log;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 171
diff changeset
    53
import com.sun.tools.javac.util.Options;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 171
diff changeset
    54
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    55
import javax.tools.JavaFileManager;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 171
diff changeset
    56
import static javax.tools.StandardLocation.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import static com.sun.tools.javac.main.OptionName.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
/** This class converts command line arguments, environment variables
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *  and system properties (in File.pathSeparator-separated String form)
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *  into a boot class path, user class path, and source path (in
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *  Collection<String> form).
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    64
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    65
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 */
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    69
public class Locations {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    /** The log to use for warning output */
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    private Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    /** Collection of command-line options */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    private Options options;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /** Handler for -Xlint options */
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    private Lint lint;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
    80
    /** Access to (possibly cached) file info */
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
    81
    private FSInfo fsInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    83
    /** Whether to warn about non-existent path elements */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    84
    private boolean warn;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    85
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    86
    // TODO: remove need for this
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    87
    private boolean inited = false; // TODO? caching bad?
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    88
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    89
    public Locations() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
    90
        initHandlers();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
10813
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 8424
diff changeset
    93
    public void update(Log log, Options options, Lint lint, FSInfo fsInfo) {
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 8424
diff changeset
    94
        this.log = log;
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 8424
diff changeset
    95
        this.options = options;
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 8424
diff changeset
    96
        this.lint = lint;
95b39a692cd0 7101146: Paths should more directly managed by BaseFileManager
jjg
parents: 8424
diff changeset
    97
        this.fsInfo = fsInfo;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   100
    public Collection<File> bootClassPath() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   101
        return getLocation(PLATFORM_CLASS_PATH);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
7336
3fd30668e9b5 7003477: Paths.isDefaultBootClassPath needs to be public
jjg
parents: 7335
diff changeset
   104
    public boolean isDefaultBootClassPath() {
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   105
        BootClassPathLocationHandler h =
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   106
                (BootClassPathLocationHandler) getHandler(PLATFORM_CLASS_PATH);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   107
        return h.isDefault();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
8034
d3b29ef9c062 6430241: Hard to disable symbol file feature through API
jjg
parents: 7847
diff changeset
   110
    boolean isDefaultBootClassPathRtJar(File file) {
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   111
        BootClassPathLocationHandler h =
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   112
                (BootClassPathLocationHandler) getHandler(PLATFORM_CLASS_PATH);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   113
        return h.isDefaultRtJar(file);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   114
    }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   115
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   116
    public Collection<File> userClassPath() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   117
        return getLocation(CLASS_PATH);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   118
    }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   119
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   120
    public Collection<File> sourcePath() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   121
        Collection<File> p = getLocation(SOURCE_PATH);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   122
        // TODO: this should be handled by the LocationHandler
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   123
        return p == null || p.isEmpty() ? null : p;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
1486
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   126
    /**
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   127
     * Split a path into its elements. Empty path elements will be ignored.
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   128
     * @param path The path to be split
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   129
     * @return The elements of the path
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   130
     */
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   131
    private static Iterable<File> getPathEntries(String path) {
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   132
        return getPathEntries(path, null);
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   133
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
1486
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   135
    /**
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   136
     * Split a path into its elements. If emptyPathDefault is not null, all
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   137
     * empty elements in the path, including empty elements at either end of
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   138
     * the path, will be replaced with the value of emptyPathDefault.
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   139
     * @param path The path to be split
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   140
     * @param emptyPathDefault The value to substitute for empty path elements,
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   141
     *  or null, to ignore empty path elements
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   142
     * @return The elements of the path
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   143
     */
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   144
    private static Iterable<File> getPathEntries(String path, File emptyPathDefault) {
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   145
        ListBuffer<File> entries = new ListBuffer<File>();
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   146
        int start = 0;
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   147
        while (start <= path.length()) {
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   148
            int sep = path.indexOf(File.pathSeparatorChar, start);
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   149
            if (sep == -1)
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   150
                sep = path.length();
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   151
            if (start < sep)
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   152
                entries.add(new File(path.substring(start, sep)));
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   153
            else if (emptyPathDefault != null)
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   154
                entries.add(emptyPathDefault);
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   155
            start = sep + 1;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        }
1486
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   157
        return entries;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   160
    /**
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   161
     * Utility class to help evaluate a path option.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   162
     * Duplicate entries are ignored, jar class paths can be expanded.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   163
     */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    private class Path extends LinkedHashSet<File> {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        private static final long serialVersionUID = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        private boolean expandJarClassPaths = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        private Set<File> canonicalValues = new HashSet<File>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        public Path expandJarClassPaths(boolean x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            expandJarClassPaths = x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        /** What to use when path element is the empty string */
1486
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   176
        private File emptyPathDefault = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
1486
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   178
        public Path emptyPathDefault(File x) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
            emptyPathDefault = x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        public Path() { super(); }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        public Path addDirectories(String dirs, boolean warn) {
8424
5e31b2eb5aa3 6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents: 8034
diff changeset
   186
            boolean prev = expandJarClassPaths;
5e31b2eb5aa3 6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents: 8034
diff changeset
   187
            expandJarClassPaths = true;
5e31b2eb5aa3 6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents: 8034
diff changeset
   188
            try {
5e31b2eb5aa3 6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents: 8034
diff changeset
   189
                if (dirs != null)
5e31b2eb5aa3 6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents: 8034
diff changeset
   190
                    for (File dir : getPathEntries(dirs))
5e31b2eb5aa3 6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents: 8034
diff changeset
   191
                        addDirectory(dir, warn);
5e31b2eb5aa3 6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents: 8034
diff changeset
   192
                return this;
5e31b2eb5aa3 6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents: 8034
diff changeset
   193
            } finally {
5e31b2eb5aa3 6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents: 8034
diff changeset
   194
                expandJarClassPaths = prev;
5e31b2eb5aa3 6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents: 8034
diff changeset
   195
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        public Path addDirectories(String dirs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            return addDirectories(dirs, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
1486
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   202
        private void addDirectory(File dir, boolean warn) {
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   203
            if (!dir.isDirectory()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                if (warn)
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   205
                    log.warning(Lint.LintCategory.PATH,
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   206
                            "dir.path.element.not.found", dir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
1486
a7d1338ca96e 6759996: ignore empty entries on paths
jjg
parents: 1208
diff changeset
   210
            File[] files = dir.listFiles();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            if (files == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            for (File direntry : files) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                if (isArchive(direntry))
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                    addFile(direntry, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        public Path addFiles(String files, boolean warn) {
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 6151
diff changeset
   221
            if (files != null) {
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   222
                addFiles(getPathEntries(files, emptyPathDefault), warn);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   223
            }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   224
            return this;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   225
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   226
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   227
        public Path addFiles(String files) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   228
            return addFiles(files, warn);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   229
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   230
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   231
        public Path addFiles(Iterable<? extends File> files, boolean warn) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   232
            if (files != null) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   233
                for (File file: files)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                    addFile(file, warn);
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 6151
diff changeset
   235
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   239
        public Path addFiles(Iterable<? extends File> files) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            return addFiles(files, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        public void addFile(File file, boolean warn) {
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   244
            if (contains(file)) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   245
                // discard duplicates
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   249
            if (! fsInfo.exists(file)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
                /* No such file or directory exists */
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   251
                if (warn) {
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   252
                    log.warning(Lint.LintCategory.PATH,
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   253
                            "path.element.not.found", file);
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   254
                }
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   255
                super.add(file);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   256
                return;
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   257
            }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   258
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   259
            File canonFile = fsInfo.getCanonicalFile(file);
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   260
            if (canonicalValues.contains(canonFile)) {
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   261
                /* Discard duplicates and avoid infinite recursion */
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   262
                return;
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   263
            }
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   264
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   265
            if (fsInfo.isFile(file)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                /* File is an ordinary file. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                if (!isArchive(file)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                    /* Not a recognized extension; open it to see if
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                     it looks like a valid zip file. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                        ZipFile z = new ZipFile(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                        z.close();
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   273
                        if (warn) {
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   274
                            log.warning(Lint.LintCategory.PATH,
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   275
                                    "unexpected.archive.file", file);
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   276
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                    } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                        // FIXME: include e.getLocalizedMessage in warning
6151
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   279
                        if (warn) {
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   280
                            log.warning(Lint.LintCategory.PATH,
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   281
                                    "invalid.archive.file", file);
dd513881e71d 6957438: improve code for generating warning messages containing option names
jjg
parents: 5847
diff changeset
   282
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            /* Now what we have left is either a directory or a file name
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   289
               conforming to archive naming convention */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            super.add(file);
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   291
            canonicalValues.add(canonFile);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
7839
a1ca72d05b20 6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents: 7336
diff changeset
   293
            if (expandJarClassPaths && fsInfo.isFile(file))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                addJarClassPath(file, warn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        // Adds referenced classpath elements from a jar's Class-Path
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        // Manifest entry.  In some future release, we may want to
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        // update this code to recognize URLs rather than simple
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        // filenames, but if we do, we should redo all path-related code.
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        private void addJarClassPath(File jarFile, boolean warn) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            try {
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   303
                for (File f: fsInfo.getJarClassPath(jarFile)) {
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   304
                    addFile(f, warn);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
            } catch (IOException e) {
5007
28dee2489196 6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents: 4937
diff changeset
   307
                log.error("error.reading.file", jarFile, JavacFileManager.getMessage(e));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   312
    /**
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   313
     * Base class for handling support for the representation of Locations.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   314
     * Implementations are responsible for handling the interactions between
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   315
     * the command line options for a location, and API access via setLocation.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   316
     * @see #initHandlers
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   317
     * @see #getHandler
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   318
     */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   319
    protected abstract class LocationHandler {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   320
        final Location location;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   321
        final Set<OptionName> options;
7335
8b390fd27190 6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents: 6151
diff changeset
   322
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   323
        /**
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   324
         * Create a handler. The location and options provide a way to map
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   325
         * from a location or an option to the corresponding handler.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   326
         * @see #initHandlers
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   327
         */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   328
        protected LocationHandler(Location location, OptionName... options) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   329
            this.location = location;
10819
e1a8df45d8b0 7104905: Java SE build fails on call to CreateSymbols
jjh
parents: 10818
diff changeset
   330
            this.options = options.length == 0 ?
e1a8df45d8b0 7104905: Java SE build fails on call to CreateSymbols
jjh
parents: 10818
diff changeset
   331
                EnumSet.noneOf(OptionName.class):
e1a8df45d8b0 7104905: Java SE build fails on call to CreateSymbols
jjh
parents: 10818
diff changeset
   332
                EnumSet.copyOf(Arrays.asList(options));
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   333
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   335
        // TODO: TEMPORARY, while Options still used for command line options
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   336
        void update(Options optionTable) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   337
            for (OptionName o: options) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   338
                String v = optionTable.get(o);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   339
                if (v != null) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   340
                    handleOption(o, v);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   341
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   345
        /** @see JavaFileManager#handleOption. */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   346
        abstract boolean handleOption(OptionName option, String value);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   347
        /** @see JavaFileManager#getLocation. */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   348
        abstract Collection<File> getLocation();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   349
        /** @see JavaFileManager#setLocation. */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   350
        abstract void setLocation(Iterable<? extends File> files) throws IOException;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   351
    }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   352
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   353
    /**
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   354
     * General purpose implementation for output locations,
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   355
     * such as -d/CLASS_OUTPUT and -s/SOURCE_OUTPUT.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   356
     * All options are treated as equivalent (i.e. aliases.)
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   357
     * The value is a single file, possibly null.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   358
     */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   359
    private class OutputLocationHandler extends LocationHandler {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   360
        private File outputDir;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   361
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   362
        OutputLocationHandler(Location location, OptionName... options) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   363
            super(location, options);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   364
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   365
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   366
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   367
        boolean handleOption(OptionName option, String value) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   368
            if (!options.contains(option))
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   369
                return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   371
            // TODO: could/should validate outputDir exists and is a directory
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   372
            // need to decide how best to report issue for benefit of
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   373
            // direct API call on JavaFileManager.handleOption(specifies IAE)
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   374
            // vs. command line decoding.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   375
            outputDir = new File(value);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   376
            return true;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   377
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   378
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   379
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   380
        Collection<File> getLocation() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   381
            return (outputDir == null) ? null : Collections.singleton(outputDir);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   382
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   384
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   385
        void setLocation(Iterable<? extends File> files) throws IOException {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   386
            if (files == null) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   387
                outputDir = null;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   388
            } else {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   389
                Iterator<? extends File> pathIter = files.iterator();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   390
                if (!pathIter.hasNext())
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   391
                    throw new IllegalArgumentException("empty path for directory");
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   392
                File dir = pathIter.next();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   393
                if (pathIter.hasNext())
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   394
                    throw new IllegalArgumentException("path too long for directory");
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   395
                if (!dir.exists())
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   396
                    throw new FileNotFoundException(dir + ": does not exist");
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   397
                else if (!dir.isDirectory())
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   398
                    throw new IOException(dir + ": not a directory");
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   399
                outputDir = dir;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   400
            }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   401
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   404
    /**
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   405
     * General purpose implementation for search path locations,
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   406
     * such as -sourcepath/SOURCE_PATH and -processorPath/ANNOTATION_PROCESS_PATH.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   407
     * All options are treated as equivalent (i.e. aliases.)
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   408
     * The value is an ordered set of files and/or directories.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   409
     */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   410
    private class SimpleLocationHandler extends LocationHandler {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   411
        protected Collection<File> searchPath;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   413
        SimpleLocationHandler(Location location, OptionName... options) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   414
            super(location, options);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   415
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   417
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   418
        boolean handleOption(OptionName option, String value) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   419
            if (!options.contains(option))
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   420
                return false;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   421
            searchPath = value == null ? null :
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   422
                    Collections.unmodifiableCollection(computePath(value));
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   423
            return true;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   424
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   425
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   426
        protected Path computePath(String value) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   427
            return new Path().addFiles(value);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   428
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   430
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   431
        Collection<File> getLocation() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   432
            return searchPath;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   433
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   435
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   436
        void setLocation(Iterable<? extends File> files) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   437
            Path p;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   438
            if (files == null) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   439
                p = computePath(null);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   440
            } else {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   441
                p = new Path().addFiles(files);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   442
            }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   443
            searchPath = Collections.unmodifiableCollection(p);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   444
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   447
    /**
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   448
     * Subtype of SimpleLocationHandler for -classpath/CLASS_PATH.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   449
     * If no value is given, a default is provided, based on system properties
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   450
     * and other values.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   451
     */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   452
    private class ClassPathLocationHandler extends SimpleLocationHandler {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   453
        ClassPathLocationHandler() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   454
            super(StandardLocation.CLASS_PATH,
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   455
                    OptionName.CLASSPATH, OptionName.CP);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   456
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   457
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   458
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   459
        Collection<File> getLocation() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   460
            lazy();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   461
            return searchPath;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   462
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   463
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   464
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   465
        protected Path computePath(String value) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   466
            String cp = value;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   468
            // CLASSPATH environment variable when run from `javac'.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   469
            if (cp == null) cp = System.getProperty("env.class.path");
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   470
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   471
            // If invoked via a java VM (not the javac launcher), use the
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   472
            // platform class path
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   473
            if (cp == null && System.getProperty("application.home") == null)
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   474
                cp = System.getProperty("java.class.path");
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   475
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   476
            // Default to current working directory.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   477
            if (cp == null) cp = ".";
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   478
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   479
            return new Path()
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   480
                .expandJarClassPaths(true)        // Only search user jars for Class-Paths
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   481
                .emptyPathDefault(new File("."))  // Empty path elt ==> current directory
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   482
                .addFiles(cp);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   483
            }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   484
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   485
        private void lazy() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   486
            if (searchPath == null)
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   487
                setLocation(null);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   488
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   491
    /**
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   492
     * Custom subtype of LocationHandler for PLATFORM_CLASS_PATH.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   493
     * Various options are supported for different components of the
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   494
     * platform class path.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   495
     * Setting a value with setLocation overrides all existing option values.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   496
     * Setting any option overrides any value set with setLocation, and reverts
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   497
     * to using default values for options that have not been set.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   498
     * Setting -bootclasspath or -Xbootclasspath overrides any existing
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   499
     * value for -Xbootclasspath/p: and -Xbootclasspath/a:.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   500
     */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   501
    private class BootClassPathLocationHandler extends LocationHandler {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   502
        private Collection<File> searchPath;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   503
        final Map<OptionName, String> optionValues = new EnumMap<OptionName,String>(OptionName.class);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   504
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   505
        /**
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   506
         * rt.jar as found on the default bootclasspath.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   507
         * If the user specified a bootclasspath, null is used.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   508
         */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   509
        private File defaultBootClassPathRtJar = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   511
        /**
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   512
         *  Is bootclasspath the default?
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   513
         */
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   514
        private boolean isDefaultBootClassPath;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   515
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   516
        BootClassPathLocationHandler() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   517
            super(StandardLocation.PLATFORM_CLASS_PATH,
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   518
                    OptionName.BOOTCLASSPATH, OptionName.XBOOTCLASSPATH,
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   519
                    OptionName.XBOOTCLASSPATH_PREPEND,
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   520
                    OptionName.XBOOTCLASSPATH_APPEND,
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   521
                    OptionName.ENDORSEDDIRS, OptionName.DJAVA_ENDORSED_DIRS,
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   522
                    OptionName.EXTDIRS, OptionName.DJAVA_EXT_DIRS);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   523
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   524
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   525
        boolean isDefault() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   526
            lazy();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   527
            return isDefaultBootClassPath;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   528
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   529
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   530
        boolean isDefaultRtJar(File file) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   531
            lazy();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   532
            return file.equals(defaultBootClassPathRtJar);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   533
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   534
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   535
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   536
        boolean handleOption(OptionName option, String value) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   537
            if (!options.contains(option))
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   538
                return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   540
            option = canonicalize(option);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   541
            optionValues.put(option, value);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   542
            if (option == BOOTCLASSPATH) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   543
                optionValues.remove(XBOOTCLASSPATH_PREPEND);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   544
                optionValues.remove(XBOOTCLASSPATH_APPEND);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   545
            }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   546
            searchPath = null;  // reset to "uninitialized"
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   547
            return true;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   548
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   549
        // where
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   550
            // TODO: would be better if option aliasing was handled at a higher
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   551
            // level
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   552
            private OptionName canonicalize(OptionName option) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   553
                switch (option) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   554
                    case XBOOTCLASSPATH:
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   555
                        return OptionName.BOOTCLASSPATH;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   556
                    case DJAVA_ENDORSED_DIRS:
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   557
                        return OptionName.ENDORSEDDIRS;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   558
                    case DJAVA_EXT_DIRS:
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   559
                        return OptionName.EXTDIRS;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   560
                    default:
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   561
                        return option;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   562
                }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   563
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   565
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   566
        Collection<File> getLocation() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
            lazy();
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   568
            return searchPath;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   569
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   570
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   571
        @Override
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   572
        void setLocation(Iterable<? extends File> files) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   573
            if (files == null) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   574
                searchPath = null;  // reset to "uninitialized"
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   575
            } else {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   576
                defaultBootClassPathRtJar = null;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   577
                isDefaultBootClassPath = false;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   578
                Path p = new Path().addFiles(files, false);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   579
                searchPath = Collections.unmodifiableCollection(p);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   580
                optionValues.clear();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   581
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
        }
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   583
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   584
        Path computePath() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   585
            defaultBootClassPathRtJar = null;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   586
            Path path = new Path();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   587
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   588
            String bootclasspathOpt = optionValues.get(BOOTCLASSPATH);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   589
            String endorseddirsOpt = optionValues.get(ENDORSEDDIRS);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   590
            String extdirsOpt = optionValues.get(EXTDIRS);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   591
            String xbootclasspathPrependOpt = optionValues.get(XBOOTCLASSPATH_PREPEND);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   592
            String xbootclasspathAppendOpt = optionValues.get(XBOOTCLASSPATH_APPEND);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   593
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   594
            path.addFiles(xbootclasspathPrependOpt);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   595
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   596
            if (endorseddirsOpt != null)
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   597
                path.addDirectories(endorseddirsOpt);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   598
            else
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   599
                path.addDirectories(System.getProperty("java.endorsed.dirs"), false);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   600
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   601
            if (bootclasspathOpt != null) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   602
                path.addFiles(bootclasspathOpt);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   603
            } else {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   604
                // Standard system classes for this compiler's release.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   605
                String files = System.getProperty("sun.boot.class.path");
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   606
                path.addFiles(files, false);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   607
                File rt_jar = new File("rt.jar");
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   608
                for (File file : getPathEntries(files)) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   609
                    if (new File(file.getName()).equals(rt_jar))
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   610
                        defaultBootClassPathRtJar = file;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   611
                }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   612
            }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   613
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   614
            path.addFiles(xbootclasspathAppendOpt);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   615
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   616
            // Strictly speaking, standard extensions are not bootstrap
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   617
            // classes, but we treat them identically, so we'll pretend
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   618
            // that they are.
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   619
            if (extdirsOpt != null)
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   620
                path.addDirectories(extdirsOpt);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   621
            else
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   622
                path.addDirectories(System.getProperty("java.ext.dirs"), false);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   623
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   624
            isDefaultBootClassPath =
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   625
                    (xbootclasspathPrependOpt == null) &&
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   626
                    (bootclasspathOpt == null) &&
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   627
                    (xbootclasspathAppendOpt == null);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   628
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   629
            return path;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   630
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   631
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   632
        private void lazy() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   633
            if (searchPath == null)
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   634
                searchPath = Collections.unmodifiableCollection(computePath());
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   635
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   638
    Map<Location, LocationHandler> handlersForLocation;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   639
    Map<OptionName, LocationHandler> handlersForOption;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   640
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   641
    void initHandlers() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   642
        handlersForLocation = new HashMap<Location, LocationHandler>();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   643
        handlersForOption = new EnumMap<OptionName, LocationHandler>(OptionName.class);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   644
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   645
        LocationHandler[] handlers = {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   646
            new BootClassPathLocationHandler(),
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   647
            new ClassPathLocationHandler(),
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   648
            new SimpleLocationHandler(StandardLocation.SOURCE_PATH, OptionName.SOURCEPATH),
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   649
            new SimpleLocationHandler(StandardLocation.ANNOTATION_PROCESSOR_PATH, OptionName.PROCESSORPATH),
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   650
            new OutputLocationHandler((StandardLocation.CLASS_OUTPUT), OptionName.D),
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   651
            new OutputLocationHandler((StandardLocation.SOURCE_OUTPUT), OptionName.S)
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   652
        };
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   654
        for (LocationHandler h: handlers) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   655
            handlersForLocation.put(h.location, h);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   656
            for (OptionName o: h.options)
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   657
                handlersForOption.put(o, h);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
        }
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   659
    }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   660
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   661
    boolean handleOption(OptionName option, String value) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   662
        LocationHandler h = handlersForOption.get(option);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   663
        return (h == null ? false : h.handleOption(option, value));
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   664
    }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   665
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   666
    Collection<File> getLocation(Location location) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   667
        LocationHandler h = getHandler(location);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   668
        return (h == null ? null : h.getLocation());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   671
    File getOutputLocation(Location location) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   672
        if (!location.isOutputLocation())
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   673
            throw new IllegalArgumentException();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   674
        LocationHandler h = getHandler(location);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   675
        return ((OutputLocationHandler) h).outputDir;
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   676
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   678
    void setLocation(Location location, Iterable<? extends File> files) throws IOException {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   679
        LocationHandler h = getHandler(location);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   680
        if (h == null) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   681
            if (location.isOutputLocation())
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   682
                h = new OutputLocationHandler(location);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   683
            else
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   684
                h = new SimpleLocationHandler(location);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   685
            handlersForLocation.put(location, h);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   686
        }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   687
        h.setLocation(files);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   688
    }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   689
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   690
    protected LocationHandler getHandler(Location location) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   691
        location.getClass(); // null check
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   692
        lazy();
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   693
        return handlersForLocation.get(location);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   694
    }
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   695
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   696
// TOGO
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   697
    protected void lazy() {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   698
        if (!inited) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   699
            warn = lint.isEnabled(Lint.LintCategory.PATH);
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   700
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   701
            for (LocationHandler h: handlersForLocation.values()) {
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   702
                h.update(options);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
            }
10818
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   704
e95eb04c68cc 7104039: refactor/cleanup javac Paths class
jjg
parents: 10813
diff changeset
   705
            inited = true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
    /** Is this the name of an archive file? */
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   710
    private boolean isArchive(File file) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
        String n = file.getName().toLowerCase();
1208
5072b0dd3d52 6743107: clean up use of static caches in file manager
jjg
parents: 735
diff changeset
   712
        return fsInfo.isFile(file)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
            && (n.endsWith(".jar") || n.endsWith(".zip"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
    }
4937
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   715
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   716
    /**
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   717
     * Utility method for converting a search path string to an array
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   718
     * of directory and JAR file URLs.
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   719
     *
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   720
     * Note that this method is called by apt and the DocletInvoker.
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   721
     *
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   722
     * @param path the search path string
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   723
     * @return the resulting array of directory and JAR file URLs
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   724
     */
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   725
    public static URL[] pathToURLs(String path) {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   726
        StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   727
        URL[] urls = new URL[st.countTokens()];
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   728
        int count = 0;
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   729
        while (st.hasMoreTokens()) {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   730
            URL url = fileToURL(new File(st.nextToken()));
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   731
            if (url != null) {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   732
                urls[count++] = url;
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   733
            }
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   734
        }
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   735
        if (urls.length != count) {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   736
            URL[] tmp = new URL[count];
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   737
            System.arraycopy(urls, 0, tmp, 0, count);
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   738
            urls = tmp;
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   739
        }
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   740
        return urls;
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   741
    }
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   742
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   743
    /**
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   744
     * Returns the directory or JAR file URL corresponding to the specified
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   745
     * local file name.
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   746
     *
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   747
     * @param file the File object
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   748
     * @return the resulting directory or JAR file URL, or null if unknown
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   749
     */
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   750
    private static URL fileToURL(File file) {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   751
        String name;
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   752
        try {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   753
            name = file.getCanonicalPath();
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   754
        } catch (IOException e) {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   755
            name = file.getAbsolutePath();
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   756
        }
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   757
        name = name.replace(File.separatorChar, '/');
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   758
        if (!name.startsWith("/")) {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   759
            name = "/" + name;
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   760
        }
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   761
        // If the file does not exist, then assume that it's a directory
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   762
        if (!file.isFile()) {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   763
            name = name + "/";
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   764
        }
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   765
        try {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   766
            return new URL("file", "", name);
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   767
        } catch (MalformedURLException e) {
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   768
            throw new IllegalArgumentException(file.toString());
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   769
        }
2fc03fb01efa 6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents: 4548
diff changeset
   770
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
}