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