author | jjg |
Tue, 07 Apr 2015 11:04:29 -0700 | |
changeset 29842 | 826ac2519523 |
parent 26266 | 2d24bda701dc |
child 40232 | 4995ab1a4558 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22165
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.processing; |
|
27 |
||
28 |
import java.io.Closeable; |
|
6573
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
29 |
import java.io.FileNotFoundException; |
10 | 30 |
import java.io.InputStream; |
31 |
import java.io.OutputStream; |
|
32 |
import java.io.FilterOutputStream; |
|
33 |
import java.io.Reader; |
|
34 |
import java.io.Writer; |
|
35 |
import java.io.FilterWriter; |
|
36 |
import java.io.PrintWriter; |
|
37 |
import java.io.IOException; |
|
6721 | 38 |
import java.util.*; |
10 | 39 |
|
40 |
import static java.util.Collections.*; |
|
41 |
||
6721 | 42 |
import javax.annotation.processing.*; |
43 |
import javax.lang.model.SourceVersion; |
|
44 |
import javax.lang.model.element.NestingKind; |
|
45 |
import javax.lang.model.element.Modifier; |
|
46 |
import javax.lang.model.element.Element; |
|
47 |
import javax.tools.*; |
|
10 | 48 |
import javax.tools.JavaFileManager.Location; |
6721 | 49 |
|
10 | 50 |
import static javax.tools.StandardLocation.SOURCE_OUTPUT; |
51 |
import static javax.tools.StandardLocation.CLASS_OUTPUT; |
|
52 |
||
6721 | 53 |
import com.sun.tools.javac.code.Lint; |
54 |
import com.sun.tools.javac.util.*; |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
55 |
import com.sun.tools.javac.util.DefinedBy.Api; |
6721 | 56 |
|
57 |
import static com.sun.tools.javac.code.Lint.LintCategory.PROCESSING; |
|
58 |
||
10 | 59 |
/** |
60 |
* The FilerImplementation class must maintain a number of |
|
61 |
* constraints. First, multiple attempts to open the same path within |
|
62 |
* the same invocation of the tool results in an IOException being |
|
63 |
* thrown. For example, trying to open the same source file twice: |
|
64 |
* |
|
65 |
* <pre> |
|
66 |
* createSourceFile("foo.Bar") |
|
67 |
* ... |
|
68 |
* createSourceFile("foo.Bar") |
|
69 |
* </pre> |
|
70 |
* |
|
71 |
* is disallowed as is opening a text file that happens to have |
|
72 |
* the same name as a source file: |
|
73 |
* |
|
74 |
* <pre> |
|
75 |
* createSourceFile("foo.Bar") |
|
76 |
* ... |
|
77 |
* createTextFile(SOURCE_TREE, "foo", new File("Bar"), null) |
|
78 |
* </pre> |
|
79 |
* |
|
80 |
* <p>Additionally, creating a source file that corresponds to an |
|
81 |
* already created class file (or vice versa) also results in an |
|
82 |
* IOException since each type can only be created once. However, if |
|
83 |
* the Filer is used to create a text file named *.java that happens |
|
84 |
* to correspond to an existing class file, a warning is *not* |
|
85 |
* generated. Similarly, a warning is not generated for a binary file |
|
86 |
* named *.class and an existing source file. |
|
87 |
* |
|
88 |
* <p>The reason for this difference is that source files and class |
|
89 |
* files are registered with the tool and can get passed on as |
|
90 |
* declarations to the next round of processing. Files that are just |
|
91 |
* named *.java and *.class are not processed in that manner; although |
|
92 |
* having extra source files and class files on the source path and |
|
93 |
* class path can alter the behavior of the tool and any final |
|
94 |
* compile. |
|
95 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
96 |
* <p><b>This is NOT part of any supported API. |
10 | 97 |
* If you write code that depends on this, you do so at your own risk. |
98 |
* This code and its internal interfaces are subject to change or |
|
99 |
* deletion without notice.</b> |
|
100 |
*/ |
|
101 |
public class JavacFiler implements Filer, Closeable { |
|
102 |
// TODO: Implement different transaction model for updating the |
|
103 |
// Filer's record keeping on file close. |
|
104 |
||
105 |
private static final String ALREADY_OPENED = |
|
106 |
"Output stream or writer has already been opened."; |
|
107 |
private static final String NOT_FOR_READING = |
|
108 |
"FileObject was not opened for reading."; |
|
109 |
private static final String NOT_FOR_WRITING = |
|
110 |
"FileObject was not opened for writing."; |
|
111 |
||
112 |
/** |
|
113 |
* Wrap a JavaFileObject to manage writing by the Filer. |
|
114 |
*/ |
|
115 |
private class FilerOutputFileObject extends ForwardingFileObject<FileObject> { |
|
116 |
private boolean opened = false; |
|
117 |
private String name; |
|
118 |
||
119 |
FilerOutputFileObject(String name, FileObject fileObject) { |
|
120 |
super(fileObject); |
|
121 |
this.name = name; |
|
122 |
} |
|
123 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
124 |
@Override @DefinedBy(Api.COMPILER) |
10 | 125 |
public synchronized OutputStream openOutputStream() throws IOException { |
126 |
if (opened) |
|
127 |
throw new IOException(ALREADY_OPENED); |
|
128 |
opened = true; |
|
129 |
return new FilerOutputStream(name, fileObject); |
|
130 |
} |
|
131 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
132 |
@Override @DefinedBy(Api.COMPILER) |
10 | 133 |
public synchronized Writer openWriter() throws IOException { |
134 |
if (opened) |
|
135 |
throw new IOException(ALREADY_OPENED); |
|
136 |
opened = true; |
|
137 |
return new FilerWriter(name, fileObject); |
|
138 |
} |
|
139 |
||
140 |
// Three anti-literacy methods |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
141 |
@Override @DefinedBy(Api.COMPILER) |
10 | 142 |
public InputStream openInputStream() throws IOException { |
143 |
throw new IllegalStateException(NOT_FOR_READING); |
|
144 |
} |
|
145 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
146 |
@Override @DefinedBy(Api.COMPILER) |
10 | 147 |
public Reader openReader(boolean ignoreEncodingErrors) throws IOException { |
148 |
throw new IllegalStateException(NOT_FOR_READING); |
|
149 |
} |
|
150 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
151 |
@Override @DefinedBy(Api.COMPILER) |
10 | 152 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
153 |
throw new IllegalStateException(NOT_FOR_READING); |
|
154 |
} |
|
155 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
156 |
@Override @DefinedBy(Api.COMPILER) |
10 | 157 |
public boolean delete() { |
158 |
return false; |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
private class FilerOutputJavaFileObject extends FilerOutputFileObject implements JavaFileObject { |
|
163 |
private final JavaFileObject javaFileObject; |
|
164 |
FilerOutputJavaFileObject(String name, JavaFileObject javaFileObject) { |
|
165 |
super(name, javaFileObject); |
|
166 |
this.javaFileObject = javaFileObject; |
|
167 |
} |
|
168 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
169 |
@DefinedBy(Api.COMPILER) |
10 | 170 |
public JavaFileObject.Kind getKind() { |
171 |
return javaFileObject.getKind(); |
|
172 |
} |
|
173 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
174 |
@DefinedBy(Api.COMPILER) |
10 | 175 |
public boolean isNameCompatible(String simpleName, |
176 |
JavaFileObject.Kind kind) { |
|
177 |
return javaFileObject.isNameCompatible(simpleName, kind); |
|
178 |
} |
|
179 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
180 |
@DefinedBy(Api.COMPILER) |
10 | 181 |
public NestingKind getNestingKind() { |
182 |
return javaFileObject.getNestingKind(); |
|
183 |
} |
|
184 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
185 |
@DefinedBy(Api.COMPILER) |
10 | 186 |
public Modifier getAccessLevel() { |
187 |
return javaFileObject.getAccessLevel(); |
|
188 |
} |
|
189 |
} |
|
190 |
||
191 |
/** |
|
192 |
* Wrap a JavaFileObject to manage reading by the Filer. |
|
193 |
*/ |
|
194 |
private class FilerInputFileObject extends ForwardingFileObject<FileObject> { |
|
195 |
FilerInputFileObject(FileObject fileObject) { |
|
196 |
super(fileObject); |
|
197 |
} |
|
198 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
199 |
@Override @DefinedBy(Api.COMPILER) |
10 | 200 |
public OutputStream openOutputStream() throws IOException { |
201 |
throw new IllegalStateException(NOT_FOR_WRITING); |
|
202 |
} |
|
203 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
204 |
@Override @DefinedBy(Api.COMPILER) |
10 | 205 |
public Writer openWriter() throws IOException { |
206 |
throw new IllegalStateException(NOT_FOR_WRITING); |
|
207 |
} |
|
208 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
209 |
@Override @DefinedBy(Api.COMPILER) |
10 | 210 |
public boolean delete() { |
211 |
return false; |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
private class FilerInputJavaFileObject extends FilerInputFileObject implements JavaFileObject { |
|
216 |
private final JavaFileObject javaFileObject; |
|
217 |
FilerInputJavaFileObject(JavaFileObject javaFileObject) { |
|
218 |
super(javaFileObject); |
|
219 |
this.javaFileObject = javaFileObject; |
|
220 |
} |
|
221 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
222 |
@DefinedBy(Api.COMPILER) |
10 | 223 |
public JavaFileObject.Kind getKind() { |
224 |
return javaFileObject.getKind(); |
|
225 |
} |
|
226 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
227 |
@DefinedBy(Api.COMPILER) |
10 | 228 |
public boolean isNameCompatible(String simpleName, |
229 |
JavaFileObject.Kind kind) { |
|
230 |
return javaFileObject.isNameCompatible(simpleName, kind); |
|
231 |
} |
|
232 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
233 |
@DefinedBy(Api.COMPILER) |
10 | 234 |
public NestingKind getNestingKind() { |
235 |
return javaFileObject.getNestingKind(); |
|
236 |
} |
|
237 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
238 |
@DefinedBy(Api.COMPILER) |
10 | 239 |
public Modifier getAccessLevel() { |
240 |
return javaFileObject.getAccessLevel(); |
|
241 |
} |
|
242 |
} |
|
243 |
||
244 |
||
245 |
/** |
|
246 |
* Wrap a {@code OutputStream} returned from the {@code |
|
247 |
* JavaFileManager} to properly register source or class files |
|
248 |
* when they are closed. |
|
249 |
*/ |
|
250 |
private class FilerOutputStream extends FilterOutputStream { |
|
251 |
String typeName; |
|
252 |
FileObject fileObject; |
|
253 |
boolean closed = false; |
|
254 |
||
255 |
/** |
|
256 |
* @param typeName name of class or {@code null} if just a |
|
257 |
* binary file |
|
258 |
*/ |
|
259 |
FilerOutputStream(String typeName, FileObject fileObject) throws IOException { |
|
260 |
super(fileObject.openOutputStream()); |
|
261 |
this.typeName = typeName; |
|
262 |
this.fileObject = fileObject; |
|
263 |
} |
|
264 |
||
265 |
public synchronized void close() throws IOException { |
|
266 |
if (!closed) { |
|
267 |
closed = true; |
|
268 |
/* |
|
269 |
* If an IOException occurs when closing the underlying |
|
270 |
* stream, still try to process the file. |
|
271 |
*/ |
|
272 |
||
273 |
closeFileObject(typeName, fileObject); |
|
274 |
out.close(); |
|
275 |
} |
|
276 |
} |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Wrap a {@code Writer} returned from the {@code JavaFileManager} |
|
281 |
* to properly register source or class files when they are |
|
282 |
* closed. |
|
283 |
*/ |
|
284 |
private class FilerWriter extends FilterWriter { |
|
285 |
String typeName; |
|
286 |
FileObject fileObject; |
|
287 |
boolean closed = false; |
|
288 |
||
289 |
/** |
|
290 |
* @param fileObject the fileObject to be written to |
|
291 |
* @param typeName name of source file or {@code null} if just a |
|
292 |
* text file |
|
293 |
*/ |
|
294 |
FilerWriter(String typeName, FileObject fileObject) throws IOException { |
|
295 |
super(fileObject.openWriter()); |
|
296 |
this.typeName = typeName; |
|
297 |
this.fileObject = fileObject; |
|
298 |
} |
|
299 |
||
300 |
public synchronized void close() throws IOException { |
|
301 |
if (!closed) { |
|
302 |
closed = true; |
|
303 |
/* |
|
304 |
* If an IOException occurs when closing the underlying |
|
305 |
* Writer, still try to process the file. |
|
306 |
*/ |
|
307 |
||
308 |
closeFileObject(typeName, fileObject); |
|
309 |
out.close(); |
|
310 |
} |
|
311 |
} |
|
312 |
} |
|
313 |
||
314 |
JavaFileManager fileManager; |
|
315 |
Log log; |
|
316 |
Context context; |
|
317 |
boolean lastRound; |
|
318 |
||
319 |
private final boolean lint; |
|
320 |
||
321 |
/** |
|
322 |
* Logical names of all created files. This set must be |
|
323 |
* synchronized. |
|
324 |
*/ |
|
325 |
private final Set<FileObject> fileObjectHistory; |
|
326 |
||
327 |
/** |
|
328 |
* Names of types that have had files created but not closed. |
|
329 |
*/ |
|
330 |
private final Set<String> openTypeNames; |
|
331 |
||
332 |
/** |
|
333 |
* Names of source files closed in this round. This set must be |
|
334 |
* synchronized. Its iterators should preserve insertion order. |
|
335 |
*/ |
|
336 |
private Set<String> generatedSourceNames; |
|
337 |
||
338 |
/** |
|
339 |
* Names and class files of the class files closed in this round. |
|
340 |
* This set must be synchronized. Its iterators should preserve |
|
341 |
* insertion order. |
|
342 |
*/ |
|
343 |
private final Map<String, JavaFileObject> generatedClasses; |
|
344 |
||
345 |
/** |
|
346 |
* JavaFileObjects for source files closed in this round. This |
|
347 |
* set must be synchronized. Its iterators should preserve |
|
348 |
* insertion order. |
|
349 |
*/ |
|
350 |
private Set<JavaFileObject> generatedSourceFileObjects; |
|
351 |
||
352 |
/** |
|
353 |
* Names of all created source files. Its iterators should |
|
354 |
* preserve insertion order. |
|
355 |
*/ |
|
356 |
private final Set<String> aggregateGeneratedSourceNames; |
|
357 |
||
358 |
/** |
|
359 |
* Names of all created class files. Its iterators should |
|
360 |
* preserve insertion order. |
|
361 |
*/ |
|
362 |
private final Set<String> aggregateGeneratedClassNames; |
|
363 |
||
364 |
||
365 |
JavacFiler(Context context) { |
|
366 |
this.context = context; |
|
367 |
fileManager = context.get(JavaFileManager.class); |
|
368 |
||
369 |
log = Log.instance(context); |
|
370 |
||
22165
ec53c8946fc2
8030807: langtools should still build using jdk 7
vromero
parents:
22163
diff
changeset
|
371 |
fileObjectHistory = synchronizedSet(new LinkedHashSet<FileObject>()); |
ec53c8946fc2
8030807: langtools should still build using jdk 7
vromero
parents:
22163
diff
changeset
|
372 |
generatedSourceNames = synchronizedSet(new LinkedHashSet<String>()); |
ec53c8946fc2
8030807: langtools should still build using jdk 7
vromero
parents:
22163
diff
changeset
|
373 |
generatedSourceFileObjects = synchronizedSet(new LinkedHashSet<JavaFileObject>()); |
10 | 374 |
|
22165
ec53c8946fc2
8030807: langtools should still build using jdk 7
vromero
parents:
22163
diff
changeset
|
375 |
generatedClasses = synchronizedMap(new LinkedHashMap<String, JavaFileObject>()); |
10 | 376 |
|
22165
ec53c8946fc2
8030807: langtools should still build using jdk 7
vromero
parents:
22163
diff
changeset
|
377 |
openTypeNames = synchronizedSet(new LinkedHashSet<String>()); |
10 | 378 |
|
22163 | 379 |
aggregateGeneratedSourceNames = new LinkedHashSet<>(); |
380 |
aggregateGeneratedClassNames = new LinkedHashSet<>(); |
|
10 | 381 |
|
6721 | 382 |
lint = (Lint.instance(context)).isEnabled(PROCESSING); |
10 | 383 |
} |
384 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
385 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 386 |
public JavaFileObject createSourceFile(CharSequence name, |
387 |
Element... originatingElements) throws IOException { |
|
388 |
return createSourceOrClassFile(true, name.toString()); |
|
389 |
} |
|
390 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
391 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 392 |
public JavaFileObject createClassFile(CharSequence name, |
393 |
Element... originatingElements) throws IOException { |
|
394 |
return createSourceOrClassFile(false, name.toString()); |
|
395 |
} |
|
396 |
||
397 |
private JavaFileObject createSourceOrClassFile(boolean isSourceFile, String name) throws IOException { |
|
6157
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
398 |
if (lint) { |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
399 |
int periodIndex = name.lastIndexOf("."); |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
400 |
if (periodIndex != -1) { |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
401 |
String base = name.substring(periodIndex); |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
402 |
String extn = (isSourceFile ? ".java" : ".class"); |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
403 |
if (base.equals(extn)) |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
404 |
log.warning("proc.suspicious.class.name", name, extn); |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
405 |
} |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
406 |
} |
10 | 407 |
checkNameAndExistence(name, isSourceFile); |
408 |
Location loc = (isSourceFile ? SOURCE_OUTPUT : CLASS_OUTPUT); |
|
409 |
JavaFileObject.Kind kind = (isSourceFile ? |
|
410 |
JavaFileObject.Kind.SOURCE : |
|
411 |
JavaFileObject.Kind.CLASS); |
|
412 |
||
413 |
JavaFileObject fileObject = |
|
414 |
fileManager.getJavaFileForOutput(loc, name, kind, null); |
|
415 |
checkFileReopening(fileObject, true); |
|
416 |
||
417 |
if (lastRound) |
|
418 |
log.warning("proc.file.create.last.round", name); |
|
419 |
||
420 |
if (isSourceFile) |
|
421 |
aggregateGeneratedSourceNames.add(name); |
|
422 |
else |
|
423 |
aggregateGeneratedClassNames.add(name); |
|
424 |
openTypeNames.add(name); |
|
425 |
||
426 |
return new FilerOutputJavaFileObject(name, fileObject); |
|
427 |
} |
|
428 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
429 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 430 |
public FileObject createResource(JavaFileManager.Location location, |
431 |
CharSequence pkg, |
|
432 |
CharSequence relativeName, |
|
433 |
Element... originatingElements) throws IOException { |
|
434 |
locationCheck(location); |
|
435 |
||
436 |
String strPkg = pkg.toString(); |
|
437 |
if (strPkg.length() > 0) |
|
438 |
checkName(strPkg); |
|
439 |
||
440 |
FileObject fileObject = |
|
441 |
fileManager.getFileForOutput(location, strPkg, |
|
442 |
relativeName.toString(), null); |
|
443 |
checkFileReopening(fileObject, true); |
|
444 |
||
445 |
if (fileObject instanceof JavaFileObject) |
|
446 |
return new FilerOutputJavaFileObject(null, (JavaFileObject)fileObject); |
|
447 |
else |
|
448 |
return new FilerOutputFileObject(null, fileObject); |
|
449 |
} |
|
450 |
||
451 |
private void locationCheck(JavaFileManager.Location location) { |
|
452 |
if (location instanceof StandardLocation) { |
|
453 |
StandardLocation stdLoc = (StandardLocation) location; |
|
454 |
if (!stdLoc.isOutputLocation()) |
|
455 |
throw new IllegalArgumentException("Resource creation not supported in location " + |
|
456 |
stdLoc); |
|
457 |
} |
|
458 |
} |
|
459 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
460 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 461 |
public FileObject getResource(JavaFileManager.Location location, |
462 |
CharSequence pkg, |
|
463 |
CharSequence relativeName) throws IOException { |
|
464 |
String strPkg = pkg.toString(); |
|
465 |
if (strPkg.length() > 0) |
|
466 |
checkName(strPkg); |
|
467 |
||
468 |
// TODO: Only support reading resources in selected output |
|
469 |
// locations? Only allow reading of non-source, non-class |
|
470 |
// files from the supported input locations? |
|
10633
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
471 |
|
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
472 |
// In the following, getFileForInput is the "obvious" method |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
473 |
// to use, but it does not have the "obvious" semantics for |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
474 |
// SOURCE_OUTPUT and CLASS_OUTPUT. Conversely, getFileForOutput |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
475 |
// does not have the correct semantics for any "path" location |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
476 |
// with more than one component. So, for now, we use a hybrid |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
477 |
// invocation. |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
478 |
FileObject fileObject; |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
479 |
if (location.isOutputLocation()) { |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
480 |
fileObject = fileManager.getFileForOutput(location, |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
481 |
pkg.toString(), |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
482 |
relativeName.toString(), |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
483 |
null); |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
484 |
} else { |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
485 |
fileObject = fileManager.getFileForInput(location, |
6573
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
486 |
pkg.toString(), |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
487 |
relativeName.toString()); |
10633
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
488 |
} |
6573
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
489 |
if (fileObject == null) { |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
490 |
String name = (pkg.length() == 0) |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
491 |
? relativeName.toString() : (pkg + "/" + relativeName); |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
492 |
throw new FileNotFoundException(name); |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
493 |
} |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
494 |
|
10 | 495 |
// If the path was already opened for writing, throw an exception. |
496 |
checkFileReopening(fileObject, false); |
|
497 |
return new FilerInputFileObject(fileObject); |
|
498 |
} |
|
499 |
||
500 |
private void checkName(String name) throws FilerException { |
|
501 |
checkName(name, false); |
|
502 |
} |
|
503 |
||
504 |
private void checkName(String name, boolean allowUnnamedPackageInfo) throws FilerException { |
|
505 |
if (!SourceVersion.isName(name) && !isPackageInfo(name, allowUnnamedPackageInfo)) { |
|
506 |
if (lint) |
|
507 |
log.warning("proc.illegal.file.name", name); |
|
508 |
throw new FilerException("Illegal name " + name); |
|
509 |
} |
|
510 |
} |
|
511 |
||
512 |
private boolean isPackageInfo(String name, boolean allowUnnamedPackageInfo) { |
|
513 |
// Is the name of the form "package-info" or |
|
514 |
// "foo.bar.package-info"? |
|
515 |
final String PKG_INFO = "package-info"; |
|
516 |
int periodIndex = name.lastIndexOf("."); |
|
517 |
if (periodIndex == -1) { |
|
518 |
return allowUnnamedPackageInfo ? name.equals(PKG_INFO) : false; |
|
519 |
} else { |
|
520 |
// "foo.bar.package-info." illegal |
|
521 |
String prefix = name.substring(0, periodIndex); |
|
522 |
String simple = name.substring(periodIndex+1); |
|
523 |
return SourceVersion.isName(prefix) && simple.equals(PKG_INFO); |
|
524 |
} |
|
525 |
} |
|
526 |
||
527 |
private void checkNameAndExistence(String typename, boolean allowUnnamedPackageInfo) throws FilerException { |
|
528 |
// TODO: Check if type already exists on source or class path? |
|
529 |
// If so, use warning message key proc.type.already.exists |
|
530 |
checkName(typename, allowUnnamedPackageInfo); |
|
531 |
if (aggregateGeneratedSourceNames.contains(typename) || |
|
532 |
aggregateGeneratedClassNames.contains(typename)) { |
|
533 |
if (lint) |
|
534 |
log.warning("proc.type.recreate", typename); |
|
535 |
throw new FilerException("Attempt to recreate a file for type " + typename); |
|
536 |
} |
|
537 |
} |
|
538 |
||
539 |
/** |
|
540 |
* Check to see if the file has already been opened; if so, throw |
|
541 |
* an exception, otherwise add it to the set of files. |
|
542 |
*/ |
|
543 |
private void checkFileReopening(FileObject fileObject, boolean addToHistory) throws FilerException { |
|
544 |
for(FileObject veteran : fileObjectHistory) { |
|
545 |
if (fileManager.isSameFile(veteran, fileObject)) { |
|
546 |
if (lint) |
|
547 |
log.warning("proc.file.reopening", fileObject.getName()); |
|
548 |
throw new FilerException("Attempt to reopen a file for path " + fileObject.getName()); |
|
549 |
} |
|
550 |
} |
|
551 |
if (addToHistory) |
|
552 |
fileObjectHistory.add(fileObject); |
|
553 |
} |
|
554 |
||
555 |
public boolean newFiles() { |
|
556 |
return (!generatedSourceNames.isEmpty()) |
|
557 |
|| (!generatedClasses.isEmpty()); |
|
558 |
} |
|
559 |
||
560 |
public Set<String> getGeneratedSourceNames() { |
|
561 |
return generatedSourceNames; |
|
562 |
} |
|
563 |
||
564 |
public Set<JavaFileObject> getGeneratedSourceFileObjects() { |
|
565 |
return generatedSourceFileObjects; |
|
566 |
} |
|
567 |
||
568 |
public Map<String, JavaFileObject> getGeneratedClasses() { |
|
569 |
return generatedClasses; |
|
570 |
} |
|
571 |
||
572 |
public void warnIfUnclosedFiles() { |
|
573 |
if (!openTypeNames.isEmpty()) |
|
574 |
log.warning("proc.unclosed.type.files", openTypeNames.toString()); |
|
575 |
} |
|
576 |
||
577 |
/** |
|
578 |
* Update internal state for a new round. |
|
579 |
*/ |
|
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22165
diff
changeset
|
580 |
public void newRound() { |
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6157
diff
changeset
|
581 |
clearRoundState(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6157
diff
changeset
|
582 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6157
diff
changeset
|
583 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6157
diff
changeset
|
584 |
void setLastRound(boolean lastRound) { |
10 | 585 |
this.lastRound = lastRound; |
586 |
} |
|
587 |
||
588 |
public void close() { |
|
589 |
clearRoundState(); |
|
590 |
// Cross-round state |
|
591 |
fileObjectHistory.clear(); |
|
592 |
openTypeNames.clear(); |
|
593 |
aggregateGeneratedSourceNames.clear(); |
|
594 |
aggregateGeneratedClassNames.clear(); |
|
595 |
} |
|
596 |
||
597 |
private void clearRoundState() { |
|
598 |
generatedSourceNames.clear(); |
|
599 |
generatedSourceFileObjects.clear(); |
|
600 |
generatedClasses.clear(); |
|
601 |
} |
|
602 |
||
603 |
/** |
|
604 |
* Debugging function to display internal state. |
|
605 |
*/ |
|
606 |
public void displayState() { |
|
607 |
PrintWriter xout = context.get(Log.outKey); |
|
608 |
xout.println("File Object History : " + fileObjectHistory); |
|
609 |
xout.println("Open Type Names : " + openTypeNames); |
|
610 |
xout.println("Gen. Src Names : " + generatedSourceNames); |
|
611 |
xout.println("Gen. Cls Names : " + generatedClasses.keySet()); |
|
612 |
xout.println("Agg. Gen. Src Names : " + aggregateGeneratedSourceNames); |
|
613 |
xout.println("Agg. Gen. Cls Names : " + aggregateGeneratedClassNames); |
|
614 |
} |
|
615 |
||
616 |
public String toString() { |
|
617 |
return "javac Filer"; |
|
618 |
} |
|
619 |
||
620 |
/** |
|
621 |
* Upon close, register files opened by create{Source, Class}File |
|
622 |
* for annotation processing. |
|
623 |
*/ |
|
624 |
private void closeFileObject(String typeName, FileObject fileObject) { |
|
625 |
/* |
|
626 |
* If typeName is non-null, the file object was opened as a |
|
627 |
* source or class file by the user. If a file was opened as |
|
628 |
* a resource, typeName will be null and the file is *not* |
|
629 |
* subject to annotation processing. |
|
630 |
*/ |
|
631 |
if ((typeName != null)) { |
|
632 |
if (!(fileObject instanceof JavaFileObject)) |
|
633 |
throw new AssertionError("JavaFileOject not found for " + fileObject); |
|
634 |
JavaFileObject javaFileObject = (JavaFileObject)fileObject; |
|
635 |
switch(javaFileObject.getKind()) { |
|
636 |
case SOURCE: |
|
637 |
generatedSourceNames.add(typeName); |
|
638 |
generatedSourceFileObjects.add(javaFileObject); |
|
639 |
openTypeNames.remove(typeName); |
|
640 |
break; |
|
641 |
||
642 |
case CLASS: |
|
643 |
generatedClasses.put(typeName, javaFileObject); |
|
644 |
openTypeNames.remove(typeName); |
|
645 |
break; |
|
646 |
||
647 |
default: |
|
648 |
break; |
|
649 |
} |
|
650 |
} |
|
651 |
} |
|
652 |
||
653 |
} |