author | rfield |
Wed, 08 Feb 2017 10:43:16 -0800 | |
changeset 43758 | 868af3718a21 |
parent 42261 | bb52b5514ad5 |
child 43772 | 4e5350b7be75 |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
37848
3c8ff4204d2d
8154956: Module system implementation refresh (4/2016)
alanb
parents:
33715
diff
changeset
|
2 |
* Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 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. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle 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 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. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jshell; |
|
27 |
||
28 |
import java.io.ByteArrayInputStream; |
|
29 |
import java.io.ByteArrayOutputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.InputStream; |
|
32 |
import java.io.OutputStream; |
|
33 |
import java.lang.reflect.InvocationTargetException; |
|
34 |
import java.lang.reflect.Method; |
|
35 |
import java.net.URI; |
|
36 |
import java.nio.file.FileSystems; |
|
37 |
import java.nio.file.Files; |
|
38 |
import java.nio.file.Path; |
|
39 |
import java.util.Collection; |
|
40 |
import java.util.Iterator; |
|
41 |
import java.util.Map; |
|
42 |
import java.util.NoSuchElementException; |
|
43 |
import java.util.Set; |
|
44 |
import java.util.TreeMap; |
|
33715
74b1bed86932
8141092: JShell: Completion hangs on identifier completion
jlahoda
parents:
33362
diff
changeset
|
45 |
|
33362 | 46 |
import javax.tools.JavaFileObject.Kind; |
47 |
import static javax.tools.StandardLocation.CLASS_PATH; |
|
48 |
import javax.tools.FileObject; |
|
49 |
import javax.tools.JavaFileManager; |
|
50 |
import javax.tools.JavaFileObject; |
|
51 |
import javax.tools.SimpleJavaFileObject; |
|
52 |
import javax.tools.StandardJavaFileManager; |
|
33715
74b1bed86932
8141092: JShell: Completion hangs on identifier completion
jlahoda
parents:
33362
diff
changeset
|
53 |
import javax.tools.StandardLocation; |
33362 | 54 |
|
55 |
||
56 |
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_FMGR; |
|
57 |
||
58 |
/** |
|
59 |
* File manager for the compiler API. Reads from memory (Strings) and writes |
|
60 |
* class files to memory (cached OutputMemoryJavaFileObject). |
|
61 |
* |
|
62 |
* @author Robert Field |
|
63 |
*/ |
|
64 |
class MemoryFileManager implements JavaFileManager { |
|
65 |
||
66 |
private final StandardJavaFileManager stdFileManager; |
|
67 |
||
68 |
private final Map<String, OutputMemoryJavaFileObject> classObjects = new TreeMap<>(); |
|
69 |
||
70 |
private ClassFileCreationListener classListener = null; |
|
71 |
||
72 |
private final JShell proc; |
|
73 |
||
74 |
// Upcoming Jigsaw |
|
75 |
private Method inferModuleNameMethod = null; |
|
42261
bb52b5514ad5
8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents:
40606
diff
changeset
|
76 |
private Method listLocationsForModulesMethod = null; |
33362 | 77 |
|
33715
74b1bed86932
8141092: JShell: Completion hangs on identifier completion
jlahoda
parents:
33362
diff
changeset
|
78 |
Iterable<? extends Path> getLocationAsPaths(Location loc) { |
74b1bed86932
8141092: JShell: Completion hangs on identifier completion
jlahoda
parents:
33362
diff
changeset
|
79 |
return this.stdFileManager.getLocationAsPaths(loc); |
74b1bed86932
8141092: JShell: Completion hangs on identifier completion
jlahoda
parents:
33362
diff
changeset
|
80 |
} |
74b1bed86932
8141092: JShell: Completion hangs on identifier completion
jlahoda
parents:
33362
diff
changeset
|
81 |
|
33362 | 82 |
static abstract class MemoryJavaFileObject extends SimpleJavaFileObject { |
83 |
||
84 |
public MemoryJavaFileObject(String name, JavaFileObject.Kind kind) { |
|
85 |
super(URI.create("string:///" + name.replace('.', '/') |
|
86 |
+ kind.extension), kind); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
class SourceMemoryJavaFileObject extends MemoryJavaFileObject { |
|
91 |
private final String src; |
|
92 |
private final Object origin; |
|
93 |
||
94 |
SourceMemoryJavaFileObject(Object origin, String className, String code) { |
|
95 |
super(className, JavaFileObject.Kind.SOURCE); |
|
96 |
this.origin = origin; |
|
97 |
this.src = code; |
|
98 |
} |
|
99 |
||
100 |
public Object getOrigin() { |
|
101 |
return origin; |
|
102 |
} |
|
103 |
||
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
104 |
@Override |
33362 | 105 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
106 |
return src; |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
static class OutputMemoryJavaFileObject extends MemoryJavaFileObject { |
|
111 |
||
112 |
/** |
|
113 |
* Byte code created by the compiler will be stored in this |
|
114 |
* ByteArrayOutputStream. |
|
115 |
*/ |
|
116 |
private ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
|
117 |
private byte[] bytes = null; |
|
118 |
||
119 |
private final String className; |
|
120 |
||
121 |
public OutputMemoryJavaFileObject(String name, JavaFileObject.Kind kind) { |
|
122 |
super(name, kind); |
|
123 |
this.className = name; |
|
124 |
} |
|
125 |
||
126 |
public byte[] getBytes() { |
|
127 |
if (bytes == null) { |
|
128 |
bytes = bos.toByteArray(); |
|
129 |
bos = null; |
|
130 |
} |
|
131 |
return bytes; |
|
132 |
} |
|
133 |
||
134 |
public void dump() { |
|
135 |
try { |
|
136 |
Path dumpDir = FileSystems.getDefault().getPath("dump"); |
|
137 |
if (Files.notExists(dumpDir)) { |
|
138 |
Files.createDirectory(dumpDir); |
|
139 |
} |
|
140 |
Path file = FileSystems.getDefault().getPath("dump", getName() + ".class"); |
|
141 |
Files.write(file, getBytes()); |
|
142 |
} catch (IOException ex) { |
|
143 |
throw new RuntimeException(ex); |
|
144 |
} |
|
145 |
} |
|
146 |
||
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
147 |
@Override |
33362 | 148 |
public String getName() { |
149 |
return className; |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Will provide the compiler with an output stream that leads to our |
|
154 |
* byte array. |
|
155 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
156 |
@Override |
33362 | 157 |
public OutputStream openOutputStream() throws IOException { |
158 |
return bos; |
|
159 |
} |
|
160 |
||
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
161 |
@Override |
33362 | 162 |
public InputStream openInputStream() throws IOException { |
163 |
return new ByteArrayInputStream(getBytes()); |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
public MemoryFileManager(StandardJavaFileManager standardManager, JShell proc) { |
|
43758 | 168 |
this.stdFileManager = proc.fileManagerMapping != null |
169 |
? proc.fileManagerMapping.apply(standardManager) |
|
170 |
: standardManager; |
|
33362 | 171 |
this.proc = proc; |
172 |
} |
|
173 |
||
174 |
private Collection<OutputMemoryJavaFileObject> generatedClasses() { |
|
175 |
return classObjects.values(); |
|
176 |
} |
|
177 |
||
178 |
// For debugging dumps |
|
179 |
public void dumpClasses() { |
|
180 |
for (OutputMemoryJavaFileObject co : generatedClasses()) { |
|
181 |
co.dump(); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
public JavaFileObject createSourceFileObject(Object origin, String name, String code) { |
|
186 |
return new SourceMemoryJavaFileObject(origin, name, code); |
|
187 |
} |
|
188 |
||
189 |
// Make compatible with Jigsaw |
|
43758 | 190 |
@Override |
33362 | 191 |
public String inferModuleName(Location location) { |
192 |
try { |
|
193 |
if (inferModuleNameMethod == null) { |
|
194 |
inferModuleNameMethod = JavaFileManager.class.getDeclaredMethod("inferModuleName", Location.class); |
|
195 |
} |
|
196 |
@SuppressWarnings("unchecked") |
|
197 |
String result = (String) inferModuleNameMethod.invoke(stdFileManager, location); |
|
198 |
return result; |
|
199 |
} catch (NoSuchMethodException | SecurityException ex) { |
|
200 |
throw new InternalError("Cannot lookup JavaFileManager method", ex); |
|
201 |
} catch (IllegalAccessException | |
|
202 |
IllegalArgumentException | |
|
203 |
InvocationTargetException ex) { |
|
204 |
throw new InternalError("Cannot invoke JavaFileManager method", ex); |
|
205 |
} |
|
206 |
} |
|
207 |
||
208 |
// Make compatible with Jigsaw |
|
43758 | 209 |
@Override |
42261
bb52b5514ad5
8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents:
40606
diff
changeset
|
210 |
public Iterable<Set<Location>> listLocationsForModules(Location location) throws IOException { |
33362 | 211 |
try { |
42261
bb52b5514ad5
8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents:
40606
diff
changeset
|
212 |
if (listLocationsForModulesMethod == null) { |
bb52b5514ad5
8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents:
40606
diff
changeset
|
213 |
listLocationsForModulesMethod = JavaFileManager.class.getDeclaredMethod("listLocationsForModules", Location.class); |
33362 | 214 |
} |
215 |
@SuppressWarnings("unchecked") |
|
42261
bb52b5514ad5
8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents:
40606
diff
changeset
|
216 |
Iterable<Set<Location>> result = (Iterable<Set<Location>>) listLocationsForModulesMethod.invoke(stdFileManager, location); |
33362 | 217 |
return result; |
218 |
} catch (NoSuchMethodException | SecurityException ex) { |
|
219 |
throw new InternalError("Cannot lookup JavaFileManager method", ex); |
|
220 |
} catch (IllegalAccessException | |
|
221 |
IllegalArgumentException | |
|
222 |
InvocationTargetException ex) { |
|
223 |
throw new InternalError("Cannot invoke JavaFileManager method", ex); |
|
224 |
} |
|
225 |
} |
|
226 |
||
227 |
||
228 |
/** |
|
229 |
* Returns a class loader for loading plug-ins from the given location. For |
|
230 |
* example, to load annotation processors, a compiler will request a class |
|
231 |
* loader for the {@link |
|
232 |
* StandardLocation#ANNOTATION_PROCESSOR_PATH |
|
233 |
* ANNOTATION_PROCESSOR_PATH} location. |
|
234 |
* |
|
235 |
* @param location a location |
|
236 |
* @return a class loader for the given location; or {@code null} |
|
237 |
* if loading plug-ins from the given location is disabled or if |
|
238 |
* the location is not known |
|
239 |
* @throws SecurityException if a class loader can not be created |
|
240 |
* in the current security context |
|
241 |
* @throws IllegalStateException if {@link #close} has been called |
|
242 |
* and this file manager cannot be reopened |
|
243 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
244 |
@Override |
33362 | 245 |
public ClassLoader getClassLoader(JavaFileManager.Location location) { |
246 |
proc.debug(DBG_FMGR, "getClassLoader: location\n", location); |
|
40603
a890cefaa41b
8158507: JShell: new jdk.jshell.MemoryFileManager(StandardJavaFileManager, JShell) creates a jdk.jshell.MemoryFileManager$REPLClassLoader classloader, which should be performed within a doPrivileged block
rfield
parents:
37848
diff
changeset
|
247 |
return stdFileManager.getClassLoader(location); |
33362 | 248 |
} |
249 |
||
250 |
/** |
|
251 |
* Lists all file objects matching the given criteria in the given |
|
252 |
* location. List file objects in "subpackages" if recurse is |
|
253 |
* true. |
|
254 |
* |
|
255 |
* <p>Note: even if the given location is unknown to this file |
|
256 |
* manager, it may not return {@code null}. Also, an unknown |
|
257 |
* location may not cause an exception. |
|
258 |
* |
|
259 |
* @param location a location |
|
260 |
* @param packageName a package name |
|
261 |
* @param kinds return objects only of these kinds |
|
262 |
* @param recurse if true include "subpackages" |
|
263 |
* @return an Iterable of file objects matching the given criteria |
|
264 |
* @throws IOException if an I/O error occurred, or if {@link |
|
265 |
* #close} has been called and this file manager cannot be |
|
266 |
* reopened |
|
267 |
* @throws IllegalStateException if {@link #close} has been called |
|
268 |
* and this file manager cannot be reopened |
|
269 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
270 |
@Override |
33362 | 271 |
public Iterable<JavaFileObject> list(JavaFileManager.Location location, |
272 |
String packageName, |
|
273 |
Set<JavaFileObject.Kind> kinds, |
|
274 |
boolean recurse) |
|
275 |
throws IOException { |
|
276 |
Iterable<JavaFileObject> stdList = stdFileManager.list(location, packageName, kinds, recurse); |
|
277 |
if (location==CLASS_PATH && packageName.equals("REPL")) { |
|
278 |
// if the desired list is for our JShell package, lazily iterate over |
|
279 |
// first the standard list then any generated classes. |
|
280 |
return () -> new Iterator<JavaFileObject>() { |
|
281 |
boolean stdDone = false; |
|
282 |
Iterator<? extends JavaFileObject> it; |
|
283 |
||
284 |
@Override |
|
285 |
public boolean hasNext() { |
|
286 |
if (it == null) { |
|
287 |
it = stdList.iterator(); |
|
288 |
} |
|
289 |
if (it.hasNext()) { |
|
290 |
return true; |
|
291 |
} |
|
292 |
if (stdDone) { |
|
293 |
return false; |
|
294 |
} else { |
|
295 |
stdDone = true; |
|
296 |
it = generatedClasses().iterator(); |
|
297 |
return it.hasNext(); |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
@Override |
|
302 |
public JavaFileObject next() { |
|
303 |
if (!hasNext()) { |
|
304 |
throw new NoSuchElementException(); |
|
305 |
} |
|
306 |
return it.next(); |
|
307 |
} |
|
308 |
||
309 |
}; |
|
310 |
} else { |
|
311 |
return stdList; |
|
312 |
} |
|
313 |
} |
|
314 |
||
315 |
/** |
|
316 |
* Infers a binary name of a file object based on a location. The |
|
317 |
* binary name returned might not be a valid binary name according to |
|
318 |
* <cite>The Java&trade { throw new UnsupportedOperationException("Not supported yet."); } Language Specification</cite>. |
|
319 |
* |
|
320 |
* @param location a location |
|
321 |
* @param file a file object |
|
322 |
* @return a binary name or {@code null} the file object is not |
|
323 |
* found in the given location |
|
324 |
* @throws IllegalStateException if {@link #close} has been called |
|
325 |
* and this file manager cannot be reopened |
|
326 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
327 |
@Override |
33362 | 328 |
public String inferBinaryName(JavaFileManager.Location location, JavaFileObject file) { |
329 |
if (file instanceof OutputMemoryJavaFileObject) { |
|
330 |
OutputMemoryJavaFileObject ofo = (OutputMemoryJavaFileObject) file; |
|
331 |
proc.debug(DBG_FMGR, "inferBinaryName %s => %s\n", file, ofo.getName()); |
|
332 |
return ofo.getName(); |
|
333 |
} else { |
|
334 |
return stdFileManager.inferBinaryName(location, file); |
|
335 |
} |
|
336 |
} |
|
337 |
||
338 |
/** |
|
339 |
* Compares two file objects and return true if they represent the |
|
340 |
* same underlying object. |
|
341 |
* |
|
342 |
* @param a a file object |
|
343 |
* @param b a file object |
|
344 |
* @return true if the given file objects represent the same |
|
345 |
* underlying object |
|
346 |
* |
|
347 |
* @throws IllegalArgumentException if either of the arguments |
|
348 |
* were created with another file manager and this file manager |
|
349 |
* does not support foreign file objects |
|
350 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
351 |
@Override |
33362 | 352 |
public boolean isSameFile(FileObject a, FileObject b) { |
353 |
return stdFileManager.isSameFile(b, b); |
|
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Determines if the given option is supported and if so, the |
|
358 |
* number of arguments the option takes. |
|
359 |
* |
|
360 |
* @param option an option |
|
361 |
* @return the number of arguments the given option takes or -1 if |
|
362 |
* the option is not supported |
|
363 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
364 |
@Override |
33362 | 365 |
public int isSupportedOption(String option) { |
366 |
proc.debug(DBG_FMGR, "isSupportedOption: %s\n", option); |
|
367 |
return stdFileManager.isSupportedOption(option); |
|
368 |
} |
|
369 |
||
370 |
/** |
|
371 |
* Handles one option. If {@code current} is an option to this |
|
372 |
* file manager it will consume any arguments to that option from |
|
373 |
* {@code remaining} and return true, otherwise return false. |
|
374 |
* |
|
375 |
* @param current current option |
|
376 |
* @param remaining remaining options |
|
377 |
* @return true if this option was handled by this file manager, |
|
378 |
* false otherwise |
|
379 |
* @throws IllegalArgumentException if this option to this file |
|
380 |
* manager is used incorrectly |
|
381 |
* @throws IllegalStateException if {@link #close} has been called |
|
382 |
* and this file manager cannot be reopened |
|
383 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
384 |
@Override |
33362 | 385 |
public boolean handleOption(String current, Iterator<String> remaining) { |
386 |
proc.debug(DBG_FMGR, "handleOption: current: %s\n", current + |
|
387 |
", remaining: " + remaining); |
|
388 |
return stdFileManager.handleOption(current, remaining); |
|
389 |
} |
|
390 |
||
391 |
/** |
|
392 |
* Determines if a location is known to this file manager. |
|
393 |
* |
|
394 |
* @param location a location |
|
395 |
* @return true if the location is known |
|
396 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
397 |
@Override |
33362 | 398 |
public boolean hasLocation(JavaFileManager.Location location) { |
399 |
proc.debug(DBG_FMGR, "hasLocation: location: %s\n", location); |
|
400 |
return stdFileManager.hasLocation(location); |
|
401 |
} |
|
402 |
||
403 |
interface ClassFileCreationListener { |
|
404 |
void newClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location, |
|
405 |
String className, Kind kind, FileObject sibling); |
|
406 |
} |
|
407 |
||
408 |
void registerClassFileCreationListener(ClassFileCreationListener listen) { |
|
409 |
this.classListener = listen; |
|
410 |
} |
|
411 |
||
412 |
/** |
|
413 |
* Returns a {@linkplain JavaFileObject file object} for input |
|
414 |
* representing the specified class of the specified kind in the |
|
415 |
* given location. |
|
416 |
* |
|
417 |
* @param location a location |
|
418 |
* @param className the name of a class |
|
419 |
* @param kind the kind of file, must be one of {@link |
|
420 |
* JavaFileObject.Kind#SOURCE SOURCE} or {@link |
|
421 |
* JavaFileObject.Kind#CLASS CLASS} |
|
422 |
* @return a file object, might return {@code null} if the |
|
423 |
* file does not exist |
|
424 |
* @throws IllegalArgumentException if the location is not known |
|
425 |
* to this file manager and the file manager does not support |
|
426 |
* unknown locations, or if the kind is not valid |
|
427 |
* @throws IOException if an I/O error occurred, or if {@link |
|
428 |
* #close} has been called and this file manager cannot be |
|
429 |
* reopened |
|
430 |
* @throws IllegalStateException if {@link #close} has been called |
|
431 |
* and this file manager cannot be reopened |
|
432 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
433 |
@Override |
33362 | 434 |
public JavaFileObject getJavaFileForInput(JavaFileManager.Location location, |
435 |
String className, |
|
436 |
JavaFileObject.Kind kind) |
|
437 |
throws IOException { |
|
438 |
return stdFileManager.getJavaFileForInput(location, className, kind); |
|
439 |
} |
|
440 |
||
441 |
/** |
|
442 |
* Returns a {@linkplain JavaFileObject file object} for output |
|
443 |
* representing the specified class of the specified kind in the |
|
444 |
* given location. |
|
445 |
* |
|
446 |
* <p>Optionally, this file manager might consider the sibling as |
|
447 |
* a hint for where to place the output. The exact semantics of |
|
448 |
* this hint is unspecified. The JDK compiler, javac, for |
|
449 |
* example, will place class files in the same directories as |
|
450 |
* originating source files unless a class file output directory |
|
451 |
* is provided. To facilitate this behavior, javac might provide |
|
452 |
* the originating source file as sibling when calling this |
|
453 |
* method. |
|
454 |
* |
|
455 |
* @param location a location |
|
456 |
* @param className the name of a class |
|
457 |
* @param kind the kind of file, must be one of {@link |
|
458 |
* JavaFileObject.Kind#SOURCE SOURCE} or {@link |
|
459 |
* JavaFileObject.Kind#CLASS CLASS} |
|
460 |
* @param sibling a file object to be used as hint for placement; |
|
461 |
* might be {@code null} |
|
462 |
* @return a file object for output |
|
463 |
* @throws IllegalArgumentException if sibling is not known to |
|
464 |
* this file manager, or if the location is not known to this file |
|
465 |
* manager and the file manager does not support unknown |
|
466 |
* locations, or if the kind is not valid |
|
467 |
* @throws IOException if an I/O error occurred, or if {@link |
|
468 |
* #close} has been called and this file manager cannot be |
|
469 |
* reopened |
|
470 |
* @throws IllegalStateException {@link #close} has been called |
|
471 |
* and this file manager cannot be reopened |
|
472 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
473 |
@Override |
33362 | 474 |
public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, |
475 |
String className, Kind kind, FileObject sibling) throws IOException { |
|
476 |
||
477 |
OutputMemoryJavaFileObject fo; |
|
478 |
fo = new OutputMemoryJavaFileObject(className, kind); |
|
479 |
classObjects.put(className, fo); |
|
480 |
proc.debug(DBG_FMGR, "Set out file: %s = %s\n", className, fo); |
|
481 |
if (classListener != null) { |
|
482 |
classListener.newClassFile(fo, location, className, kind, sibling); |
|
483 |
} |
|
484 |
return fo; |
|
485 |
} |
|
486 |
||
487 |
/** |
|
488 |
* Returns a {@linkplain FileObject file object} for input |
|
489 |
* representing the specified <a href="JavaFileManager.html#relative_name">relative |
|
490 |
* name</a> in the specified package in the given location. |
|
491 |
* |
|
492 |
* <p>If the returned object represents a {@linkplain |
|
493 |
* JavaFileObject.Kind#SOURCE source} or {@linkplain |
|
494 |
* JavaFileObject.Kind#CLASS class} file, it must be an instance |
|
495 |
* of {@link JavaFileObject}. |
|
496 |
* |
|
497 |
* <p>Informally, the file object returned by this method is |
|
498 |
* located in the concatenation of the location, package name, and |
|
499 |
* relative name. For example, to locate the properties file |
|
500 |
* "resources/compiler.properties" in the package |
|
501 |
* "com.sun.tools.javac" in the {@linkplain |
|
502 |
* StandardLocation#SOURCE_PATH SOURCE_PATH} location, this method |
|
503 |
* might be called like so: |
|
504 |
* |
|
505 |
* <pre>getFileForInput(SOURCE_PATH, "com.sun.tools.javac", "resources/compiler.properties");</pre> |
|
506 |
* |
|
507 |
* <p>If the call was executed on Windows, with SOURCE_PATH set to |
|
508 |
* <code>"C:\Documents and Settings\UncleBob\src\share\classes"</code>, |
|
509 |
* a valid result would be a file object representing the file |
|
510 |
* <code>"C:\Documents and Settings\UncleBob\src\share\classes\com\sun\tools\javac\resources\compiler.properties"</code>. |
|
511 |
* |
|
512 |
* @param location a location |
|
513 |
* @param packageName a package name |
|
514 |
* @param relativeName a relative name |
|
515 |
* @return a file object, might return {@code null} if the file |
|
516 |
* does not exist |
|
517 |
* @throws IllegalArgumentException if the location is not known |
|
518 |
* to this file manager and the file manager does not support |
|
519 |
* unknown locations, or if {@code relativeName} is not valid |
|
520 |
* @throws IOException if an I/O error occurred, or if {@link |
|
521 |
* #close} has been called and this file manager cannot be |
|
522 |
* reopened |
|
523 |
* @throws IllegalStateException if {@link #close} has been called |
|
524 |
* and this file manager cannot be reopened |
|
525 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
526 |
@Override |
33362 | 527 |
public FileObject getFileForInput(JavaFileManager.Location location, |
528 |
String packageName, |
|
529 |
String relativeName) |
|
530 |
throws IOException { |
|
531 |
proc.debug(DBG_FMGR, "getFileForInput location=%s packageName=%s\n", location, packageName); |
|
532 |
return stdFileManager.getFileForInput(location, packageName, relativeName); |
|
533 |
} |
|
534 |
||
535 |
/** |
|
536 |
* Returns a {@linkplain FileObject file object} for output |
|
537 |
* representing the specified <a href="JavaFileManager.html#relative_name">relative |
|
538 |
* name</a> in the specified package in the given location. |
|
539 |
* |
|
540 |
* <p>Optionally, this file manager might consider the sibling as |
|
541 |
* a hint for where to place the output. The exact semantics of |
|
542 |
* this hint is unspecified. The JDK compiler, javac, for |
|
543 |
* example, will place class files in the same directories as |
|
544 |
* originating source files unless a class file output directory |
|
545 |
* is provided. To facilitate this behavior, javac might provide |
|
546 |
* the originating source file as sibling when calling this |
|
547 |
* method. |
|
548 |
* |
|
549 |
* <p>If the returned object represents a {@linkplain |
|
550 |
* JavaFileObject.Kind#SOURCE source} or {@linkplain |
|
551 |
* JavaFileObject.Kind#CLASS class} file, it must be an instance |
|
552 |
* of {@link JavaFileObject}. |
|
553 |
* |
|
554 |
* <p>Informally, the file object returned by this method is |
|
555 |
* located in the concatenation of the location, package name, and |
|
556 |
* relative name or next to the sibling argument. See {@link |
|
557 |
* #getFileForInput getFileForInput} for an example. |
|
558 |
* |
|
559 |
* @param location a location |
|
560 |
* @param packageName a package name |
|
561 |
* @param relativeName a relative name |
|
562 |
* @param sibling a file object to be used as hint for placement; |
|
563 |
* might be {@code null} |
|
564 |
* @return a file object |
|
565 |
* @throws IllegalArgumentException if sibling is not known to |
|
566 |
* this file manager, or if the location is not known to this file |
|
567 |
* manager and the file manager does not support unknown |
|
568 |
* locations, or if {@code relativeName} is not valid |
|
569 |
* @throws IOException if an I/O error occurred, or if {@link |
|
570 |
* #close} has been called and this file manager cannot be |
|
571 |
* reopened |
|
572 |
* @throws IllegalStateException if {@link #close} has been called |
|
573 |
* and this file manager cannot be reopened |
|
574 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
575 |
@Override |
33362 | 576 |
public FileObject getFileForOutput(JavaFileManager.Location location, |
577 |
String packageName, |
|
578 |
String relativeName, |
|
579 |
FileObject sibling) |
|
580 |
throws IOException { |
|
581 |
throw new UnsupportedOperationException("getFileForOutput: location: " + location + |
|
582 |
", packageName: " + packageName + |
|
583 |
", relativeName: " + relativeName + |
|
584 |
", sibling: " + sibling); |
|
585 |
} |
|
586 |
||
587 |
/** |
|
588 |
* Flushes any resources opened for output by this file manager |
|
589 |
* directly or indirectly. Flushing a closed file manager has no |
|
590 |
* effect. |
|
591 |
* |
|
592 |
* @throws IOException if an I/O error occurred |
|
593 |
* @see #close |
|
594 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
595 |
@Override |
33362 | 596 |
public void flush() throws IOException { |
597 |
// Nothing to flush |
|
598 |
} |
|
599 |
||
600 |
/** |
|
601 |
* Releases any resources opened by this file manager directly or |
|
602 |
* indirectly. This might render this file manager useless and |
|
603 |
* the effect of subsequent calls to methods on this object or any |
|
604 |
* objects obtained through this object is undefined unless |
|
605 |
* explicitly allowed. However, closing a file manager which has |
|
606 |
* already been closed has no effect. |
|
607 |
* |
|
608 |
* @throws IOException if an I/O error occurred |
|
609 |
* @see #flush |
|
610 |
*/ |
|
40606
eb2c81860c86
8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents:
40603
diff
changeset
|
611 |
@Override |
33362 | 612 |
public void close() throws IOException { |
613 |
// Nothing to close |
|
614 |
} |
|
615 |
} |