|
1 /* |
|
2 * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. |
|
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 |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
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 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
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; |
|
40 import java.nio.file.Path; |
|
41 import java.nio.file.attribute.Attributes; |
|
42 import java.nio.file.attribute.BasicFileAttributes; |
|
43 import javax.lang.model.element.Modifier; |
|
44 import javax.lang.model.element.NestingKind; |
|
45 import javax.tools.JavaFileObject; |
|
46 |
|
47 import com.sun.tools.javac.util.BaseFileManager; |
|
48 |
|
49 |
|
50 /** |
|
51 * Implementation of JavaFileObject using java.nio.file API. |
|
52 * |
|
53 * <p>PathFileObjects are, for the most part, straightforward wrappers around |
|
54 * Path objects. The primary complexity is the support for "inferBinaryName". |
|
55 * This is left as an abstract method, implemented by each of a number of |
|
56 * different factory methods, which compute the binary name based on |
|
57 * information available at the time the file object is created. |
|
58 * |
|
59 * <p><b>This is NOT part of any API supported by Sun Microsystems. If |
|
60 * you write code that depends on this, you do so at your own risk. |
|
61 * This code and its internal interfaces are subject to change or |
|
62 * deletion without notice.</b> |
|
63 */ |
|
64 abstract class PathFileObject implements JavaFileObject { |
|
65 private JavacPathFileManager fileManager; |
|
66 private Path path; |
|
67 |
|
68 /** |
|
69 * Create a PathFileObject within a directory, such that the binary name |
|
70 * can be inferred from the relationship to the parent directory. |
|
71 */ |
|
72 static PathFileObject createDirectoryPathFileObject(JavacPathFileManager fileManager, |
|
73 final Path path, final Path dir) { |
|
74 return new PathFileObject(fileManager, path) { |
|
75 @Override |
|
76 String inferBinaryName(Iterable<? extends Path> paths) { |
|
77 return toBinaryName(dir.relativize(path)); |
|
78 } |
|
79 }; |
|
80 } |
|
81 |
|
82 /** |
|
83 * Create a PathFileObject in a file system such as a jar file, such that |
|
84 * the binary name can be inferred from its position within the filesystem. |
|
85 */ |
|
86 static PathFileObject createJarPathFileObject(JavacPathFileManager fileManager, |
|
87 final Path path) { |
|
88 return new PathFileObject(fileManager, path) { |
|
89 @Override |
|
90 String inferBinaryName(Iterable<? extends Path> paths) { |
|
91 return toBinaryName(path); |
|
92 } |
|
93 }; |
|
94 } |
|
95 |
|
96 /** |
|
97 * Create a PathFileObject whose binary name can be inferred from the |
|
98 * relative path to a sibling. |
|
99 */ |
|
100 static PathFileObject createSiblingPathFileObject(JavacPathFileManager fileManager, |
|
101 final Path path, final String relativePath) { |
|
102 return new PathFileObject(fileManager, path) { |
|
103 @Override |
|
104 String inferBinaryName(Iterable<? extends Path> paths) { |
|
105 return toBinaryName(relativePath, "/"); |
|
106 } |
|
107 }; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Create a PathFileObject whose binary name might be inferred from its |
|
112 * position on a search path. |
|
113 */ |
|
114 static PathFileObject createSimplePathFileObject(JavacPathFileManager fileManager, |
|
115 final Path path) { |
|
116 return new PathFileObject(fileManager, path) { |
|
117 @Override |
|
118 String inferBinaryName(Iterable<? extends Path> paths) { |
|
119 Path absPath = path.toAbsolutePath(); |
|
120 for (Path p: paths) { |
|
121 Path ap = p.toAbsolutePath(); |
|
122 if (absPath.startsWith(ap)) { |
|
123 try { |
|
124 Path rp = ap.relativize(absPath); |
|
125 if (rp != null) // maybe null if absPath same as ap |
|
126 return toBinaryName(rp); |
|
127 } catch (IllegalArgumentException e) { |
|
128 // ignore this p if cannot relativize path to p |
|
129 } |
|
130 } |
|
131 } |
|
132 return null; |
|
133 } |
|
134 }; |
|
135 } |
|
136 |
|
137 protected PathFileObject(JavacPathFileManager fileManager, Path path) { |
|
138 fileManager.getClass(); // null check |
|
139 path.getClass(); // null check |
|
140 this.fileManager = fileManager; |
|
141 this.path = path; |
|
142 } |
|
143 |
|
144 abstract String inferBinaryName(Iterable<? extends Path> paths); |
|
145 |
|
146 /** |
|
147 * Return the Path for this object. |
|
148 * @return the Path for this object. |
|
149 */ |
|
150 Path getPath() { |
|
151 return path; |
|
152 } |
|
153 |
|
154 @Override |
|
155 public Kind getKind() { |
|
156 return BaseFileManager.getKind(path.getName().toString()); |
|
157 } |
|
158 |
|
159 @Override |
|
160 public boolean isNameCompatible(String simpleName, Kind kind) { |
|
161 simpleName.getClass(); |
|
162 // null check |
|
163 if (kind == Kind.OTHER && getKind() != kind) { |
|
164 return false; |
|
165 } |
|
166 String sn = simpleName + kind.extension; |
|
167 String pn = path.getName().toString(); |
|
168 if (pn.equals(sn)) { |
|
169 return true; |
|
170 } |
|
171 if (pn.equalsIgnoreCase(sn)) { |
|
172 try { |
|
173 // allow for Windows |
|
174 return path.toRealPath(false).getName().toString().equals(sn); |
|
175 } catch (IOException e) { |
|
176 } |
|
177 } |
|
178 return false; |
|
179 } |
|
180 |
|
181 @Override |
|
182 public NestingKind getNestingKind() { |
|
183 return null; |
|
184 } |
|
185 |
|
186 @Override |
|
187 public Modifier getAccessLevel() { |
|
188 return null; |
|
189 } |
|
190 |
|
191 @Override |
|
192 public URI toUri() { |
|
193 return path.toUri(); |
|
194 } |
|
195 |
|
196 @Override |
|
197 public String getName() { |
|
198 return path.toString(); |
|
199 } |
|
200 |
|
201 @Override |
|
202 public InputStream openInputStream() throws IOException { |
|
203 return path.newInputStream(); |
|
204 } |
|
205 |
|
206 @Override |
|
207 public OutputStream openOutputStream() throws IOException { |
|
208 ensureParentDirectoriesExist(); |
|
209 return path.newOutputStream(); |
|
210 } |
|
211 |
|
212 @Override |
|
213 public Reader openReader(boolean ignoreEncodingErrors) throws IOException { |
|
214 CharsetDecoder decoder = fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); |
|
215 return new InputStreamReader(openInputStream(), decoder); |
|
216 } |
|
217 |
|
218 @Override |
|
219 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
|
220 CharBuffer cb = fileManager.getCachedContent(this); |
|
221 if (cb == null) { |
|
222 InputStream in = openInputStream(); |
|
223 try { |
|
224 ByteBuffer bb = fileManager.makeByteBuffer(in); |
|
225 JavaFileObject prev = fileManager.log.useSource(this); |
|
226 try { |
|
227 cb = fileManager.decode(bb, ignoreEncodingErrors); |
|
228 } finally { |
|
229 fileManager.log.useSource(prev); |
|
230 } |
|
231 fileManager.recycleByteBuffer(bb); |
|
232 if (!ignoreEncodingErrors) { |
|
233 fileManager.cache(this, cb); |
|
234 } |
|
235 } finally { |
|
236 in.close(); |
|
237 } |
|
238 } |
|
239 return cb; |
|
240 } |
|
241 |
|
242 @Override |
|
243 public Writer openWriter() throws IOException { |
|
244 ensureParentDirectoriesExist(); |
|
245 return new OutputStreamWriter(path.newOutputStream(), fileManager.getEncodingName()); |
|
246 } |
|
247 |
|
248 @Override |
|
249 public long getLastModified() { |
|
250 try { |
|
251 BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path); |
|
252 return attrs.lastModifiedTime().toMillis(); |
|
253 } catch (IOException e) { |
|
254 return -1; |
|
255 } |
|
256 } |
|
257 |
|
258 @Override |
|
259 public boolean delete() { |
|
260 try { |
|
261 path.delete(); |
|
262 return true; |
|
263 } catch (IOException e) { |
|
264 return false; |
|
265 } |
|
266 } |
|
267 |
|
268 public boolean isSameFile(PathFileObject other) { |
|
269 try { |
|
270 return path.isSameFile(other.path); |
|
271 } catch (IOException e) { |
|
272 return false; |
|
273 } |
|
274 } |
|
275 |
|
276 @Override |
|
277 public boolean equals(Object other) { |
|
278 return (other instanceof PathFileObject && path.equals(((PathFileObject) other).path)); |
|
279 } |
|
280 |
|
281 @Override |
|
282 public int hashCode() { |
|
283 return path.hashCode(); |
|
284 } |
|
285 |
|
286 @Override |
|
287 public String toString() { |
|
288 return getClass().getSimpleName() + "[" + path + "]"; |
|
289 } |
|
290 |
|
291 private void ensureParentDirectoriesExist() throws IOException { |
|
292 Path parent = path.getParent(); |
|
293 if (parent != null) |
|
294 Files.createDirectories(parent); |
|
295 } |
|
296 |
|
297 private long size() { |
|
298 try { |
|
299 BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path); |
|
300 return attrs.size(); |
|
301 } catch (IOException e) { |
|
302 return -1; |
|
303 } |
|
304 } |
|
305 |
|
306 protected static String toBinaryName(Path relativePath) { |
|
307 return toBinaryName(relativePath.toString(), |
|
308 relativePath.getFileSystem().getSeparator()); |
|
309 } |
|
310 |
|
311 protected static String toBinaryName(String relativePath, String sep) { |
|
312 return removeExtension(relativePath).replaceAll(sep, "."); |
|
313 } |
|
314 |
|
315 protected static String removeExtension(String fileName) { |
|
316 int lastDot = fileName.lastIndexOf("."); |
|
317 return (lastDot == -1 ? fileName : fileName.substring(0, lastDot)); |
|
318 } |
|
319 } |