author | chegar |
Wed, 03 Dec 2014 19:28:40 +0000 | |
changeset 27858 | 443efec4bf09 |
parent 27579 | d1a63c99cdd5 |
parent 27852 | 2e6ad0e4fe20 |
permissions | -rw-r--r-- |
4548 | 1 |
/* |
24897
655b72d7b96e
7026941: 199: path options ignored when reusing filemanager across tasks
jjg
parents:
22163
diff
changeset
|
2 |
* Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved. |
4548 | 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 |
4548 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
4548 | 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. |
|
4548 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.nio; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.io.IOException; |
|
30 |
import java.net.MalformedURLException; |
|
31 |
import java.net.URL; |
|
32 |
import java.nio.charset.Charset; |
|
33 |
import java.nio.file.FileSystem; |
|
34 |
import java.nio.file.FileSystems; |
|
35 |
import java.nio.file.FileVisitOption; |
|
36 |
import java.nio.file.FileVisitResult; |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
37 |
import java.nio.file.Files; |
4548 | 38 |
import java.nio.file.Path; |
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
39 |
import java.nio.file.Paths; |
4548 | 40 |
import java.nio.file.SimpleFileVisitor; |
41 |
import java.nio.file.attribute.BasicFileAttributes; |
|
42 |
import java.util.ArrayList; |
|
43 |
import java.util.Arrays; |
|
44 |
import java.util.Collection; |
|
45 |
import java.util.Collections; |
|
46 |
import java.util.EnumSet; |
|
47 |
import java.util.HashMap; |
|
48 |
import java.util.Iterator; |
|
49 |
import java.util.LinkedHashSet; |
|
50 |
import java.util.Map; |
|
51 |
import java.util.Set; |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
52 |
|
4548 | 53 |
import javax.lang.model.SourceVersion; |
54 |
import javax.tools.FileObject; |
|
55 |
import javax.tools.JavaFileManager; |
|
56 |
import javax.tools.JavaFileObject; |
|
57 |
import javax.tools.JavaFileObject.Kind; |
|
58 |
import javax.tools.StandardLocation; |
|
59 |
||
60 |
import com.sun.tools.javac.util.BaseFileManager; |
|
61 |
import com.sun.tools.javac.util.Context; |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
62 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
63 |
import com.sun.tools.javac.util.DefinedBy.Api; |
4548 | 64 |
import com.sun.tools.javac.util.List; |
65 |
import com.sun.tools.javac.util.ListBuffer; |
|
66 |
||
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
67 |
import static java.nio.file.FileVisitOption.*; |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
68 |
|
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
69 |
import static javax.tools.StandardLocation.*; |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
70 |
|
11314 | 71 |
import static com.sun.tools.javac.main.Option.*; |
4548 | 72 |
|
73 |
||
74 |
// NOTE the imports carefully for this compilation unit. |
|
75 |
// |
|
76 |
// Path: java.nio.file.Path -- the new NIO type for which this file manager exists |
|
77 |
// |
|
78 |
// Paths: com.sun.tools.javac.file.Paths -- legacy javac type for handling path options |
|
79 |
// The other Paths (java.nio.file.Paths) is not used |
|
80 |
||
81 |
// NOTE this and related classes depend on new API in JDK 7. |
|
82 |
// This requires special handling while bootstrapping the JDK build, |
|
83 |
// when these classes might not yet have been compiled. To workaround |
|
84 |
// this, the build arranges to make stubs of these classes available |
|
85 |
// when compiling this and related classes. The set of stub files |
|
86 |
// is specified in make/build.properties. |
|
87 |
||
88 |
/** |
|
89 |
* Implementation of PathFileManager: a JavaFileManager based on the use |
|
90 |
* of java.nio.file.Path. |
|
91 |
* |
|
92 |
* <p>Just as a Path is somewhat analagous to a File, so too is this |
|
93 |
* JavacPathFileManager analogous to JavacFileManager, as it relates to the |
|
94 |
* support of FileObjects based on File objects (i.e. just RegularFileObject, |
|
95 |
* not ZipFileObject and its variants.) |
|
96 |
* |
|
97 |
* <p>The default values for the standard locations supported by this file |
|
98 |
* manager are the same as the default values provided by JavacFileManager -- |
|
99 |
* i.e. as determined by the javac.file.Paths class. To override these values, |
|
100 |
* call {@link #setLocation}. |
|
101 |
* |
|
102 |
* <p>To reduce confusion with Path objects, the locations such as "class path", |
|
103 |
* "source path", etc, are generically referred to here as "search paths". |
|
104 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
105 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
106 |
* If you write code that depends on this, you do so at your own risk. |
4548 | 107 |
* This code and its internal interfaces are subject to change or |
108 |
* deletion without notice.</b> |
|
109 |
*/ |
|
110 |
public class JavacPathFileManager extends BaseFileManager implements PathFileManager { |
|
111 |
protected FileSystem defaultFileSystem; |
|
112 |
||
113 |
/** |
|
114 |
* Create a JavacPathFileManager using a given context, optionally registering |
|
115 |
* it as the JavaFileManager for that context. |
|
116 |
*/ |
|
117 |
public JavacPathFileManager(Context context, boolean register, Charset charset) { |
|
118 |
super(charset); |
|
119 |
if (register) |
|
120 |
context.put(JavaFileManager.class, this); |
|
22163 | 121 |
pathsForLocation = new HashMap<>(); |
122 |
fileSystems = new HashMap<>(); |
|
4548 | 123 |
setContext(context); |
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Set the context for JavacPathFileManager. |
|
128 |
*/ |
|
129 |
@Override |
|
10813
95b39a692cd0
7101146: Paths should more directly managed by BaseFileManager
jjg
parents:
9087
diff
changeset
|
130 |
public void setContext(Context context) { |
4548 | 131 |
super.setContext(context); |
132 |
} |
|
133 |
||
134 |
@Override |
|
135 |
public FileSystem getDefaultFileSystem() { |
|
136 |
if (defaultFileSystem == null) |
|
137 |
defaultFileSystem = FileSystems.getDefault(); |
|
138 |
return defaultFileSystem; |
|
139 |
} |
|
140 |
||
141 |
@Override |
|
142 |
public void setDefaultFileSystem(FileSystem fs) { |
|
143 |
defaultFileSystem = fs; |
|
144 |
} |
|
145 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
146 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 147 |
public void flush() throws IOException { |
148 |
contentCache.clear(); |
|
149 |
} |
|
150 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
151 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 152 |
public void close() throws IOException { |
153 |
for (FileSystem fs: fileSystems.values()) |
|
154 |
fs.close(); |
|
155 |
} |
|
156 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
157 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 158 |
public ClassLoader getClassLoader(Location location) { |
159 |
nullCheck(location); |
|
160 |
Iterable<? extends Path> path = getLocation(location); |
|
161 |
if (path == null) |
|
162 |
return null; |
|
22163 | 163 |
ListBuffer<URL> lb = new ListBuffer<>(); |
4548 | 164 |
for (Path p: path) { |
165 |
try { |
|
166 |
lb.append(p.toUri().toURL()); |
|
167 |
} catch (MalformedURLException e) { |
|
168 |
throw new AssertionError(e); |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
return getClassLoader(lb.toArray(new URL[lb.size()])); |
|
173 |
} |
|
174 |
||
175 |
// <editor-fold defaultstate="collapsed" desc="Location handling"> |
|
176 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
177 |
@DefinedBy(Api.COMPILER) |
4548 | 178 |
public boolean hasLocation(Location location) { |
179 |
return (getLocation(location) != null); |
|
180 |
} |
|
181 |
||
182 |
public Iterable<? extends Path> getLocation(Location location) { |
|
183 |
nullCheck(location); |
|
184 |
lazyInitSearchPaths(); |
|
185 |
PathsForLocation path = pathsForLocation.get(location); |
|
186 |
if (path == null && !pathsForLocation.containsKey(location)) { |
|
187 |
setDefaultForLocation(location); |
|
188 |
path = pathsForLocation.get(location); |
|
189 |
} |
|
190 |
return path; |
|
191 |
} |
|
192 |
||
193 |
private Path getOutputLocation(Location location) { |
|
194 |
Iterable<? extends Path> paths = getLocation(location); |
|
195 |
return (paths == null ? null : paths.iterator().next()); |
|
196 |
} |
|
197 |
||
198 |
public void setLocation(Location location, Iterable<? extends Path> searchPath) |
|
199 |
throws IOException |
|
200 |
{ |
|
201 |
nullCheck(location); |
|
202 |
lazyInitSearchPaths(); |
|
203 |
if (searchPath == null) { |
|
204 |
setDefaultForLocation(location); |
|
205 |
} else { |
|
206 |
if (location.isOutputLocation()) |
|
207 |
checkOutputPath(searchPath); |
|
208 |
PathsForLocation pl = new PathsForLocation(); |
|
209 |
for (Path p: searchPath) |
|
210 |
pl.add(p); // TODO -Xlint:path warn if path not found |
|
211 |
pathsForLocation.put(location, pl); |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
private void checkOutputPath(Iterable<? extends Path> searchPath) throws IOException { |
|
216 |
Iterator<? extends Path> pathIter = searchPath.iterator(); |
|
217 |
if (!pathIter.hasNext()) |
|
218 |
throw new IllegalArgumentException("empty path for directory"); |
|
219 |
Path path = pathIter.next(); |
|
220 |
if (pathIter.hasNext()) |
|
221 |
throw new IllegalArgumentException("path too long for directory"); |
|
8231 | 222 |
if (!isDirectory(path)) |
4548 | 223 |
throw new IOException(path + ": not a directory"); |
224 |
} |
|
225 |
||
226 |
private void setDefaultForLocation(Location locn) { |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
227 |
Collection<Path> files = null; |
4548 | 228 |
if (locn instanceof StandardLocation) { |
229 |
switch ((StandardLocation) locn) { |
|
230 |
case CLASS_PATH: |
|
10818 | 231 |
files = locations.userClassPath(); |
4548 | 232 |
break; |
233 |
case PLATFORM_CLASS_PATH: |
|
10818 | 234 |
files = locations.bootClassPath(); |
4548 | 235 |
break; |
236 |
case SOURCE_PATH: |
|
10818 | 237 |
files = locations.sourcePath(); |
4548 | 238 |
break; |
239 |
case CLASS_OUTPUT: { |
|
240 |
String arg = options.get(D); |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
241 |
files = (arg == null ? null : Collections.singleton(Paths.get(arg))); |
4548 | 242 |
break; |
243 |
} |
|
244 |
case SOURCE_OUTPUT: { |
|
245 |
String arg = options.get(S); |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
246 |
files = (arg == null ? null : Collections.singleton(Paths.get(arg))); |
4548 | 247 |
break; |
248 |
} |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
PathsForLocation pl = new PathsForLocation(); |
|
253 |
if (files != null) { |
|
27852
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
254 |
for (Path f: files) |
2e6ad0e4fe20
8061876: replace java.io.File with java.nio.file.Path (again)
jjg
parents:
27226
diff
changeset
|
255 |
pl.add(f); |
4548 | 256 |
} |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
11314
diff
changeset
|
257 |
if (!pl.isEmpty()) |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
11314
diff
changeset
|
258 |
pathsForLocation.put(locn, pl); |
4548 | 259 |
} |
260 |
||
261 |
private void lazyInitSearchPaths() { |
|
262 |
if (!inited) { |
|
263 |
setDefaultForLocation(PLATFORM_CLASS_PATH); |
|
264 |
setDefaultForLocation(CLASS_PATH); |
|
265 |
setDefaultForLocation(SOURCE_PATH); |
|
266 |
inited = true; |
|
267 |
} |
|
268 |
} |
|
269 |
// where |
|
270 |
private boolean inited = false; |
|
271 |
||
272 |
private Map<Location, PathsForLocation> pathsForLocation; |
|
273 |
||
274 |
private static class PathsForLocation extends LinkedHashSet<Path> { |
|
275 |
private static final long serialVersionUID = 6788510222394486733L; |
|
276 |
} |
|
277 |
||
278 |
// </editor-fold> |
|
279 |
||
280 |
// <editor-fold defaultstate="collapsed" desc="FileObject handling"> |
|
281 |
||
282 |
@Override |
|
283 |
public Path getPath(FileObject fo) { |
|
284 |
nullCheck(fo); |
|
285 |
if (!(fo instanceof PathFileObject)) |
|
286 |
throw new IllegalArgumentException(); |
|
287 |
return ((PathFileObject) fo).getPath(); |
|
288 |
} |
|
289 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
290 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 291 |
public boolean isSameFile(FileObject a, FileObject b) { |
292 |
nullCheck(a); |
|
293 |
nullCheck(b); |
|
294 |
if (!(a instanceof PathFileObject)) |
|
295 |
throw new IllegalArgumentException("Not supported: " + a); |
|
296 |
if (!(b instanceof PathFileObject)) |
|
297 |
throw new IllegalArgumentException("Not supported: " + b); |
|
298 |
return ((PathFileObject) a).isSameFile((PathFileObject) b); |
|
299 |
} |
|
300 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
301 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 302 |
public Iterable<JavaFileObject> list(Location location, |
303 |
String packageName, Set<Kind> kinds, boolean recurse) |
|
304 |
throws IOException { |
|
305 |
// validatePackageName(packageName); |
|
306 |
nullCheck(packageName); |
|
307 |
nullCheck(kinds); |
|
308 |
||
309 |
Iterable<? extends Path> paths = getLocation(location); |
|
310 |
if (paths == null) |
|
311 |
return List.nil(); |
|
22163 | 312 |
ListBuffer<JavaFileObject> results = new ListBuffer<>(); |
4548 | 313 |
|
314 |
for (Path path : paths) |
|
315 |
list(path, packageName, kinds, recurse, results); |
|
316 |
||
317 |
return results.toList(); |
|
318 |
} |
|
319 |
||
320 |
private void list(Path path, String packageName, final Set<Kind> kinds, |
|
321 |
boolean recurse, final ListBuffer<JavaFileObject> results) |
|
322 |
throws IOException { |
|
8231 | 323 |
if (!Files.exists(path)) |
4548 | 324 |
return; |
325 |
||
326 |
final Path pathDir; |
|
327 |
if (isDirectory(path)) |
|
328 |
pathDir = path; |
|
329 |
else { |
|
330 |
FileSystem fs = getFileSystem(path); |
|
331 |
if (fs == null) |
|
332 |
return; |
|
333 |
pathDir = fs.getRootDirectories().iterator().next(); |
|
334 |
} |
|
335 |
String sep = path.getFileSystem().getSeparator(); |
|
336 |
Path packageDir = packageName.isEmpty() ? pathDir |
|
337 |
: pathDir.resolve(packageName.replace(".", sep)); |
|
8231 | 338 |
if (!Files.exists(packageDir)) |
4548 | 339 |
return; |
340 |
||
341 |
/* Alternate impl of list, superceded by use of Files.walkFileTree */ |
|
342 |
// Deque<Path> queue = new LinkedList<Path>(); |
|
343 |
// queue.add(packageDir); |
|
344 |
// |
|
345 |
// Path dir; |
|
346 |
// while ((dir = queue.poll()) != null) { |
|
347 |
// DirectoryStream<Path> ds = dir.newDirectoryStream(); |
|
348 |
// try { |
|
349 |
// for (Path p: ds) { |
|
8231 | 350 |
// String name = p.getFileName().toString(); |
4548 | 351 |
// if (isDirectory(p)) { |
352 |
// if (recurse && SourceVersion.isIdentifier(name)) { |
|
353 |
// queue.add(p); |
|
354 |
// } |
|
355 |
// } else { |
|
356 |
// if (kinds.contains(getKind(name))) { |
|
357 |
// JavaFileObject fe = |
|
358 |
// PathFileObject.createDirectoryPathFileObject(this, p, pathDir); |
|
359 |
// results.append(fe); |
|
360 |
// } |
|
361 |
// } |
|
362 |
// } |
|
363 |
// } finally { |
|
364 |
// ds.close(); |
|
365 |
// } |
|
366 |
// } |
|
367 |
int maxDepth = (recurse ? Integer.MAX_VALUE : 1); |
|
6722
7a6305ae0721
6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents:
5847
diff
changeset
|
368 |
Set<FileVisitOption> opts = EnumSet.of(FOLLOW_LINKS); |
4548 | 369 |
Files.walkFileTree(packageDir, opts, maxDepth, |
370 |
new SimpleFileVisitor<Path>() { |
|
371 |
@Override |
|
6722
7a6305ae0721
6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents:
5847
diff
changeset
|
372 |
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) { |
8231 | 373 |
Path name = dir.getFileName(); |
27579 | 374 |
if (name == null || SourceVersion.isIdentifier(name.toString())) |
4548 | 375 |
return FileVisitResult.CONTINUE; |
376 |
else |
|
377 |
return FileVisitResult.SKIP_SUBTREE; |
|
378 |
} |
|
379 |
||
380 |
@Override |
|
381 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
|
8231 | 382 |
if (attrs.isRegularFile() && kinds.contains(getKind(file.getFileName().toString()))) { |
27579 | 383 |
// WORKAROUND for .jimage files |
384 |
if (!file.isAbsolute()) |
|
385 |
file = pathDir.resolve(file); |
|
4548 | 386 |
JavaFileObject fe = |
387 |
PathFileObject.createDirectoryPathFileObject( |
|
388 |
JavacPathFileManager.this, file, pathDir); |
|
389 |
results.append(fe); |
|
390 |
} |
|
391 |
return FileVisitResult.CONTINUE; |
|
392 |
} |
|
393 |
}); |
|
394 |
} |
|
395 |
||
396 |
@Override |
|
397 |
public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths( |
|
398 |
Iterable<? extends Path> paths) { |
|
399 |
ArrayList<PathFileObject> result; |
|
400 |
if (paths instanceof Collection<?>) |
|
22163 | 401 |
result = new ArrayList<>(((Collection<?>)paths).size()); |
4548 | 402 |
else |
22163 | 403 |
result = new ArrayList<>(); |
4548 | 404 |
for (Path p: paths) |
405 |
result.add(PathFileObject.createSimplePathFileObject(this, nullCheck(p))); |
|
406 |
return result; |
|
407 |
} |
|
408 |
||
409 |
@Override |
|
410 |
public Iterable<? extends JavaFileObject> getJavaFileObjects(Path... paths) { |
|
411 |
return getJavaFileObjectsFromPaths(Arrays.asList(nullCheck(paths))); |
|
412 |
} |
|
413 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
414 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 415 |
public JavaFileObject getJavaFileForInput(Location location, |
416 |
String className, Kind kind) throws IOException { |
|
417 |
return getFileForInput(location, getRelativePath(className, kind)); |
|
418 |
} |
|
419 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
420 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 421 |
public FileObject getFileForInput(Location location, |
422 |
String packageName, String relativeName) throws IOException { |
|
423 |
return getFileForInput(location, getRelativePath(packageName, relativeName)); |
|
424 |
} |
|
425 |
||
426 |
private JavaFileObject getFileForInput(Location location, String relativePath) |
|
427 |
throws IOException { |
|
428 |
for (Path p: getLocation(location)) { |
|
429 |
if (isDirectory(p)) { |
|
430 |
Path f = resolve(p, relativePath); |
|
8231 | 431 |
if (Files.exists(f)) |
4548 | 432 |
return PathFileObject.createDirectoryPathFileObject(this, f, p); |
433 |
} else { |
|
434 |
FileSystem fs = getFileSystem(p); |
|
435 |
if (fs != null) { |
|
436 |
Path file = getPath(fs, relativePath); |
|
8231 | 437 |
if (Files.exists(file)) |
4548 | 438 |
return PathFileObject.createJarPathFileObject(this, file); |
439 |
} |
|
440 |
} |
|
441 |
} |
|
442 |
return null; |
|
443 |
} |
|
444 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
445 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 446 |
public JavaFileObject getJavaFileForOutput(Location location, |
447 |
String className, Kind kind, FileObject sibling) throws IOException { |
|
448 |
return getFileForOutput(location, getRelativePath(className, kind), sibling); |
|
449 |
} |
|
450 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
451 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 452 |
public FileObject getFileForOutput(Location location, String packageName, |
453 |
String relativeName, FileObject sibling) |
|
454 |
throws IOException { |
|
455 |
return getFileForOutput(location, getRelativePath(packageName, relativeName), sibling); |
|
456 |
} |
|
457 |
||
458 |
private JavaFileObject getFileForOutput(Location location, |
|
459 |
String relativePath, FileObject sibling) { |
|
460 |
Path dir = getOutputLocation(location); |
|
461 |
if (dir == null) { |
|
462 |
if (location == CLASS_OUTPUT) { |
|
463 |
Path siblingDir = null; |
|
464 |
if (sibling != null && sibling instanceof PathFileObject) { |
|
465 |
siblingDir = ((PathFileObject) sibling).getPath().getParent(); |
|
466 |
} |
|
467 |
return PathFileObject.createSiblingPathFileObject(this, |
|
468 |
siblingDir.resolve(getBaseName(relativePath)), |
|
469 |
relativePath); |
|
470 |
} else if (location == SOURCE_OUTPUT) { |
|
471 |
dir = getOutputLocation(CLASS_OUTPUT); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
Path file; |
|
476 |
if (dir != null) { |
|
477 |
file = resolve(dir, relativePath); |
|
478 |
return PathFileObject.createDirectoryPathFileObject(this, file, dir); |
|
479 |
} else { |
|
480 |
file = getPath(getDefaultFileSystem(), relativePath); |
|
481 |
return PathFileObject.createSimplePathFileObject(this, file); |
|
482 |
} |
|
483 |
||
484 |
} |
|
485 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
486 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 487 |
public String inferBinaryName(Location location, JavaFileObject fo) { |
488 |
nullCheck(fo); |
|
489 |
// Need to match the path semantics of list(location, ...) |
|
490 |
Iterable<? extends Path> paths = getLocation(location); |
|
491 |
if (paths == null) { |
|
492 |
return null; |
|
493 |
} |
|
494 |
||
495 |
if (!(fo instanceof PathFileObject)) |
|
496 |
throw new IllegalArgumentException(fo.getClass().getName()); |
|
497 |
||
498 |
return ((PathFileObject) fo).inferBinaryName(paths); |
|
499 |
} |
|
500 |
||
501 |
private FileSystem getFileSystem(Path p) throws IOException { |
|
502 |
FileSystem fs = fileSystems.get(p); |
|
503 |
if (fs == null) { |
|
8231 | 504 |
fs = FileSystems.newFileSystem(p, null); |
4548 | 505 |
fileSystems.put(p, fs); |
506 |
} |
|
507 |
return fs; |
|
508 |
} |
|
509 |
||
510 |
private Map<Path,FileSystem> fileSystems; |
|
511 |
||
512 |
// </editor-fold> |
|
513 |
||
514 |
// <editor-fold defaultstate="collapsed" desc="Utility methods"> |
|
515 |
||
516 |
private static String getRelativePath(String className, Kind kind) { |
|
517 |
return className.replace(".", "/") + kind.extension; |
|
518 |
} |
|
519 |
||
520 |
private static String getRelativePath(String packageName, String relativeName) { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
11314
diff
changeset
|
521 |
return packageName.isEmpty() |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
11314
diff
changeset
|
522 |
? relativeName : packageName.replace(".", "/") + "/" + relativeName; |
4548 | 523 |
} |
524 |
||
525 |
private static String getBaseName(String relativePath) { |
|
526 |
int lastSep = relativePath.lastIndexOf("/"); |
|
527 |
return relativePath.substring(lastSep + 1); // safe if "/" not found |
|
528 |
} |
|
529 |
||
530 |
private static boolean isDirectory(Path path) throws IOException { |
|
8231 | 531 |
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class); |
4548 | 532 |
return attrs.isDirectory(); |
533 |
} |
|
534 |
||
535 |
private static Path getPath(FileSystem fs, String relativePath) { |
|
536 |
return fs.getPath(relativePath.replace("/", fs.getSeparator())); |
|
537 |
} |
|
538 |
||
539 |
private static Path resolve(Path base, String relativePath) { |
|
540 |
FileSystem fs = base.getFileSystem(); |
|
541 |
Path rp = fs.getPath(relativePath.replace("/", fs.getSeparator())); |
|
542 |
return base.resolve(rp); |
|
543 |
} |
|
544 |
||
545 |
// </editor-fold> |
|
546 |
||
547 |
} |