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