author | jfdenise |
Thu, 25 Jun 2015 18:00:52 +0200 | |
changeset 31753 | 72417309a675 |
parent 29780 | 8f8e54a1fa20 |
child 34560 | b6a567b677f7 |
permissions | -rw-r--r-- |
4548 | 1 |
/* |
31753 | 2 |
* Copyright (c) 2009, 2015, 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 |
||
29780
8f8e54a1fa20
8076420: Consolidate javac file handling in javac.file package
jjg
parents:
29291
diff
changeset
|
26 |
package com.sun.tools.javac.file; |
4548 | 27 |
|
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.InputStreamReader; |
|
31 |
import java.io.OutputStream; |
|
32 |
import java.io.OutputStreamWriter; |
|
33 |
import java.io.Reader; |
|
34 |
import java.io.Writer; |
|
35 |
import java.net.URI; |
|
36 |
import java.nio.ByteBuffer; |
|
37 |
import java.nio.CharBuffer; |
|
38 |
import java.nio.charset.CharsetDecoder; |
|
39 |
import java.nio.file.Files; |
|
9084
0e6af03b5c9e
7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents:
8231
diff
changeset
|
40 |
import java.nio.file.LinkOption; |
4548 | 41 |
import java.nio.file.Path; |
29291
076c277565f7
8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents:
27579
diff
changeset
|
42 |
import java.util.Objects; |
29780
8f8e54a1fa20
8076420: Consolidate javac file handling in javac.file package
jjg
parents:
29291
diff
changeset
|
43 |
|
4548 | 44 |
import javax.lang.model.element.Modifier; |
45 |
import javax.lang.model.element.NestingKind; |
|
46 |
import javax.tools.JavaFileObject; |
|
47 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
48 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
49 |
import com.sun.tools.javac.util.DefinedBy.Api; |
4548 | 50 |
|
51 |
||
52 |
/** |
|
53 |
* Implementation of JavaFileObject using java.nio.file API. |
|
54 |
* |
|
55 |
* <p>PathFileObjects are, for the most part, straightforward wrappers around |
|
56 |
* Path objects. The primary complexity is the support for "inferBinaryName". |
|
57 |
* This is left as an abstract method, implemented by each of a number of |
|
58 |
* different factory methods, which compute the binary name based on |
|
59 |
* information available at the time the file object is created. |
|
60 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
61 |
* <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
|
62 |
* If you write code that depends on this, you do so at your own risk. |
4548 | 63 |
* This code and its internal interfaces are subject to change or |
64 |
* deletion without notice.</b> |
|
65 |
*/ |
|
27579 | 66 |
public abstract class PathFileObject implements JavaFileObject { |
67 |
private final BaseFileManager fileManager; |
|
68 |
private final Path path; |
|
4548 | 69 |
|
70 |
/** |
|
71 |
* Create a PathFileObject within a directory, such that the binary name |
|
72 |
* can be inferred from the relationship to the parent directory. |
|
73 |
*/ |
|
27579 | 74 |
static PathFileObject createDirectoryPathFileObject(BaseFileManager fileManager, |
4548 | 75 |
final Path path, final Path dir) { |
76 |
return new PathFileObject(fileManager, path) { |
|
77 |
@Override |
|
27579 | 78 |
public String inferBinaryName(Iterable<? extends Path> paths) { |
4548 | 79 |
return toBinaryName(dir.relativize(path)); |
80 |
} |
|
81 |
}; |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Create a PathFileObject in a file system such as a jar file, such that |
|
86 |
* the binary name can be inferred from its position within the filesystem. |
|
87 |
*/ |
|
27579 | 88 |
public static PathFileObject createJarPathFileObject(BaseFileManager fileManager, |
4548 | 89 |
final Path path) { |
90 |
return new PathFileObject(fileManager, path) { |
|
91 |
@Override |
|
27579 | 92 |
public String inferBinaryName(Iterable<? extends Path> paths) { |
4548 | 93 |
return toBinaryName(path); |
94 |
} |
|
95 |
}; |
|
96 |
} |
|
97 |
||
98 |
/** |
|
27579 | 99 |
* Create a PathFileObject in a modular file system, such as jrt:, such that |
100 |
* the binary name can be inferred from its position within the filesystem. |
|
101 |
*/ |
|
102 |
public static PathFileObject createJRTPathFileObject(BaseFileManager fileManager, |
|
103 |
final Path path) { |
|
104 |
return new PathFileObject(fileManager, path) { |
|
105 |
@Override |
|
106 |
public String inferBinaryName(Iterable<? extends Path> paths) { |
|
31753 | 107 |
// use subpath to ignore the leading /modules/MODULE-NAME |
108 |
return toBinaryName(path.subpath(2, path.getNameCount())); |
|
27579 | 109 |
} |
110 |
}; |
|
111 |
} |
|
112 |
||
113 |
/** |
|
4548 | 114 |
* Create a PathFileObject whose binary name can be inferred from the |
115 |
* relative path to a sibling. |
|
116 |
*/ |
|
27579 | 117 |
static PathFileObject createSiblingPathFileObject(BaseFileManager fileManager, |
4548 | 118 |
final Path path, final String relativePath) { |
119 |
return new PathFileObject(fileManager, path) { |
|
120 |
@Override |
|
27579 | 121 |
public String inferBinaryName(Iterable<? extends Path> paths) { |
4548 | 122 |
return toBinaryName(relativePath, "/"); |
123 |
} |
|
124 |
}; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Create a PathFileObject whose binary name might be inferred from its |
|
129 |
* position on a search path. |
|
130 |
*/ |
|
27579 | 131 |
static PathFileObject createSimplePathFileObject(BaseFileManager fileManager, |
4548 | 132 |
final Path path) { |
133 |
return new PathFileObject(fileManager, path) { |
|
134 |
@Override |
|
27579 | 135 |
public String inferBinaryName(Iterable<? extends Path> paths) { |
4548 | 136 |
Path absPath = path.toAbsolutePath(); |
137 |
for (Path p: paths) { |
|
138 |
Path ap = p.toAbsolutePath(); |
|
139 |
if (absPath.startsWith(ap)) { |
|
140 |
try { |
|
141 |
Path rp = ap.relativize(absPath); |
|
142 |
if (rp != null) // maybe null if absPath same as ap |
|
143 |
return toBinaryName(rp); |
|
144 |
} catch (IllegalArgumentException e) { |
|
145 |
// ignore this p if cannot relativize path to p |
|
146 |
} |
|
147 |
} |
|
148 |
} |
|
149 |
return null; |
|
150 |
} |
|
151 |
}; |
|
152 |
} |
|
153 |
||
27579 | 154 |
protected PathFileObject(BaseFileManager fileManager, Path path) { |
29291
076c277565f7
8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents:
27579
diff
changeset
|
155 |
this.fileManager = Objects.requireNonNull(fileManager); |
076c277565f7
8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents:
27579
diff
changeset
|
156 |
this.path = Objects.requireNonNull(path); |
4548 | 157 |
} |
158 |
||
27579 | 159 |
public abstract String inferBinaryName(Iterable<? extends Path> paths); |
4548 | 160 |
|
161 |
/** |
|
162 |
* Return the Path for this object. |
|
163 |
* @return the Path for this object. |
|
164 |
*/ |
|
27579 | 165 |
public Path getPath() { |
4548 | 166 |
return path; |
167 |
} |
|
168 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
169 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 170 |
public Kind getKind() { |
8231 | 171 |
return BaseFileManager.getKind(path.getFileName().toString()); |
4548 | 172 |
} |
173 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
174 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 175 |
public boolean isNameCompatible(String simpleName, Kind kind) { |
29291
076c277565f7
8073550: java* tools: replace obj.getClass hacks with Assert.checkNonNull or Objects.requireNonNull
mcimadamore
parents:
27579
diff
changeset
|
176 |
Objects.requireNonNull(simpleName); |
4548 | 177 |
// null check |
178 |
if (kind == Kind.OTHER && getKind() != kind) { |
|
179 |
return false; |
|
180 |
} |
|
181 |
String sn = simpleName + kind.extension; |
|
8231 | 182 |
String pn = path.getFileName().toString(); |
4548 | 183 |
if (pn.equals(sn)) { |
184 |
return true; |
|
185 |
} |
|
186 |
if (pn.equalsIgnoreCase(sn)) { |
|
187 |
try { |
|
188 |
// allow for Windows |
|
9084
0e6af03b5c9e
7029979: (fs) Path.toRealPath(boolean) should be toRealPath(LinkOption...)
alanb
parents:
8231
diff
changeset
|
189 |
return path.toRealPath(LinkOption.NOFOLLOW_LINKS).getFileName().toString().equals(sn); |
4548 | 190 |
} catch (IOException e) { |
191 |
} |
|
192 |
} |
|
193 |
return false; |
|
194 |
} |
|
195 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
196 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 197 |
public NestingKind getNestingKind() { |
198 |
return null; |
|
199 |
} |
|
200 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
201 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 202 |
public Modifier getAccessLevel() { |
203 |
return null; |
|
204 |
} |
|
205 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
206 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 207 |
public URI toUri() { |
208 |
return path.toUri(); |
|
209 |
} |
|
210 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
211 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 212 |
public String getName() { |
213 |
return path.toString(); |
|
214 |
} |
|
215 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
216 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 217 |
public InputStream openInputStream() throws IOException { |
8231 | 218 |
return Files.newInputStream(path); |
4548 | 219 |
} |
220 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
221 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 222 |
public OutputStream openOutputStream() throws IOException { |
10461
998e642f9811
7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents:
9091
diff
changeset
|
223 |
fileManager.flushCache(this); |
4548 | 224 |
ensureParentDirectoriesExist(); |
8231 | 225 |
return Files.newOutputStream(path); |
4548 | 226 |
} |
227 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
228 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 229 |
public Reader openReader(boolean ignoreEncodingErrors) throws IOException { |
230 |
CharsetDecoder decoder = fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); |
|
231 |
return new InputStreamReader(openInputStream(), decoder); |
|
232 |
} |
|
233 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
234 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 235 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
236 |
CharBuffer cb = fileManager.getCachedContent(this); |
|
237 |
if (cb == null) { |
|
22159
682da512ec17
8030253: Update langtools to use strings-in-switch
briangoetz
parents:
14258
diff
changeset
|
238 |
try (InputStream in = openInputStream()) { |
4548 | 239 |
ByteBuffer bb = fileManager.makeByteBuffer(in); |
240 |
JavaFileObject prev = fileManager.log.useSource(this); |
|
241 |
try { |
|
242 |
cb = fileManager.decode(bb, ignoreEncodingErrors); |
|
243 |
} finally { |
|
244 |
fileManager.log.useSource(prev); |
|
245 |
} |
|
246 |
fileManager.recycleByteBuffer(bb); |
|
247 |
if (!ignoreEncodingErrors) { |
|
248 |
fileManager.cache(this, cb); |
|
249 |
} |
|
250 |
} |
|
251 |
} |
|
252 |
return cb; |
|
253 |
} |
|
254 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
255 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 256 |
public Writer openWriter() throws IOException { |
10461
998e642f9811
7068451: Regression: javac compiles fixed sources against previous, not current, version of generated sources
jjg
parents:
9091
diff
changeset
|
257 |
fileManager.flushCache(this); |
4548 | 258 |
ensureParentDirectoriesExist(); |
8231 | 259 |
return new OutputStreamWriter(Files.newOutputStream(path), fileManager.getEncodingName()); |
4548 | 260 |
} |
261 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
262 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 263 |
public long getLastModified() { |
264 |
try { |
|
8231 | 265 |
return Files.getLastModifiedTime(path).toMillis(); |
4548 | 266 |
} catch (IOException e) { |
267 |
return -1; |
|
268 |
} |
|
269 |
} |
|
270 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
271 |
@Override @DefinedBy(Api.COMPILER) |
4548 | 272 |
public boolean delete() { |
273 |
try { |
|
8231 | 274 |
Files.delete(path); |
4548 | 275 |
return true; |
276 |
} catch (IOException e) { |
|
277 |
return false; |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
public boolean isSameFile(PathFileObject other) { |
|
282 |
try { |
|
8231 | 283 |
return Files.isSameFile(path, other.path); |
4548 | 284 |
} catch (IOException e) { |
285 |
return false; |
|
286 |
} |
|
287 |
} |
|
288 |
||
289 |
@Override |
|
290 |
public boolean equals(Object other) { |
|
291 |
return (other instanceof PathFileObject && path.equals(((PathFileObject) other).path)); |
|
292 |
} |
|
293 |
||
294 |
@Override |
|
295 |
public int hashCode() { |
|
296 |
return path.hashCode(); |
|
297 |
} |
|
298 |
||
299 |
@Override |
|
300 |
public String toString() { |
|
301 |
return getClass().getSimpleName() + "[" + path + "]"; |
|
302 |
} |
|
303 |
||
304 |
private void ensureParentDirectoriesExist() throws IOException { |
|
305 |
Path parent = path.getParent(); |
|
306 |
if (parent != null) |
|
307 |
Files.createDirectories(parent); |
|
308 |
} |
|
309 |
||
310 |
private long size() { |
|
311 |
try { |
|
8231 | 312 |
return Files.size(path); |
4548 | 313 |
} catch (IOException e) { |
314 |
return -1; |
|
315 |
} |
|
316 |
} |
|
317 |
||
318 |
protected static String toBinaryName(Path relativePath) { |
|
319 |
return toBinaryName(relativePath.toString(), |
|
320 |
relativePath.getFileSystem().getSeparator()); |
|
321 |
} |
|
322 |
||
323 |
protected static String toBinaryName(String relativePath, String sep) { |
|
4701
39e13c0d8d75
6915476: java.util.regex.PatternSyntaxException in com.sun.tools.javac.nio.PathFileObject
jjg
parents:
4548
diff
changeset
|
324 |
return removeExtension(relativePath).replace(sep, "."); |
4548 | 325 |
} |
326 |
||
327 |
protected static String removeExtension(String fileName) { |
|
328 |
int lastDot = fileName.lastIndexOf("."); |
|
329 |
return (lastDot == -1 ? fileName : fileName.substring(0, lastDot)); |
|
330 |
} |
|
331 |
} |