author | jjg |
Wed, 10 Aug 2016 15:47:46 -0700 | |
changeset 40308 | 274367a99f98 |
parent 40302 | 8c0d8d2c3519 |
child 40513 | 39b67170b045 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
36526 | 2 |
* Copyright (c) 2003, 2016, 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 |
*/ |
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
29780
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 |
|
36526 | 28 |
import java.io.Closeable; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
29 |
import java.io.File; |
10818 | 30 |
import java.io.FileNotFoundException; |
10 | 31 |
import java.io.IOException; |
27579 | 32 |
import java.io.UncheckedIOException; |
36526 | 33 |
import java.net.URI; |
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4548
diff
changeset
|
34 |
import java.net.URL; |
36526 | 35 |
import java.net.URLClassLoader; |
36 |
import java.nio.file.DirectoryIteratorException; |
|
37 |
import java.nio.file.DirectoryStream; |
|
38 |
import java.nio.file.FileSystem; |
|
39 |
import java.nio.file.FileSystemNotFoundException; |
|
40 |
import java.nio.file.FileSystems; |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
41 |
import java.nio.file.Files; |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
42 |
import java.nio.file.Path; |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
43 |
import java.nio.file.Paths; |
36526 | 44 |
import java.nio.file.ProviderNotFoundException; |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
45 |
import java.util.ArrayList; |
10818 | 46 |
import java.util.Arrays; |
47 |
import java.util.Collection; |
|
48 |
import java.util.Collections; |
|
49 |
import java.util.EnumMap; |
|
50 |
import java.util.EnumSet; |
|
10 | 51 |
import java.util.HashMap; |
52 |
import java.util.HashSet; |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
53 |
import java.util.Iterator; |
36526 | 54 |
import java.util.LinkedHashMap; |
10818 | 55 |
import java.util.LinkedHashSet; |
36526 | 56 |
import java.util.List; |
10 | 57 |
import java.util.Map; |
29291
076c277565f7
8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents:
29053
diff
changeset
|
58 |
import java.util.Objects; |
36526 | 59 |
import java.util.NoSuchElementException; |
10 | 60 |
import java.util.Set; |
36526 | 61 |
import java.util.regex.Matcher; |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
62 |
import java.util.regex.Pattern; |
27579 | 63 |
import java.util.stream.Collectors; |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
64 |
import java.util.stream.Stream; |
10 | 65 |
import java.util.zip.ZipFile; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
66 |
|
36526 | 67 |
import javax.lang.model.SourceVersion; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
68 |
import javax.tools.JavaFileManager; |
10 | 69 |
import javax.tools.JavaFileManager.Location; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
70 |
import javax.tools.StandardJavaFileManager; |
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
71 |
import javax.tools.StandardJavaFileManager.PathFactory; |
10818 | 72 |
import javax.tools.StandardLocation; |
10 | 73 |
|
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
171
diff
changeset
|
74 |
import com.sun.tools.javac.code.Lint; |
11314 | 75 |
import com.sun.tools.javac.main.Option; |
36526 | 76 |
import com.sun.tools.javac.resources.CompilerProperties.Errors; |
77 |
import com.sun.tools.javac.resources.CompilerProperties.Warnings; |
|
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
78 |
import com.sun.tools.javac.util.DefinedBy; |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
79 |
import com.sun.tools.javac.util.DefinedBy.Api; |
1486 | 80 |
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
|
81 |
import com.sun.tools.javac.util.Log; |
36526 | 82 |
import com.sun.tools.javac.util.Pair; |
22153
f9f06fcca59d
8029800: Flags.java uses String.toLowerCase without specifying Locale
jlahoda
parents:
14360
diff
changeset
|
83 |
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
|
84 |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
85 |
import static javax.tools.StandardLocation.PLATFORM_CLASS_PATH; |
10 | 86 |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
87 |
import static com.sun.tools.javac.main.Option.BOOT_CLASS_PATH; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
88 |
import static com.sun.tools.javac.main.Option.DJAVA_ENDORSED_DIRS; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
89 |
import static com.sun.tools.javac.main.Option.DJAVA_EXT_DIRS; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
90 |
import static com.sun.tools.javac.main.Option.ENDORSEDDIRS; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
91 |
import static com.sun.tools.javac.main.Option.EXTDIRS; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
92 |
import static com.sun.tools.javac.main.Option.XBOOTCLASSPATH; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
93 |
import static com.sun.tools.javac.main.Option.XBOOTCLASSPATH_APPEND; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
94 |
import static com.sun.tools.javac.main.Option.XBOOTCLASSPATH_PREPEND; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
95 |
|
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
96 |
/** |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
97 |
* This class converts command line arguments, environment variables and system properties (in |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
98 |
* File.pathSeparator-separated String form) into a boot class path, user class path, and source |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
99 |
* path (in {@code Collection<String>} form). |
10 | 100 |
* |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
101 |
* <p> |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
102 |
* <b>This is NOT part of any supported API. If you write code that depends on this, you do so at |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
103 |
* your own risk. This code and its internal interfaces are subject to change or deletion without |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
104 |
* notice.</b> |
10 | 105 |
*/ |
10818 | 106 |
public class Locations { |
10 | 107 |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
108 |
/** |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
109 |
* The log to use for warning output |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
110 |
*/ |
10 | 111 |
private Log log; |
112 |
||
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
113 |
/** |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
114 |
* Access to (possibly cached) file info |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
115 |
*/ |
1208 | 116 |
private FSInfo fsInfo; |
10 | 117 |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
118 |
/** |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
119 |
* Whether to warn about non-existent path elements |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
120 |
*/ |
10818 | 121 |
private boolean warn; |
122 |
||
36526 | 123 |
private ModuleNameReader moduleNameReader; |
124 |
||
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
125 |
private PathFactory pathFactory = Paths::get; |
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
126 |
|
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
127 |
static final Path javaHome = FileSystems.getDefault().getPath(System.getProperty("java.home")); |
36526 | 128 |
static final Path thisSystemModules = javaHome.resolve("lib").resolve("modules"); |
129 |
||
130 |
Map<Path, FileSystem> fileSystems = new LinkedHashMap<>(); |
|
131 |
List<Closeable> closeables = new ArrayList<>(); |
|
27579 | 132 |
|
29780
8f8e54a1fa20
8076420: Consolidate javac file handling in javac.file package
jjg
parents:
29291
diff
changeset
|
133 |
Locations() { |
10818 | 134 |
initHandlers(); |
10 | 135 |
} |
136 |
||
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
137 |
Path getPath(String first, String... more) { |
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
138 |
return pathFactory.getPath(first, more); |
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
139 |
} |
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
140 |
|
36526 | 141 |
public void close() throws IOException { |
142 |
ListBuffer<IOException> list = new ListBuffer<>(); |
|
143 |
closeables.forEach(closeable -> { |
|
144 |
try { |
|
145 |
closeable.close(); |
|
146 |
} catch (IOException ex) { |
|
147 |
list.add(ex); |
|
148 |
} |
|
149 |
}); |
|
150 |
if (list.nonEmpty()) { |
|
151 |
IOException ex = new IOException(); |
|
152 |
for (IOException e: list) |
|
153 |
ex.addSuppressed(e); |
|
154 |
throw ex; |
|
155 |
} |
|
156 |
} |
|
157 |
||
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
158 |
// could replace Lint by "boolean warn" |
29780
8f8e54a1fa20
8076420: Consolidate javac file handling in javac.file package
jjg
parents:
29291
diff
changeset
|
159 |
void update(Log log, Lint lint, FSInfo fsInfo) { |
10813
95b39a692cd0
7101146: Paths should more directly managed by BaseFileManager
jjg
parents:
8424
diff
changeset
|
160 |
this.log = log; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
161 |
warn = lint.isEnabled(Lint.LintCategory.PATH); |
10813
95b39a692cd0
7101146: Paths should more directly managed by BaseFileManager
jjg
parents:
8424
diff
changeset
|
162 |
this.fsInfo = fsInfo; |
10 | 163 |
} |
164 |
||
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
165 |
void setPathFactory(PathFactory f) { |
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
166 |
pathFactory = f; |
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
167 |
} |
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
168 |
|
29780
8f8e54a1fa20
8076420: Consolidate javac file handling in javac.file package
jjg
parents:
29291
diff
changeset
|
169 |
boolean isDefaultBootClassPath() { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
170 |
BootClassPathLocationHandler h |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
171 |
= (BootClassPathLocationHandler) getHandler(PLATFORM_CLASS_PATH); |
10818 | 172 |
return h.isDefault(); |
10 | 173 |
} |
174 |
||
1486 | 175 |
/** |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
176 |
* Split a search path into its elements. Empty path elements will be ignored. |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
177 |
* |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
178 |
* @param searchPath The search path to be split |
1486 | 179 |
* @return The elements of the path |
180 |
*/ |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
181 |
private Iterable<Path> getPathEntries(String searchPath) { |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
182 |
return getPathEntries(searchPath, null); |
1486 | 183 |
} |
10 | 184 |
|
1486 | 185 |
/** |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
186 |
* Split a search path into its elements. If emptyPathDefault is not null, all empty elements in the |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
187 |
* path, including empty elements at either end of the path, will be replaced with the value of |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
188 |
* emptyPathDefault. |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
189 |
* |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
190 |
* @param searchPath The search path to be split |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
191 |
* @param emptyPathDefault The value to substitute for empty path elements, or null, to ignore |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
192 |
* empty path elements |
1486 | 193 |
* @return The elements of the path |
194 |
*/ |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
195 |
private Iterable<Path> getPathEntries(String searchPath, Path emptyPathDefault) { |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
196 |
ListBuffer<Path> entries = new ListBuffer<>(); |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
197 |
for (String s: searchPath.split(Pattern.quote(File.pathSeparator), -1)) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
198 |
if (s.isEmpty()) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
199 |
if (emptyPathDefault != null) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
200 |
entries.add(emptyPathDefault); |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
201 |
} |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
202 |
} else { |
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
203 |
entries.add(getPath(s)); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
204 |
} |
10 | 205 |
} |
1486 | 206 |
return entries; |
10 | 207 |
} |
208 |
||
10818 | 209 |
/** |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
210 |
* Utility class to help evaluate a path option. Duplicate entries are ignored, jar class paths |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
211 |
* can be expanded. |
10818 | 212 |
*/ |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
213 |
private class SearchPath extends LinkedHashSet<Path> { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
214 |
|
10 | 215 |
private static final long serialVersionUID = 0; |
216 |
||
217 |
private boolean expandJarClassPaths = false; |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
218 |
private final Set<Path> canonicalValues = new HashSet<>(); |
10 | 219 |
|
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
220 |
public SearchPath expandJarClassPaths(boolean x) { |
10 | 221 |
expandJarClassPaths = x; |
222 |
return this; |
|
223 |
} |
|
224 |
||
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
225 |
/** |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
226 |
* What to use when path element is the empty string |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
227 |
*/ |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
228 |
private Path emptyPathDefault = null; |
10 | 229 |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
230 |
public SearchPath emptyPathDefault(Path x) { |
10 | 231 |
emptyPathDefault = x; |
232 |
return this; |
|
233 |
} |
|
234 |
||
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
235 |
public SearchPath addDirectories(String dirs, boolean warn) { |
8424
5e31b2eb5aa3
6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents:
8034
diff
changeset
|
236 |
boolean prev = expandJarClassPaths; |
5e31b2eb5aa3
6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents:
8034
diff
changeset
|
237 |
expandJarClassPaths = true; |
5e31b2eb5aa3
6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents:
8034
diff
changeset
|
238 |
try { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
239 |
if (dirs != null) { |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
240 |
for (Path dir : getPathEntries(dirs)) { |
8424
5e31b2eb5aa3
6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents:
8034
diff
changeset
|
241 |
addDirectory(dir, warn); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
242 |
} |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
243 |
} |
8424
5e31b2eb5aa3
6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents:
8034
diff
changeset
|
244 |
return this; |
5e31b2eb5aa3
6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents:
8034
diff
changeset
|
245 |
} finally { |
5e31b2eb5aa3
6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents:
8034
diff
changeset
|
246 |
expandJarClassPaths = prev; |
5e31b2eb5aa3
6485027: javac incorrectly handles relative paths in manifest classpath
jjg
parents:
8034
diff
changeset
|
247 |
} |
10 | 248 |
} |
249 |
||
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
250 |
public SearchPath addDirectories(String dirs) { |
10 | 251 |
return addDirectories(dirs, warn); |
252 |
} |
|
253 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
254 |
private void addDirectory(Path dir, boolean warn) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
255 |
if (!Files.isDirectory(dir)) { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
256 |
if (warn) { |
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
257 |
log.warning(Lint.LintCategory.PATH, |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
258 |
"dir.path.element.not.found", dir); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
259 |
} |
10 | 260 |
return; |
261 |
} |
|
262 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
263 |
try (Stream<Path> s = Files.list(dir)) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
264 |
s.filter(dirEntry -> isArchive(dirEntry)) |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
265 |
.forEach(dirEntry -> addFile(dirEntry, warn)); |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
266 |
} catch (IOException ignore) { |
10 | 267 |
} |
268 |
} |
|
269 |
||
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
270 |
public SearchPath 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
|
271 |
if (files != null) { |
10818 | 272 |
addFiles(getPathEntries(files, emptyPathDefault), warn); |
273 |
} |
|
274 |
return this; |
|
275 |
} |
|
276 |
||
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
277 |
public SearchPath addFiles(String files) { |
10818 | 278 |
return addFiles(files, warn); |
279 |
} |
|
280 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
281 |
public SearchPath addFiles(Iterable<? extends Path> files, boolean warn) { |
10818 | 282 |
if (files != null) { |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
283 |
for (Path file : files) { |
10 | 284 |
addFile(file, warn); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
285 |
} |
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
6151
diff
changeset
|
286 |
} |
10 | 287 |
return this; |
288 |
} |
|
289 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
290 |
public SearchPath addFiles(Iterable<? extends Path> files) { |
10 | 291 |
return addFiles(files, warn); |
292 |
} |
|
293 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
294 |
public void addFile(Path 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
|
295 |
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
|
296 |
// discard duplicates |
10 | 297 |
return; |
298 |
} |
|
299 |
||
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
300 |
if (!fsInfo.exists(file)) { |
10 | 301 |
/* No such file or directory exists */ |
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
302 |
if (warn) { |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
303 |
log.warning(Lint.LintCategory.PATH, |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
304 |
"path.element.not.found", file); |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
305 |
} |
7839
a1ca72d05b20
6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents:
7336
diff
changeset
|
306 |
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
|
307 |
return; |
a1ca72d05b20
6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents:
7336
diff
changeset
|
308 |
} |
a1ca72d05b20
6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents:
7336
diff
changeset
|
309 |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
310 |
Path canonFile = fsInfo.getCanonicalFile(file); |
7839
a1ca72d05b20
6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents:
7336
diff
changeset
|
311 |
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
|
312 |
/* 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
|
313 |
return; |
a1ca72d05b20
6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents:
7336
diff
changeset
|
314 |
} |
a1ca72d05b20
6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents:
7336
diff
changeset
|
315 |
|
a1ca72d05b20
6999460: Glassfish build with JDK 6 / 7 is 5x-10x slower on Windows than on Linux
jjh
parents:
7336
diff
changeset
|
316 |
if (fsInfo.isFile(file)) { |
10 | 317 |
/* File is an ordinary file. */ |
36526 | 318 |
if (!isArchive(file) |
319 |
&& !file.getFileName().toString().endsWith(".jmod") |
|
320 |
&& !file.endsWith("modules")) { |
|
10 | 321 |
/* Not a recognized extension; open it to see if |
322 |
it looks like a valid zip file. */ |
|
323 |
try { |
|
36526 | 324 |
// TODO: use of ZipFile should be updated |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
325 |
ZipFile z = new ZipFile(file.toFile()); |
10 | 326 |
z.close(); |
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
327 |
if (warn) { |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
328 |
log.warning(Lint.LintCategory.PATH, |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
329 |
"unexpected.archive.file", file); |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
330 |
} |
10 | 331 |
} catch (IOException e) { |
332 |
// FIXME: include e.getLocalizedMessage in warning |
|
6151
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
333 |
if (warn) { |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
334 |
log.warning(Lint.LintCategory.PATH, |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
335 |
"invalid.archive.file", file); |
dd513881e71d
6957438: improve code for generating warning messages containing option names
jjg
parents:
5847
diff
changeset
|
336 |
} |
10 | 337 |
return; |
338 |
} |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
/* Now what we have left is either a directory or a file name |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
343 |
conforming to archive naming convention */ |
10 | 344 |
super.add(file); |
1208 | 345 |
canonicalValues.add(canonFile); |
10 | 346 |
|
36526 | 347 |
if (expandJarClassPaths && fsInfo.isFile(file) && !file.endsWith("modules")) { |
10 | 348 |
addJarClassPath(file, warn); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
349 |
} |
10 | 350 |
} |
351 |
||
352 |
// Adds referenced classpath elements from a jar's Class-Path |
|
353 |
// Manifest entry. In some future release, we may want to |
|
354 |
// update this code to recognize URLs rather than simple |
|
355 |
// filenames, but if we do, we should redo all path-related code. |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
356 |
private void addJarClassPath(Path jarFile, boolean warn) { |
10 | 357 |
try { |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
358 |
for (Path f : fsInfo.getJarClassPath(jarFile)) { |
1208 | 359 |
addFile(f, warn); |
10 | 360 |
} |
361 |
} catch (IOException e) { |
|
5007
28dee2489196
6930076: "null" can incorrectly appear in error message compiler.err.error.reading.file
jjg
parents:
4937
diff
changeset
|
362 |
log.error("error.reading.file", jarFile, JavacFileManager.getMessage(e)); |
10 | 363 |
} |
364 |
} |
|
365 |
} |
|
366 |
||
10818 | 367 |
/** |
36526 | 368 |
* Base class for handling support for the representation of Locations. |
369 |
* |
|
370 |
* Locations are (by design) opaque handles that can easily be implemented |
|
371 |
* by enums like StandardLocation. Within JavacFileManager, each Location |
|
372 |
* has an associated LocationHandler, which provides much of the appropriate |
|
373 |
* functionality for the corresponding Location. |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
374 |
* |
10818 | 375 |
* @see #initHandlers |
376 |
* @see #getHandler |
|
377 |
*/ |
|
378 |
protected abstract class LocationHandler { |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
379 |
|
36526 | 380 |
/** |
381 |
* @see JavaFileManager#handleOption |
|
382 |
*/ |
|
383 |
abstract boolean handleOption(Option option, String value); |
|
384 |
||
385 |
/** |
|
386 |
* @see StandardJavaFileManager#hasLocation |
|
387 |
*/ |
|
388 |
boolean isSet() { |
|
389 |
return (getPaths() != null); |
|
390 |
} |
|
391 |
||
392 |
/** |
|
393 |
* @see StandardJavaFileManager#getLocation |
|
394 |
*/ |
|
395 |
abstract Collection<Path> getPaths(); |
|
396 |
||
397 |
/** |
|
398 |
* @see StandardJavaFileManager#setLocation |
|
399 |
*/ |
|
400 |
abstract void setPaths(Iterable<? extends Path> files) throws IOException; |
|
401 |
||
402 |
/** |
|
403 |
* @see JavaFileManager#getModuleLocation(Location, String) |
|
404 |
*/ |
|
405 |
Location getModuleLocation(String moduleName) throws IOException { |
|
406 |
return null; |
|
407 |
} |
|
408 |
||
409 |
/** |
|
410 |
* @see JavaFileManager#getModuleLocation(Location, JavaFileObject, String) |
|
411 |
*/ |
|
412 |
Location getModuleLocation(Path dir) { |
|
413 |
return null; |
|
414 |
} |
|
415 |
||
416 |
/** |
|
417 |
* @see JavaFileManager#inferModuleName |
|
418 |
*/ |
|
419 |
String inferModuleName() { |
|
420 |
return null; |
|
421 |
} |
|
422 |
||
423 |
/** |
|
424 |
* @see JavaFileManager#listModuleLocations |
|
425 |
*/ |
|
426 |
Iterable<Set<Location>> listModuleLocations() throws IOException { |
|
427 |
return null; |
|
428 |
} |
|
429 |
} |
|
430 |
||
431 |
/** |
|
432 |
* A LocationHandler for a given Location, and associated set of options. |
|
433 |
*/ |
|
434 |
private abstract class BasicLocationHandler extends LocationHandler { |
|
435 |
||
10818 | 436 |
final Location location; |
11314 | 437 |
final Set<Option> options; |
7335
8b390fd27190
6900037: javac should warn if earlier -source is used and bootclasspath not set
jjg
parents:
6151
diff
changeset
|
438 |
|
10818 | 439 |
/** |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
440 |
* Create a handler. The location and options provide a way to map from a location or an |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
441 |
* option to the corresponding handler. |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
442 |
* |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
443 |
* @param location the location for which this is the handler |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
444 |
* @param options the options affecting this location |
10818 | 445 |
* @see #initHandlers |
446 |
*/ |
|
36526 | 447 |
protected BasicLocationHandler(Location location, Option... options) { |
10818 | 448 |
this.location = location; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
449 |
this.options = options.length == 0 |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
450 |
? EnumSet.noneOf(Option.class) |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
451 |
: EnumSet.copyOf(Arrays.asList(options)); |
10818 | 452 |
} |
453 |
} |
|
454 |
||
455 |
/** |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
456 |
* General purpose implementation for output locations, such as -d/CLASS_OUTPUT and |
36526 | 457 |
* -s/SOURCE_OUTPUT. All options are treated as equivalent (i.e. aliases.) |
458 |
* The value is a single file, possibly null. |
|
10818 | 459 |
*/ |
36526 | 460 |
private class OutputLocationHandler extends BasicLocationHandler { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
461 |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
462 |
private Path outputDir; |
36526 | 463 |
private Map<String, Location> moduleLocations; |
10818 | 464 |
|
11314 | 465 |
OutputLocationHandler(Location location, Option... options) { |
10818 | 466 |
super(location, options); |
467 |
} |
|
468 |
||
469 |
@Override |
|
11314 | 470 |
boolean handleOption(Option option, String value) { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
471 |
if (!options.contains(option)) { |
10818 | 472 |
return false; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
473 |
} |
10 | 474 |
|
10818 | 475 |
// TODO: could/should validate outputDir exists and is a directory |
476 |
// need to decide how best to report issue for benefit of |
|
477 |
// direct API call on JavaFileManager.handleOption(specifies IAE) |
|
478 |
// vs. command line decoding. |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
479 |
outputDir = (value == null) ? null : getPath(value); |
10818 | 480 |
return true; |
481 |
} |
|
482 |
||
483 |
@Override |
|
36526 | 484 |
Collection<Path> getPaths() { |
10818 | 485 |
return (outputDir == null) ? null : Collections.singleton(outputDir); |
486 |
} |
|
10 | 487 |
|
10818 | 488 |
@Override |
36526 | 489 |
void setPaths(Iterable<? extends Path> files) throws IOException { |
10818 | 490 |
if (files == null) { |
491 |
outputDir = null; |
|
492 |
} else { |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
493 |
Iterator<? extends Path> pathIter = files.iterator(); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
494 |
if (!pathIter.hasNext()) { |
10818 | 495 |
throw new IllegalArgumentException("empty path for directory"); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
496 |
} |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
497 |
Path dir = pathIter.next(); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
498 |
if (pathIter.hasNext()) { |
10818 | 499 |
throw new IllegalArgumentException("path too long for directory"); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
500 |
} |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
501 |
if (!Files.exists(dir)) { |
10818 | 502 |
throw new FileNotFoundException(dir + ": does not exist"); |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
503 |
} else if (!Files.isDirectory(dir)) { |
10818 | 504 |
throw new IOException(dir + ": not a directory"); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
505 |
} |
10818 | 506 |
outputDir = dir; |
507 |
} |
|
36526 | 508 |
moduleLocations = null; |
509 |
} |
|
510 |
||
511 |
@Override |
|
512 |
Location getModuleLocation(String name) { |
|
513 |
if (moduleLocations == null) |
|
514 |
moduleLocations = new HashMap<>(); |
|
515 |
Location l = moduleLocations.get(name); |
|
516 |
if (l == null) { |
|
517 |
l = new ModuleLocationHandler(location.getName() + "[" + name + "]", |
|
518 |
name, |
|
519 |
Collections.singleton(outputDir.resolve(name)), |
|
520 |
true, false); |
|
521 |
moduleLocations.put(name, l); |
|
522 |
} |
|
523 |
return l; |
|
10818 | 524 |
} |
10 | 525 |
} |
526 |
||
10818 | 527 |
/** |
36526 | 528 |
* General purpose implementation for search path locations, |
529 |
* such as -sourcepath/SOURCE_PATH and -processorPath/ANNOTATION_PROCESSOR_PATH. |
|
530 |
* All options are treated as equivalent (i.e. aliases.) |
|
10818 | 531 |
* The value is an ordered set of files and/or directories. |
532 |
*/ |
|
36526 | 533 |
private class SimpleLocationHandler extends BasicLocationHandler { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
534 |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
535 |
protected Collection<Path> searchPath; |
10 | 536 |
|
11314 | 537 |
SimpleLocationHandler(Location location, Option... options) { |
10818 | 538 |
super(location, options); |
539 |
} |
|
10 | 540 |
|
10818 | 541 |
@Override |
11314 | 542 |
boolean handleOption(Option option, String value) { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
543 |
if (!options.contains(option)) { |
10818 | 544 |
return false; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
545 |
} |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
546 |
searchPath = value == null ? null |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
547 |
: Collections.unmodifiableCollection(createPath().addFiles(value)); |
10818 | 548 |
return true; |
549 |
} |
|
550 |
||
551 |
@Override |
|
36526 | 552 |
Collection<Path> getPaths() { |
10818 | 553 |
return searchPath; |
554 |
} |
|
10 | 555 |
|
10818 | 556 |
@Override |
36526 | 557 |
void setPaths(Iterable<? extends Path> files) { |
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
558 |
SearchPath p; |
10818 | 559 |
if (files == null) { |
560 |
p = computePath(null); |
|
561 |
} else { |
|
14360
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
562 |
p = createPath().addFiles(files); |
10818 | 563 |
} |
564 |
searchPath = Collections.unmodifiableCollection(p); |
|
565 |
} |
|
14360
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
566 |
|
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
567 |
protected SearchPath computePath(String value) { |
14360
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
568 |
return createPath().addFiles(value); |
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
569 |
} |
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
570 |
|
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
571 |
protected SearchPath createPath() { |
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
572 |
return new SearchPath(); |
14360
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
573 |
} |
10 | 574 |
} |
575 |
||
10818 | 576 |
/** |
36526 | 577 |
* Subtype of SimpleLocationHandler for -classpath/CLASS_PATH. |
578 |
* If no value is given, a default is provided, based on system properties and other values. |
|
10818 | 579 |
*/ |
580 |
private class ClassPathLocationHandler extends SimpleLocationHandler { |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
581 |
|
10818 | 582 |
ClassPathLocationHandler() { |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
583 |
super(StandardLocation.CLASS_PATH, Option.CLASS_PATH); |
10818 | 584 |
} |
585 |
||
586 |
@Override |
|
36526 | 587 |
Collection<Path> getPaths() { |
10818 | 588 |
lazy(); |
589 |
return searchPath; |
|
590 |
} |
|
591 |
||
592 |
@Override |
|
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
593 |
protected SearchPath computePath(String value) { |
10818 | 594 |
String cp = value; |
10 | 595 |
|
10818 | 596 |
// CLASSPATH environment variable when run from `javac'. |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
597 |
if (cp == null) { |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
598 |
cp = System.getProperty("env.class.path"); |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
599 |
} |
10818 | 600 |
|
601 |
// If invoked via a java VM (not the javac launcher), use the |
|
602 |
// platform class path |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
603 |
if (cp == null && System.getProperty("application.home") == null) { |
10818 | 604 |
cp = System.getProperty("java.class.path"); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
605 |
} |
10818 | 606 |
|
607 |
// Default to current working directory. |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
608 |
if (cp == null) { |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
609 |
cp = "."; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
610 |
} |
10818 | 611 |
|
14360
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
612 |
return createPath().addFiles(cp); |
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
613 |
} |
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
614 |
|
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
615 |
@Override |
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
616 |
protected SearchPath createPath() { |
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
617 |
return new SearchPath() |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
618 |
.expandJarClassPaths(true) // Only search user jars for Class-Paths |
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
619 |
.emptyPathDefault(getPath(".")); // Empty path elt ==> current directory |
14360
2a8304e421ed
6725230: Java Compilation with Jsr199 ignores Class-Path in manifest
jjg
parents:
14259
diff
changeset
|
620 |
} |
10818 | 621 |
|
622 |
private void lazy() { |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
623 |
if (searchPath == null) { |
36526 | 624 |
setPaths(null); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
625 |
} |
10818 | 626 |
} |
10 | 627 |
} |
628 |
||
10818 | 629 |
/** |
36526 | 630 |
* Custom subtype of LocationHandler for PLATFORM_CLASS_PATH. |
631 |
* Various options are supported for different components of the |
|
632 |
* platform class path. |
|
633 |
* Setting a value with setLocation overrides all existing option values. |
|
634 |
* Setting any option overrides any value set with setLocation, and |
|
635 |
* reverts to using default values for options that have not been set. |
|
636 |
* Setting -bootclasspath or -Xbootclasspath overrides any existing |
|
637 |
* value for -Xbootclasspath/p: and -Xbootclasspath/a:. |
|
10818 | 638 |
*/ |
36526 | 639 |
private class BootClassPathLocationHandler extends BasicLocationHandler { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
640 |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
641 |
private Collection<Path> searchPath; |
22163 | 642 |
final Map<Option, String> optionValues = new EnumMap<>(Option.class); |
10818 | 643 |
|
644 |
/** |
|
27579 | 645 |
* Is the bootclasspath the default? |
10818 | 646 |
*/ |
27579 | 647 |
private boolean isDefault; |
10818 | 648 |
|
649 |
BootClassPathLocationHandler() { |
|
650 |
super(StandardLocation.PLATFORM_CLASS_PATH, |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
651 |
Option.BOOT_CLASS_PATH, Option.XBOOTCLASSPATH, |
11314 | 652 |
Option.XBOOTCLASSPATH_PREPEND, |
653 |
Option.XBOOTCLASSPATH_APPEND, |
|
654 |
Option.ENDORSEDDIRS, Option.DJAVA_ENDORSED_DIRS, |
|
655 |
Option.EXTDIRS, Option.DJAVA_EXT_DIRS); |
|
10818 | 656 |
} |
657 |
||
658 |
boolean isDefault() { |
|
659 |
lazy(); |
|
27579 | 660 |
return isDefault; |
10818 | 661 |
} |
662 |
||
663 |
@Override |
|
11314 | 664 |
boolean handleOption(Option option, String value) { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
665 |
if (!options.contains(option)) { |
10818 | 666 |
return false; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
667 |
} |
10 | 668 |
|
10818 | 669 |
option = canonicalize(option); |
670 |
optionValues.put(option, value); |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
671 |
if (option == BOOT_CLASS_PATH) { |
10818 | 672 |
optionValues.remove(XBOOTCLASSPATH_PREPEND); |
673 |
optionValues.remove(XBOOTCLASSPATH_APPEND); |
|
674 |
} |
|
675 |
searchPath = null; // reset to "uninitialized" |
|
676 |
return true; |
|
677 |
} |
|
678 |
// where |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
679 |
// TODO: would be better if option aliasing was handled at a higher |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
680 |
// level |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
681 |
private Option canonicalize(Option option) { |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
682 |
switch (option) { |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
683 |
case XBOOTCLASSPATH: |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
684 |
return Option.BOOT_CLASS_PATH; |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
685 |
case DJAVA_ENDORSED_DIRS: |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
686 |
return Option.ENDORSEDDIRS; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
687 |
case DJAVA_EXT_DIRS: |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
688 |
return Option.EXTDIRS; |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
689 |
default: |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
690 |
return option; |
10818 | 691 |
} |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
692 |
} |
10 | 693 |
|
10818 | 694 |
@Override |
36526 | 695 |
Collection<Path> getPaths() { |
10 | 696 |
lazy(); |
10818 | 697 |
return searchPath; |
698 |
} |
|
699 |
||
700 |
@Override |
|
36526 | 701 |
void setPaths(Iterable<? extends Path> files) { |
10818 | 702 |
if (files == null) { |
703 |
searchPath = null; // reset to "uninitialized" |
|
704 |
} else { |
|
27579 | 705 |
isDefault = false; |
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
706 |
SearchPath p = new SearchPath().addFiles(files, false); |
10818 | 707 |
searchPath = Collections.unmodifiableCollection(p); |
708 |
optionValues.clear(); |
|
709 |
} |
|
10 | 710 |
} |
10818 | 711 |
|
27579 | 712 |
SearchPath computePath() throws IOException { |
27118
3c8314271af7
8060043: Rename Locations.Path to Locations.SearchPath
jjg
parents:
25874
diff
changeset
|
713 |
SearchPath path = new SearchPath(); |
10818 | 714 |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
715 |
String bootclasspathOpt = optionValues.get(BOOT_CLASS_PATH); |
10818 | 716 |
String endorseddirsOpt = optionValues.get(ENDORSEDDIRS); |
717 |
String extdirsOpt = optionValues.get(EXTDIRS); |
|
718 |
String xbootclasspathPrependOpt = optionValues.get(XBOOTCLASSPATH_PREPEND); |
|
719 |
String xbootclasspathAppendOpt = optionValues.get(XBOOTCLASSPATH_APPEND); |
|
720 |
path.addFiles(xbootclasspathPrependOpt); |
|
721 |
||
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
722 |
if (endorseddirsOpt != null) { |
10818 | 723 |
path.addDirectories(endorseddirsOpt); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
724 |
} else { |
10818 | 725 |
path.addDirectories(System.getProperty("java.endorsed.dirs"), false); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
726 |
} |
10818 | 727 |
|
728 |
if (bootclasspathOpt != null) { |
|
729 |
path.addFiles(bootclasspathOpt); |
|
730 |
} else { |
|
731 |
// Standard system classes for this compiler's release. |
|
36526 | 732 |
Collection<Path> systemClasses = systemClasses(); |
27579 | 733 |
if (systemClasses != null) { |
734 |
path.addFiles(systemClasses, false); |
|
735 |
} else { |
|
736 |
// fallback to the value of sun.boot.class.path |
|
737 |
String files = System.getProperty("sun.boot.class.path"); |
|
738 |
path.addFiles(files, false); |
|
10818 | 739 |
} |
740 |
} |
|
741 |
||
742 |
path.addFiles(xbootclasspathAppendOpt); |
|
743 |
||
744 |
// Strictly speaking, standard extensions are not bootstrap |
|
745 |
// classes, but we treat them identically, so we'll pretend |
|
746 |
// that they are. |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
747 |
if (extdirsOpt != null) { |
10818 | 748 |
path.addDirectories(extdirsOpt); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
749 |
} else { |
27579 | 750 |
// Add lib/jfxrt.jar to the search path |
36526 | 751 |
Path jfxrt = javaHome.resolve("lib/jfxrt.jar"); |
27858 | 752 |
if (Files.exists(jfxrt)) { |
27579 | 753 |
path.addFile(jfxrt, false); |
754 |
} |
|
10818 | 755 |
path.addDirectories(System.getProperty("java.ext.dirs"), false); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
756 |
} |
10818 | 757 |
|
27579 | 758 |
isDefault = |
759 |
(xbootclasspathPrependOpt == null) |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
760 |
&& (bootclasspathOpt == null) |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
761 |
&& (xbootclasspathAppendOpt == null); |
10818 | 762 |
|
763 |
return path; |
|
764 |
} |
|
765 |
||
27579 | 766 |
/** |
767 |
* Return a collection of files containing system classes. |
|
768 |
* Returns {@code null} if not running on a modular image. |
|
769 |
* |
|
770 |
* @throws UncheckedIOException if an I/O errors occurs |
|
771 |
*/ |
|
36526 | 772 |
private Collection<Path> systemClasses() throws IOException { |
773 |
// Return "modules" jimage file if available |
|
774 |
if (Files.isRegularFile(thisSystemModules)) { |
|
40302
8c0d8d2c3519
8143048: Re-examine dependency on property sun.boot.class.path
jlahoda
parents:
38536
diff
changeset
|
775 |
return Collections.singleton(thisSystemModules); |
27579 | 776 |
} |
777 |
||
778 |
// Exploded module image |
|
36526 | 779 |
Path modules = javaHome.resolve("modules"); |
27579 | 780 |
if (Files.isDirectory(modules.resolve("java.base"))) { |
29053
5c1f1d6b40f6
8067445: New modular image-based file manager skips boot classes
jlahoda
parents:
28707
diff
changeset
|
781 |
try (Stream<Path> listedModules = Files.list(modules)) { |
40302
8c0d8d2c3519
8143048: Re-examine dependency on property sun.boot.class.path
jlahoda
parents:
38536
diff
changeset
|
782 |
return listedModules.collect(Collectors.toList()); |
29053
5c1f1d6b40f6
8067445: New modular image-based file manager skips boot classes
jlahoda
parents:
28707
diff
changeset
|
783 |
} |
27579 | 784 |
} |
785 |
||
786 |
// not a modular image that we know about |
|
787 |
return null; |
|
788 |
} |
|
789 |
||
10818 | 790 |
private void lazy() { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
791 |
if (searchPath == null) { |
27579 | 792 |
try { |
10818 | 793 |
searchPath = Collections.unmodifiableCollection(computePath()); |
27579 | 794 |
} catch (IOException e) { |
795 |
// TODO: need better handling here, e.g. javac Abort? |
|
796 |
throw new UncheckedIOException(e); |
|
797 |
} |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
798 |
} |
10818 | 799 |
} |
10 | 800 |
} |
801 |
||
36526 | 802 |
/** |
803 |
* A LocationHander to represent modules found from a module-oriented |
|
804 |
* location such as MODULE_SOURCE_PATH, UPGRADE_MODULE_PATH, |
|
805 |
* SYSTEM_MODULES and MODULE_PATH. |
|
806 |
* |
|
807 |
* The Location can be specified to accept overriding classes from the |
|
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
808 |
* {@code -Xpatch:<module>=<path> } parameter. |
36526 | 809 |
*/ |
810 |
private class ModuleLocationHandler extends LocationHandler implements Location { |
|
811 |
protected final String name; |
|
812 |
protected final String moduleName; |
|
813 |
protected final Collection<Path> searchPath; |
|
814 |
protected final Collection<Path> searchPathWithOverrides; |
|
815 |
protected final boolean output; |
|
816 |
||
817 |
ModuleLocationHandler(String name, String moduleName, Collection<Path> searchPath, |
|
818 |
boolean output, boolean allowOverrides) { |
|
819 |
this.name = name; |
|
820 |
this.moduleName = moduleName; |
|
821 |
this.searchPath = searchPath; |
|
822 |
this.output = output; |
|
823 |
||
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
824 |
if (allowOverrides && patchMap != null) { |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
825 |
SearchPath mPatch = patchMap.get(moduleName); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
826 |
if (mPatch != null) { |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
827 |
SearchPath sp = new SearchPath(); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
828 |
sp.addAll(mPatch); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
829 |
sp.addAll(searchPath); |
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
830 |
searchPathWithOverrides = sp; |
36526 | 831 |
} else { |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
832 |
searchPathWithOverrides = searchPath; |
36526 | 833 |
} |
834 |
} else { |
|
835 |
searchPathWithOverrides = searchPath; |
|
836 |
} |
|
837 |
} |
|
838 |
||
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
839 |
@Override @DefinedBy(Api.COMPILER) |
36526 | 840 |
public String getName() { |
841 |
return name; |
|
842 |
} |
|
843 |
||
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
36526
diff
changeset
|
844 |
@Override @DefinedBy(Api.COMPILER) |
36526 | 845 |
public boolean isOutputLocation() { |
846 |
return output; |
|
847 |
} |
|
848 |
||
849 |
@Override // defined by LocationHandler |
|
850 |
boolean handleOption(Option option, String value) { |
|
851 |
throw new UnsupportedOperationException(); |
|
852 |
} |
|
853 |
||
854 |
@Override // defined by LocationHandler |
|
855 |
Collection<Path> getPaths() { |
|
856 |
// For now, we always return searchPathWithOverrides. This may differ from the |
|
857 |
// JVM behavior if there is a module-info.class to be found in the overriding |
|
858 |
// classes. |
|
859 |
return searchPathWithOverrides; |
|
860 |
} |
|
861 |
||
862 |
@Override // defined by LocationHandler |
|
863 |
void setPaths(Iterable<? extends Path> files) throws IOException { |
|
864 |
throw new UnsupportedOperationException(); |
|
865 |
} |
|
866 |
||
867 |
@Override // defined by LocationHandler |
|
868 |
String inferModuleName() { |
|
869 |
return moduleName; |
|
870 |
} |
|
871 |
} |
|
872 |
||
873 |
/** |
|
874 |
* A LocationHandler for simple module-oriented search paths, |
|
875 |
* like UPGRADE_MODULE_PATH and MODULE_PATH. |
|
876 |
*/ |
|
877 |
private class ModulePathLocationHandler extends SimpleLocationHandler { |
|
878 |
ModulePathLocationHandler(Location location, Option... options) { |
|
879 |
super(location, options); |
|
880 |
} |
|
881 |
||
882 |
@Override |
|
883 |
public boolean handleOption(Option option, String value) { |
|
884 |
if (!options.contains(option)) { |
|
885 |
return false; |
|
886 |
} |
|
887 |
setPaths(value == null ? null : getPathEntries(value)); |
|
888 |
return true; |
|
889 |
} |
|
890 |
||
891 |
@Override |
|
892 |
Iterable<Set<Location>> listModuleLocations() { |
|
893 |
if (searchPath == null) |
|
894 |
return Collections.emptyList(); |
|
895 |
||
896 |
return () -> new ModulePathIterator(); |
|
897 |
} |
|
898 |
||
899 |
@Override |
|
900 |
void setPaths(Iterable<? extends Path> paths) { |
|
901 |
if (paths != null) { |
|
902 |
for (Path p: paths) { |
|
903 |
checkValidModulePathEntry(p); |
|
904 |
} |
|
905 |
} |
|
906 |
super.setPaths(paths); |
|
907 |
} |
|
908 |
||
909 |
private void checkValidModulePathEntry(Path p) { |
|
910 |
if (Files.isDirectory(p)) { |
|
911 |
// either an exploded module or a directory of modules |
|
912 |
return; |
|
913 |
} |
|
914 |
||
915 |
String name = p.getFileName().toString(); |
|
916 |
int lastDot = name.lastIndexOf("."); |
|
917 |
if (lastDot > 0) { |
|
918 |
switch (name.substring(lastDot)) { |
|
919 |
case ".jar": |
|
920 |
case ".jmod": |
|
921 |
return; |
|
922 |
} |
|
923 |
} |
|
924 |
throw new IllegalArgumentException(p.toString()); |
|
925 |
} |
|
926 |
||
927 |
class ModulePathIterator implements Iterator<Set<Location>> { |
|
928 |
Iterator<Path> pathIter = searchPath.iterator(); |
|
929 |
int pathIndex = 0; |
|
930 |
Set<Location> next = null; |
|
931 |
||
932 |
@Override |
|
933 |
public boolean hasNext() { |
|
934 |
if (next != null) |
|
935 |
return true; |
|
936 |
||
937 |
while (next == null) { |
|
938 |
if (pathIter.hasNext()) { |
|
939 |
Path path = pathIter.next(); |
|
940 |
if (Files.isDirectory(path)) { |
|
941 |
next = scanDirectory(path); |
|
942 |
} else { |
|
943 |
next = scanFile(path); |
|
944 |
} |
|
945 |
pathIndex++; |
|
946 |
} else |
|
947 |
return false; |
|
948 |
} |
|
949 |
return true; |
|
950 |
} |
|
951 |
||
952 |
@Override |
|
953 |
public Set<Location> next() { |
|
954 |
hasNext(); |
|
955 |
if (next != null) { |
|
956 |
Set<Location> result = next; |
|
957 |
next = null; |
|
958 |
return result; |
|
959 |
} |
|
960 |
throw new NoSuchElementException(); |
|
961 |
} |
|
962 |
||
963 |
private Set<Location> scanDirectory(Path path) { |
|
964 |
Set<Path> paths = new LinkedHashSet<>(); |
|
965 |
Path moduleInfoClass = null; |
|
966 |
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) { |
|
967 |
for (Path entry: stream) { |
|
968 |
if (entry.endsWith("module-info.class")) { |
|
969 |
moduleInfoClass = entry; |
|
970 |
break; // no need to continue scanning |
|
971 |
} |
|
972 |
paths.add(entry); |
|
973 |
} |
|
974 |
} catch (DirectoryIteratorException | IOException ignore) { |
|
975 |
log.error(Errors.LocnCantReadDirectory(path)); |
|
976 |
return Collections.emptySet(); |
|
977 |
} |
|
978 |
||
979 |
if (moduleInfoClass != null) { |
|
980 |
// It's an exploded module directly on the module path. |
|
981 |
// We can't infer module name from the directory name, so have to |
|
982 |
// read module-info.class. |
|
983 |
try { |
|
984 |
String moduleName = readModuleName(moduleInfoClass); |
|
985 |
String name = location.getName() |
|
986 |
+ "[" + pathIndex + ":" + moduleName + "]"; |
|
987 |
ModuleLocationHandler l = new ModuleLocationHandler(name, moduleName, |
|
988 |
Collections.singleton(path), false, true); |
|
989 |
return Collections.singleton(l); |
|
990 |
} catch (ModuleNameReader.BadClassFile e) { |
|
991 |
log.error(Errors.LocnBadModuleInfo(path)); |
|
992 |
return Collections.emptySet(); |
|
993 |
} catch (IOException e) { |
|
994 |
log.error(Errors.LocnCantReadFile(path)); |
|
995 |
return Collections.emptySet(); |
|
996 |
} |
|
997 |
} |
|
998 |
||
999 |
// A directory of modules |
|
1000 |
Set<Location> result = new LinkedHashSet<>(); |
|
1001 |
int index = 0; |
|
1002 |
for (Path entry : paths) { |
|
1003 |
Pair<String,Path> module = inferModuleName(entry); |
|
1004 |
if (module == null) { |
|
1005 |
// diagnostic reported if necessary; skip to next |
|
1006 |
continue; |
|
1007 |
} |
|
1008 |
String moduleName = module.fst; |
|
1009 |
Path modulePath = module.snd; |
|
1010 |
String name = location.getName() |
|
1011 |
+ "[" + pathIndex + "." + (index++) + ":" + moduleName + "]"; |
|
1012 |
ModuleLocationHandler l = new ModuleLocationHandler(name, moduleName, |
|
1013 |
Collections.singleton(modulePath), false, true); |
|
1014 |
result.add(l); |
|
1015 |
} |
|
1016 |
return result; |
|
1017 |
} |
|
1018 |
||
1019 |
private Set<Location> scanFile(Path path) { |
|
1020 |
Pair<String,Path> module = inferModuleName(path); |
|
1021 |
if (module == null) { |
|
1022 |
// diagnostic reported if necessary |
|
1023 |
return Collections.emptySet(); |
|
1024 |
} |
|
1025 |
String moduleName = module.fst; |
|
1026 |
Path modulePath = module.snd; |
|
1027 |
String name = location.getName() |
|
1028 |
+ "[" + pathIndex + ":" + moduleName + "]"; |
|
1029 |
ModuleLocationHandler l = new ModuleLocationHandler(name, moduleName, |
|
1030 |
Collections.singleton(modulePath), false, true); |
|
1031 |
return Collections.singleton(l); |
|
1032 |
} |
|
1033 |
||
1034 |
private Pair<String,Path> inferModuleName(Path p) { |
|
1035 |
if (Files.isDirectory(p)) { |
|
1036 |
if (Files.exists(p.resolve("module-info.class"))) { |
|
1037 |
String name = p.getFileName().toString(); |
|
1038 |
if (SourceVersion.isName(name)) |
|
1039 |
return new Pair<>(name, p); |
|
1040 |
} |
|
1041 |
return null; |
|
1042 |
} |
|
1043 |
||
38536
42569f7fe4e6
8154824: Compiler should handle java.nio.file.FileSystemNotFoundException gracefully and not abort
sadayapalam
parents:
38519
diff
changeset
|
1044 |
if (p.getFileName().toString().endsWith(".jar") && fsInfo.exists(p)) { |
36526 | 1045 |
try (FileSystem fs = FileSystems.newFileSystem(p, null)) { |
1046 |
Path moduleInfoClass = fs.getPath("module-info.class"); |
|
1047 |
if (Files.exists(moduleInfoClass)) { |
|
1048 |
String moduleName = readModuleName(moduleInfoClass); |
|
1049 |
return new Pair<>(moduleName, p); |
|
1050 |
} |
|
1051 |
} catch (ModuleNameReader.BadClassFile e) { |
|
1052 |
log.error(Errors.LocnBadModuleInfo(p)); |
|
1053 |
return null; |
|
1054 |
} catch (IOException e) { |
|
1055 |
log.error(Errors.LocnCantReadFile(p)); |
|
1056 |
return null; |
|
1057 |
} |
|
1058 |
||
1059 |
//automatic module: |
|
1060 |
String fn = p.getFileName().toString(); |
|
1061 |
//from ModulePath.deriveModuleDescriptor: |
|
1062 |
||
1063 |
// drop .jar |
|
1064 |
String mn = fn.substring(0, fn.length()-4); |
|
1065 |
||
1066 |
// find first occurrence of -${NUMBER}. or -${NUMBER}$ |
|
1067 |
Matcher matcher = Pattern.compile("-(\\d+(\\.|$))").matcher(mn); |
|
1068 |
if (matcher.find()) { |
|
1069 |
int start = matcher.start(); |
|
1070 |
||
1071 |
mn = mn.substring(0, start); |
|
1072 |
} |
|
1073 |
||
1074 |
// finally clean up the module name |
|
1075 |
mn = mn.replaceAll("[^A-Za-z0-9]", ".") // replace non-alphanumeric |
|
1076 |
.replaceAll("(\\.)(\\1)+", ".") // collapse repeating dots |
|
1077 |
.replaceAll("^\\.", "") // drop leading dots |
|
1078 |
.replaceAll("\\.$", ""); // drop trailing dots |
|
1079 |
||
1080 |
||
1081 |
if (!mn.isEmpty()) { |
|
1082 |
return new Pair<>(mn, p); |
|
1083 |
} |
|
1084 |
||
1085 |
log.error(Errors.LocnCantGetModuleNameForJar(p)); |
|
1086 |
return null; |
|
1087 |
} |
|
1088 |
||
1089 |
if (p.getFileName().toString().endsWith(".jmod")) { |
|
1090 |
try { |
|
1091 |
FileSystem fs = fileSystems.get(p); |
|
1092 |
if (fs == null) { |
|
1093 |
URI uri = URI.create("jar:" + p.toUri()); |
|
1094 |
fs = FileSystems.newFileSystem(uri, Collections.emptyMap(), null); |
|
1095 |
try { |
|
1096 |
Path moduleInfoClass = fs.getPath("classes/module-info.class"); |
|
1097 |
String moduleName = readModuleName(moduleInfoClass); |
|
1098 |
Path modulePath = fs.getPath("classes"); |
|
1099 |
fileSystems.put(p, fs); |
|
1100 |
closeables.add(fs); |
|
1101 |
fs = null; // prevent fs being closed in the finally clause |
|
1102 |
return new Pair<>(moduleName, modulePath); |
|
1103 |
} finally { |
|
1104 |
if (fs != null) |
|
1105 |
fs.close(); |
|
1106 |
} |
|
1107 |
} |
|
1108 |
} catch (ProviderNotFoundException e) { |
|
1109 |
// will be thrown if the file is not a valid zip file |
|
1110 |
log.error(Errors.LocnCantReadFile(p)); |
|
1111 |
return null; |
|
1112 |
} catch (ModuleNameReader.BadClassFile e) { |
|
1113 |
log.error(Errors.LocnBadModuleInfo(p)); |
|
1114 |
} catch (IOException e) { |
|
1115 |
log.error(Errors.LocnCantReadFile(p)); |
|
1116 |
return null; |
|
1117 |
} |
|
1118 |
} |
|
1119 |
||
38519 | 1120 |
if (warn && false) { // temp disable, when enabled, massage examples.not-yet.txt suitably. |
36526 | 1121 |
log.warning(Warnings.LocnUnknownFileOnModulePath(p)); |
1122 |
} |
|
1123 |
return null; |
|
1124 |
} |
|
1125 |
||
1126 |
private String readModuleName(Path path) throws IOException, ModuleNameReader.BadClassFile { |
|
1127 |
if (moduleNameReader == null) |
|
1128 |
moduleNameReader = new ModuleNameReader(); |
|
1129 |
return moduleNameReader.readModuleName(path); |
|
1130 |
} |
|
1131 |
} |
|
1132 |
||
1133 |
} |
|
1134 |
||
1135 |
private class ModuleSourcePathLocationHandler extends BasicLocationHandler { |
|
1136 |
||
1137 |
private Map<String, Location> moduleLocations; |
|
1138 |
private Map<Path, Location> pathLocations; |
|
1139 |
||
1140 |
||
1141 |
ModuleSourcePathLocationHandler() { |
|
1142 |
super(StandardLocation.MODULE_SOURCE_PATH, |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1143 |
Option.MODULE_SOURCE_PATH); |
36526 | 1144 |
} |
1145 |
||
1146 |
@Override |
|
1147 |
boolean handleOption(Option option, String value) { |
|
1148 |
init(value); |
|
1149 |
return true; |
|
1150 |
} |
|
1151 |
||
1152 |
void init(String value) { |
|
1153 |
Collection<String> segments = new ArrayList<>(); |
|
1154 |
for (String s: value.split(File.pathSeparator)) { |
|
1155 |
expandBraces(s, segments); |
|
1156 |
} |
|
1157 |
||
1158 |
Map<String, Collection<Path>> map = new LinkedHashMap<>(); |
|
1159 |
final String MARKER = "*"; |
|
1160 |
for (String seg: segments) { |
|
1161 |
int markStart = seg.indexOf(MARKER); |
|
1162 |
if (markStart == -1) { |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1163 |
add(map, getPath(seg), null); |
36526 | 1164 |
} else { |
1165 |
if (markStart == 0 || !isSeparator(seg.charAt(markStart - 1))) { |
|
1166 |
throw new IllegalArgumentException("illegal use of " + MARKER + " in " + seg); |
|
1167 |
} |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1168 |
Path prefix = getPath(seg.substring(0, markStart - 1)); |
36526 | 1169 |
Path suffix; |
1170 |
int markEnd = markStart + MARKER.length(); |
|
1171 |
if (markEnd == seg.length()) { |
|
1172 |
suffix = null; |
|
1173 |
} else if (!isSeparator(seg.charAt(markEnd)) |
|
1174 |
|| seg.indexOf(MARKER, markEnd) != -1) { |
|
1175 |
throw new IllegalArgumentException("illegal use of " + MARKER + " in " + seg); |
|
1176 |
} else { |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1177 |
suffix = getPath(seg.substring(markEnd + 1)); |
36526 | 1178 |
} |
1179 |
add(map, prefix, suffix); |
|
1180 |
} |
|
1181 |
} |
|
1182 |
||
1183 |
moduleLocations = new LinkedHashMap<>(); |
|
1184 |
pathLocations = new LinkedHashMap<>(); |
|
1185 |
map.forEach((k, v) -> { |
|
1186 |
String name = location.getName() + "[" + k + "]"; |
|
1187 |
ModuleLocationHandler h = new ModuleLocationHandler(name, k, v, false, false); |
|
1188 |
moduleLocations.put(k, h); |
|
1189 |
v.forEach(p -> pathLocations.put(normalize(p), h)); |
|
1190 |
}); |
|
1191 |
} |
|
1192 |
||
1193 |
private boolean isSeparator(char ch) { |
|
1194 |
// allow both separators on Windows |
|
1195 |
return (ch == File.separatorChar) || (ch == '/'); |
|
1196 |
} |
|
1197 |
||
1198 |
void add(Map<String, Collection<Path>> map, Path prefix, Path suffix) { |
|
1199 |
if (!Files.isDirectory(prefix)) { |
|
1200 |
if (warn) { |
|
1201 |
String key = Files.exists(prefix) |
|
1202 |
? "dir.path.element.not.directory" |
|
1203 |
: "dir.path.element.not.found"; |
|
1204 |
log.warning(Lint.LintCategory.PATH, key, prefix); |
|
1205 |
} |
|
1206 |
return; |
|
1207 |
} |
|
1208 |
try (DirectoryStream<Path> stream = Files.newDirectoryStream(prefix, path -> Files.isDirectory(path))) { |
|
1209 |
for (Path entry: stream) { |
|
1210 |
Path path = (suffix == null) ? entry : entry.resolve(suffix); |
|
1211 |
if (Files.isDirectory(path)) { |
|
1212 |
String name = entry.getFileName().toString(); |
|
1213 |
Collection<Path> paths = map.get(name); |
|
1214 |
if (paths == null) |
|
1215 |
map.put(name, paths = new ArrayList<>()); |
|
1216 |
paths.add(path); |
|
1217 |
} |
|
1218 |
} |
|
1219 |
} catch (IOException e) { |
|
1220 |
// TODO? What to do? |
|
1221 |
System.err.println(e); |
|
1222 |
} |
|
1223 |
} |
|
1224 |
||
1225 |
private void expandBraces(String value, Collection<String> results) { |
|
1226 |
int depth = 0; |
|
1227 |
int start = -1; |
|
1228 |
String prefix = null; |
|
1229 |
String suffix = null; |
|
1230 |
for (int i = 0; i < value.length(); i++) { |
|
1231 |
switch (value.charAt(i)) { |
|
1232 |
case '{': |
|
1233 |
depth++; |
|
1234 |
if (depth == 1) { |
|
1235 |
prefix = value.substring(0, i); |
|
1236 |
suffix = value.substring(getMatchingBrace(value, i) + 1); |
|
1237 |
start = i + 1; |
|
1238 |
} |
|
1239 |
break; |
|
1240 |
||
1241 |
case ',': |
|
1242 |
if (depth == 1) { |
|
1243 |
String elem = value.substring(start, i); |
|
1244 |
expandBraces(prefix + elem + suffix, results); |
|
1245 |
start = i + 1; |
|
1246 |
} |
|
1247 |
break; |
|
1248 |
||
1249 |
case '}': |
|
1250 |
switch (depth) { |
|
1251 |
case 0: |
|
1252 |
throw new IllegalArgumentException("mismatched braces"); |
|
1253 |
||
1254 |
case 1: |
|
1255 |
String elem = value.substring(start, i); |
|
1256 |
expandBraces(prefix + elem + suffix, results); |
|
1257 |
return; |
|
1258 |
||
1259 |
default: |
|
1260 |
depth--; |
|
1261 |
} |
|
1262 |
break; |
|
1263 |
} |
|
1264 |
} |
|
1265 |
if (depth > 0) |
|
1266 |
throw new IllegalArgumentException("mismatched braces"); |
|
1267 |
results.add(value); |
|
1268 |
} |
|
1269 |
||
1270 |
int getMatchingBrace(String value, int offset) { |
|
1271 |
int depth = 1; |
|
1272 |
for (int i = offset + 1; i < value.length(); i++) { |
|
1273 |
switch (value.charAt(i)) { |
|
1274 |
case '{': |
|
1275 |
depth++; |
|
1276 |
break; |
|
1277 |
||
1278 |
case '}': |
|
1279 |
if (--depth == 0) |
|
1280 |
return i; |
|
1281 |
break; |
|
1282 |
} |
|
1283 |
} |
|
1284 |
throw new IllegalArgumentException("mismatched braces"); |
|
1285 |
} |
|
1286 |
||
1287 |
@Override |
|
1288 |
boolean isSet() { |
|
1289 |
return (moduleLocations != null); |
|
1290 |
} |
|
1291 |
||
1292 |
@Override |
|
1293 |
Collection<Path> getPaths() { |
|
1294 |
throw new UnsupportedOperationException(); |
|
1295 |
} |
|
1296 |
||
1297 |
@Override |
|
1298 |
void setPaths(Iterable<? extends Path> files) throws IOException { |
|
1299 |
throw new UnsupportedOperationException(); |
|
1300 |
} |
|
1301 |
||
1302 |
@Override |
|
1303 |
Location getModuleLocation(String name) { |
|
1304 |
return (moduleLocations == null) ? null : moduleLocations.get(name); |
|
1305 |
} |
|
1306 |
||
1307 |
@Override |
|
1308 |
Location getModuleLocation(Path dir) { |
|
1309 |
return (pathLocations == null) ? null : pathLocations.get(dir); |
|
1310 |
} |
|
1311 |
||
1312 |
@Override |
|
1313 |
Iterable<Set<Location>> listModuleLocations() { |
|
1314 |
if (moduleLocations == null) |
|
1315 |
return Collections.emptySet(); |
|
1316 |
Set<Location> locns = new LinkedHashSet<>(); |
|
1317 |
moduleLocations.forEach((k, v) -> locns.add(v)); |
|
1318 |
return Collections.singleton(locns); |
|
1319 |
} |
|
1320 |
||
1321 |
} |
|
1322 |
||
1323 |
private class SystemModulesLocationHandler extends BasicLocationHandler { |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1324 |
private Path systemJavaHome; |
36526 | 1325 |
private Path modules; |
1326 |
private Map<String, ModuleLocationHandler> systemModules; |
|
1327 |
||
1328 |
SystemModulesLocationHandler() { |
|
1329 |
super(StandardLocation.SYSTEM_MODULES, Option.SYSTEM); |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1330 |
systemJavaHome = Locations.javaHome; |
36526 | 1331 |
} |
1332 |
||
1333 |
@Override |
|
1334 |
boolean handleOption(Option option, String value) { |
|
1335 |
if (!options.contains(option)) { |
|
1336 |
return false; |
|
1337 |
} |
|
1338 |
||
1339 |
if (value == null) { |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1340 |
systemJavaHome = Locations.javaHome; |
36526 | 1341 |
} else if (value.equals("none")) { |
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1342 |
systemJavaHome = null; |
36526 | 1343 |
} else { |
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1344 |
update(getPath(value)); |
36526 | 1345 |
} |
1346 |
||
1347 |
modules = null; |
|
1348 |
return true; |
|
1349 |
} |
|
1350 |
||
1351 |
@Override |
|
1352 |
Collection<Path> getPaths() { |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1353 |
return (systemJavaHome == null) ? null : Collections.singleton(systemJavaHome); |
36526 | 1354 |
} |
1355 |
||
1356 |
@Override |
|
1357 |
void setPaths(Iterable<? extends Path> files) throws IOException { |
|
1358 |
if (files == null) { |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1359 |
systemJavaHome = null; |
36526 | 1360 |
} else { |
1361 |
Iterator<? extends Path> pathIter = files.iterator(); |
|
1362 |
if (!pathIter.hasNext()) { |
|
1363 |
throw new IllegalArgumentException("empty path for directory"); // TODO: FIXME |
|
1364 |
} |
|
1365 |
Path dir = pathIter.next(); |
|
1366 |
if (pathIter.hasNext()) { |
|
1367 |
throw new IllegalArgumentException("path too long for directory"); // TODO: FIXME |
|
1368 |
} |
|
1369 |
if (!Files.exists(dir)) { |
|
1370 |
throw new FileNotFoundException(dir + ": does not exist"); |
|
1371 |
} else if (!Files.isDirectory(dir)) { |
|
1372 |
throw new IOException(dir + ": not a directory"); |
|
1373 |
} |
|
1374 |
update(dir); |
|
1375 |
} |
|
1376 |
} |
|
1377 |
||
1378 |
private void update(Path p) { |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1379 |
if (!isCurrentPlatform(p) && !Files.exists(p.resolve("jrt-fs.jar")) && !Files.exists(systemJavaHome.resolve("modules"))) |
36526 | 1380 |
throw new IllegalArgumentException(p.toString()); |
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1381 |
systemJavaHome = p; |
36526 | 1382 |
modules = null; |
1383 |
} |
|
1384 |
||
1385 |
private boolean isCurrentPlatform(Path p) { |
|
1386 |
try { |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1387 |
return Files.isSameFile(p, Locations.javaHome); |
36526 | 1388 |
} catch (IOException ex) { |
1389 |
throw new IllegalArgumentException(p.toString(), ex); |
|
1390 |
} |
|
1391 |
} |
|
1392 |
||
1393 |
@Override |
|
1394 |
Location getModuleLocation(String name) throws IOException { |
|
1395 |
initSystemModules(); |
|
1396 |
return systemModules.get(name); |
|
1397 |
} |
|
1398 |
||
1399 |
@Override |
|
1400 |
Iterable<Set<Location>> listModuleLocations() throws IOException { |
|
1401 |
initSystemModules(); |
|
1402 |
Set<Location> locns = new LinkedHashSet<>(); |
|
1403 |
for (Location l: systemModules.values()) |
|
1404 |
locns.add(l); |
|
1405 |
return Collections.singleton(locns); |
|
1406 |
} |
|
1407 |
||
1408 |
private void initSystemModules() throws IOException { |
|
1409 |
if (systemModules != null) { |
|
1410 |
return; |
|
1411 |
} |
|
1412 |
||
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1413 |
if (systemJavaHome == null) { |
36526 | 1414 |
systemModules = Collections.emptyMap(); |
1415 |
return; |
|
1416 |
} |
|
1417 |
||
1418 |
if (modules == null) { |
|
1419 |
try { |
|
1420 |
URI jrtURI = URI.create("jrt:/"); |
|
1421 |
FileSystem jrtfs; |
|
1422 |
||
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1423 |
if (isCurrentPlatform(systemJavaHome)) { |
36526 | 1424 |
jrtfs = FileSystems.getFileSystem(jrtURI); |
1425 |
} else { |
|
1426 |
try { |
|
1427 |
Map<String, String> attrMap = |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1428 |
Collections.singletonMap("java.home", systemJavaHome.toString()); |
36526 | 1429 |
jrtfs = FileSystems.newFileSystem(jrtURI, attrMap); |
1430 |
} catch (ProviderNotFoundException ex) { |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1431 |
URL javaHomeURL = systemJavaHome.resolve("jrt-fs.jar").toUri().toURL(); |
36526 | 1432 |
ClassLoader currentLoader = Locations.class.getClassLoader(); |
1433 |
URLClassLoader fsLoader = |
|
1434 |
new URLClassLoader(new URL[] {javaHomeURL}, currentLoader); |
|
1435 |
||
1436 |
jrtfs = FileSystems.newFileSystem(jrtURI, Collections.emptyMap(), fsLoader); |
|
1437 |
||
1438 |
closeables.add(fsLoader); |
|
1439 |
} |
|
1440 |
||
1441 |
closeables.add(jrtfs); |
|
1442 |
} |
|
1443 |
||
1444 |
modules = jrtfs.getPath("/modules"); |
|
1445 |
} catch (FileSystemNotFoundException | ProviderNotFoundException e) { |
|
37944
1153fab98d25
8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents:
37848
diff
changeset
|
1446 |
modules = systemJavaHome.resolve("modules"); |
36526 | 1447 |
if (!Files.exists(modules)) |
1448 |
throw new IOException("can't find system classes", e); |
|
1449 |
} |
|
1450 |
} |
|
1451 |
||
1452 |
systemModules = new LinkedHashMap<>(); |
|
1453 |
try (DirectoryStream<Path> stream = Files.newDirectoryStream(modules, Files::isDirectory)) { |
|
1454 |
for (Path entry : stream) { |
|
1455 |
String moduleName = entry.getFileName().toString(); |
|
1456 |
String name = location.getName() + "[" + moduleName + "]"; |
|
1457 |
ModuleLocationHandler h = new ModuleLocationHandler(name, moduleName, |
|
1458 |
Collections.singleton(entry), false, true); |
|
1459 |
systemModules.put(moduleName, h); |
|
1460 |
} |
|
1461 |
} |
|
1462 |
} |
|
1463 |
} |
|
1464 |
||
10818 | 1465 |
Map<Location, LocationHandler> handlersForLocation; |
11314 | 1466 |
Map<Option, LocationHandler> handlersForOption; |
10818 | 1467 |
|
1468 |
void initHandlers() { |
|
22163 | 1469 |
handlersForLocation = new HashMap<>(); |
1470 |
handlersForOption = new EnumMap<>(Option.class); |
|
10818 | 1471 |
|
36526 | 1472 |
BasicLocationHandler[] handlers = { |
10818 | 1473 |
new BootClassPathLocationHandler(), |
1474 |
new ClassPathLocationHandler(), |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1475 |
new SimpleLocationHandler(StandardLocation.SOURCE_PATH, Option.SOURCE_PATH), |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1476 |
new SimpleLocationHandler(StandardLocation.ANNOTATION_PROCESSOR_PATH, Option.PROCESSOR_PATH), |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1477 |
new SimpleLocationHandler(StandardLocation.ANNOTATION_PROCESSOR_MODULE_PATH, Option.PROCESSOR_MODULE_PATH), |
36526 | 1478 |
new OutputLocationHandler(StandardLocation.CLASS_OUTPUT, Option.D), |
1479 |
new OutputLocationHandler(StandardLocation.SOURCE_OUTPUT, Option.S), |
|
1480 |
new OutputLocationHandler(StandardLocation.NATIVE_HEADER_OUTPUT, Option.H), |
|
1481 |
new ModuleSourcePathLocationHandler(), |
|
1482 |
// TODO: should UPGRADE_MODULE_PATH be merged with SYSTEM_MODULES? |
|
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1483 |
new ModulePathLocationHandler(StandardLocation.UPGRADE_MODULE_PATH, Option.UPGRADE_MODULE_PATH), |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1484 |
new ModulePathLocationHandler(StandardLocation.MODULE_PATH, Option.MODULE_PATH), |
36526 | 1485 |
new SystemModulesLocationHandler(), |
10818 | 1486 |
}; |
10 | 1487 |
|
36526 | 1488 |
for (BasicLocationHandler h : handlers) { |
10818 | 1489 |
handlersForLocation.put(h.location, h); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1490 |
for (Option o : h.options) { |
10818 | 1491 |
handlersForOption.put(o, h); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1492 |
} |
10 | 1493 |
} |
10818 | 1494 |
} |
1495 |
||
36526 | 1496 |
private Map<String, SearchPath> patchMap; |
1497 |
||
29780
8f8e54a1fa20
8076420: Consolidate javac file handling in javac.file package
jjg
parents:
29291
diff
changeset
|
1498 |
boolean handleOption(Option option, String value) { |
36526 | 1499 |
switch (option) { |
40308
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1500 |
case PATCH_MODULE: |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1501 |
if (value == null) { |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1502 |
patchMap = null; |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1503 |
} else { |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1504 |
// Allow an extended syntax for --patch-module consisting of a series |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1505 |
// of values separated by NULL characters. This is to facilitate |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1506 |
// supporting deferred file manager options on the command line. |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1507 |
// See Option.PATCH_MODULE for the code that composes these multiple |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1508 |
// values. |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1509 |
for (String v : value.split("\0")) { |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1510 |
int eq = v.indexOf('='); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1511 |
if (eq > 0) { |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1512 |
String mName = v.substring(0, eq); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1513 |
SearchPath mPatchPath = new SearchPath() |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1514 |
.addFiles(v.substring(eq + 1)); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1515 |
boolean ok = true; |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1516 |
for (Path p : mPatchPath) { |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1517 |
Path mi = p.resolve("module-info.class"); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1518 |
if (Files.exists(mi)) { |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1519 |
log.error(Errors.LocnModuleInfoNotAllowedOnPatchPath(mi)); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1520 |
ok = false; |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1521 |
} |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1522 |
} |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1523 |
if (ok) { |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1524 |
if (patchMap == null) { |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1525 |
patchMap = new LinkedHashMap<>(); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1526 |
} |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1527 |
patchMap.put(mName, mPatchPath); |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1528 |
} |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1529 |
} else { |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1530 |
// Should not be able to get here; |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1531 |
// this should be caught and handled in Option.PATCH_MODULE |
274367a99f98
8136930: Simplify use of module-system options by custom launchers
jjg
parents:
40302
diff
changeset
|
1532 |
log.error(Errors.LocnInvalidArgForXpatch(value)); |
36526 | 1533 |
} |
1534 |
} |
|
1535 |
} |
|
1536 |
return true; |
|
1537 |
default: |
|
1538 |
LocationHandler h = handlersForOption.get(option); |
|
1539 |
return (h == null ? false : h.handleOption(option, value)); |
|
1540 |
} |
|
1541 |
} |
|
1542 |
||
1543 |
boolean hasLocation(Location location) { |
|
1544 |
LocationHandler h = getHandler(location); |
|
1545 |
return (h == null ? false : h.isSet()); |
|
10818 | 1546 |
} |
1547 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
1548 |
Collection<Path> getLocation(Location location) { |
10818 | 1549 |
LocationHandler h = getHandler(location); |
36526 | 1550 |
return (h == null ? null : h.getPaths()); |
10 | 1551 |
} |
1552 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
1553 |
Path getOutputLocation(Location location) { |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1554 |
if (!location.isOutputLocation()) { |
10818 | 1555 |
throw new IllegalArgumentException(); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1556 |
} |
10818 | 1557 |
LocationHandler h = getHandler(location); |
1558 |
return ((OutputLocationHandler) h).outputDir; |
|
1559 |
} |
|
10 | 1560 |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
1561 |
void setLocation(Location location, Iterable<? extends Path> files) throws IOException { |
10818 | 1562 |
LocationHandler h = getHandler(location); |
1563 |
if (h == null) { |
|
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1564 |
if (location.isOutputLocation()) { |
10818 | 1565 |
h = new OutputLocationHandler(location); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1566 |
} else { |
10818 | 1567 |
h = new SimpleLocationHandler(location); |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1568 |
} |
10818 | 1569 |
handlersForLocation.put(location, h); |
1570 |
} |
|
36526 | 1571 |
h.setPaths(files); |
1572 |
} |
|
1573 |
||
1574 |
Location getModuleLocation(Location location, String name) throws IOException { |
|
1575 |
LocationHandler h = getHandler(location); |
|
1576 |
return (h == null ? null : h.getModuleLocation(name)); |
|
1577 |
} |
|
1578 |
||
1579 |
Location getModuleLocation(Location location, Path dir) { |
|
1580 |
LocationHandler h = getHandler(location); |
|
1581 |
return (h == null ? null : h.getModuleLocation(dir)); |
|
1582 |
} |
|
1583 |
||
1584 |
String inferModuleName(Location location) { |
|
1585 |
LocationHandler h = getHandler(location); |
|
1586 |
return (h == null ? null : h.inferModuleName()); |
|
1587 |
} |
|
1588 |
||
1589 |
Iterable<Set<Location>> listModuleLocations(Location location) throws IOException { |
|
1590 |
LocationHandler h = getHandler(location); |
|
1591 |
return (h == null ? null : h.listModuleLocations()); |
|
10818 | 1592 |
} |
1593 |
||
1594 |
protected LocationHandler getHandler(Location location) { |
|
29291
076c277565f7
8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents:
29053
diff
changeset
|
1595 |
Objects.requireNonNull(location); |
36526 | 1596 |
return (location instanceof LocationHandler) |
1597 |
? (LocationHandler) location |
|
1598 |
: handlersForLocation.get(location); |
|
10818 | 1599 |
} |
1600 |
||
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1601 |
/** |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1602 |
* Is this the name of an archive file? |
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1603 |
*/ |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
1604 |
private boolean isArchive(Path file) { |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
1605 |
String n = StringUtils.toLowerCase(file.getFileName().toString()); |
1208 | 1606 |
return fsInfo.isFile(file) |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
1607 |
&& (n.endsWith(".jar") || n.endsWith(".zip")); |
10 | 1608 |
} |
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4548
diff
changeset
|
1609 |
|
36526 | 1610 |
static Path normalize(Path p) { |
1611 |
try { |
|
1612 |
return p.toRealPath(); |
|
1613 |
} catch (IOException e) { |
|
1614 |
return p.toAbsolutePath().normalize(); |
|
1615 |
} |
|
1616 |
} |
|
1617 |
||
10 | 1618 |
} |