author | jlahoda |
Thu, 26 Jan 2017 14:11:38 +0100 | |
changeset 43368 | cabe410a7a5c |
parent 42815 | 050370edaade |
child 44291 | e1b620ac6c98 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
40232
4995ab1a4558
8162359: javac should use stdout for --help and --version
jjg
parents:
26266
diff
changeset
|
2 |
* Copyright (c) 2005, 2016, 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; |
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
54 |
import com.sun.tools.javac.code.Symbol.ClassSymbol; |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
55 |
import com.sun.tools.javac.code.Symbol.ModuleSymbol; |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
56 |
import com.sun.tools.javac.code.Symtab; |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
57 |
import com.sun.tools.javac.comp.Modules; |
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
58 |
import com.sun.tools.javac.model.JavacElements; |
6721 | 59 |
import com.sun.tools.javac.util.*; |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
60 |
import com.sun.tools.javac.util.DefinedBy.Api; |
6721 | 61 |
|
62 |
import static com.sun.tools.javac.code.Lint.LintCategory.PROCESSING; |
|
63 |
||
10 | 64 |
/** |
65 |
* The FilerImplementation class must maintain a number of |
|
66 |
* constraints. First, multiple attempts to open the same path within |
|
67 |
* the same invocation of the tool results in an IOException being |
|
68 |
* thrown. For example, trying to open the same source file twice: |
|
69 |
* |
|
70 |
* <pre> |
|
71 |
* createSourceFile("foo.Bar") |
|
72 |
* ... |
|
73 |
* createSourceFile("foo.Bar") |
|
74 |
* </pre> |
|
75 |
* |
|
76 |
* is disallowed as is opening a text file that happens to have |
|
77 |
* the same name as a source file: |
|
78 |
* |
|
79 |
* <pre> |
|
80 |
* createSourceFile("foo.Bar") |
|
81 |
* ... |
|
82 |
* createTextFile(SOURCE_TREE, "foo", new File("Bar"), null) |
|
83 |
* </pre> |
|
84 |
* |
|
85 |
* <p>Additionally, creating a source file that corresponds to an |
|
86 |
* already created class file (or vice versa) also results in an |
|
87 |
* IOException since each type can only be created once. However, if |
|
88 |
* the Filer is used to create a text file named *.java that happens |
|
89 |
* to correspond to an existing class file, a warning is *not* |
|
90 |
* generated. Similarly, a warning is not generated for a binary file |
|
91 |
* named *.class and an existing source file. |
|
92 |
* |
|
93 |
* <p>The reason for this difference is that source files and class |
|
94 |
* files are registered with the tool and can get passed on as |
|
95 |
* declarations to the next round of processing. Files that are just |
|
96 |
* named *.java and *.class are not processed in that manner; although |
|
97 |
* having extra source files and class files on the source path and |
|
98 |
* class path can alter the behavior of the tool and any final |
|
99 |
* compile. |
|
100 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
101 |
* <p><b>This is NOT part of any supported API. |
10 | 102 |
* If you write code that depends on this, you do so at your own risk. |
103 |
* This code and its internal interfaces are subject to change or |
|
104 |
* deletion without notice.</b> |
|
105 |
*/ |
|
106 |
public class JavacFiler implements Filer, Closeable { |
|
107 |
// TODO: Implement different transaction model for updating the |
|
108 |
// Filer's record keeping on file close. |
|
109 |
||
110 |
private static final String ALREADY_OPENED = |
|
111 |
"Output stream or writer has already been opened."; |
|
112 |
private static final String NOT_FOR_READING = |
|
113 |
"FileObject was not opened for reading."; |
|
114 |
private static final String NOT_FOR_WRITING = |
|
115 |
"FileObject was not opened for writing."; |
|
116 |
||
117 |
/** |
|
118 |
* Wrap a JavaFileObject to manage writing by the Filer. |
|
119 |
*/ |
|
120 |
private class FilerOutputFileObject extends ForwardingFileObject<FileObject> { |
|
121 |
private boolean opened = false; |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
122 |
private ModuleSymbol mod; |
10 | 123 |
private String name; |
124 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
125 |
FilerOutputFileObject(ModuleSymbol mod, String name, FileObject fileObject) { |
10 | 126 |
super(fileObject); |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
127 |
this.mod = mod; |
10 | 128 |
this.name = name; |
129 |
} |
|
130 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
131 |
@Override @DefinedBy(Api.COMPILER) |
10 | 132 |
public synchronized OutputStream openOutputStream() throws IOException { |
133 |
if (opened) |
|
134 |
throw new IOException(ALREADY_OPENED); |
|
135 |
opened = true; |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
136 |
return new FilerOutputStream(mod, name, fileObject); |
10 | 137 |
} |
138 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
139 |
@Override @DefinedBy(Api.COMPILER) |
10 | 140 |
public synchronized Writer openWriter() throws IOException { |
141 |
if (opened) |
|
142 |
throw new IOException(ALREADY_OPENED); |
|
143 |
opened = true; |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
144 |
return new FilerWriter(mod, name, fileObject); |
10 | 145 |
} |
146 |
||
147 |
// Three anti-literacy methods |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
148 |
@Override @DefinedBy(Api.COMPILER) |
10 | 149 |
public InputStream openInputStream() throws IOException { |
150 |
throw new IllegalStateException(NOT_FOR_READING); |
|
151 |
} |
|
152 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
153 |
@Override @DefinedBy(Api.COMPILER) |
10 | 154 |
public Reader openReader(boolean ignoreEncodingErrors) throws IOException { |
155 |
throw new IllegalStateException(NOT_FOR_READING); |
|
156 |
} |
|
157 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
158 |
@Override @DefinedBy(Api.COMPILER) |
10 | 159 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
160 |
throw new IllegalStateException(NOT_FOR_READING); |
|
161 |
} |
|
162 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
163 |
@Override @DefinedBy(Api.COMPILER) |
10 | 164 |
public boolean delete() { |
165 |
return false; |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
private class FilerOutputJavaFileObject extends FilerOutputFileObject implements JavaFileObject { |
|
170 |
private final JavaFileObject javaFileObject; |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
171 |
FilerOutputJavaFileObject(ModuleSymbol mod, String name, JavaFileObject javaFileObject) { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
172 |
super(mod, name, javaFileObject); |
10 | 173 |
this.javaFileObject = javaFileObject; |
174 |
} |
|
175 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
176 |
@DefinedBy(Api.COMPILER) |
10 | 177 |
public JavaFileObject.Kind getKind() { |
178 |
return javaFileObject.getKind(); |
|
179 |
} |
|
180 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
181 |
@DefinedBy(Api.COMPILER) |
10 | 182 |
public boolean isNameCompatible(String simpleName, |
183 |
JavaFileObject.Kind kind) { |
|
184 |
return javaFileObject.isNameCompatible(simpleName, kind); |
|
185 |
} |
|
186 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
187 |
@DefinedBy(Api.COMPILER) |
10 | 188 |
public NestingKind getNestingKind() { |
189 |
return javaFileObject.getNestingKind(); |
|
190 |
} |
|
191 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
192 |
@DefinedBy(Api.COMPILER) |
10 | 193 |
public Modifier getAccessLevel() { |
194 |
return javaFileObject.getAccessLevel(); |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Wrap a JavaFileObject to manage reading by the Filer. |
|
200 |
*/ |
|
201 |
private class FilerInputFileObject extends ForwardingFileObject<FileObject> { |
|
202 |
FilerInputFileObject(FileObject fileObject) { |
|
203 |
super(fileObject); |
|
204 |
} |
|
205 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
206 |
@Override @DefinedBy(Api.COMPILER) |
10 | 207 |
public OutputStream openOutputStream() throws IOException { |
208 |
throw new IllegalStateException(NOT_FOR_WRITING); |
|
209 |
} |
|
210 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
211 |
@Override @DefinedBy(Api.COMPILER) |
10 | 212 |
public Writer openWriter() throws IOException { |
213 |
throw new IllegalStateException(NOT_FOR_WRITING); |
|
214 |
} |
|
215 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
216 |
@Override @DefinedBy(Api.COMPILER) |
10 | 217 |
public boolean delete() { |
218 |
return false; |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
private class FilerInputJavaFileObject extends FilerInputFileObject implements JavaFileObject { |
|
223 |
private final JavaFileObject javaFileObject; |
|
224 |
FilerInputJavaFileObject(JavaFileObject javaFileObject) { |
|
225 |
super(javaFileObject); |
|
226 |
this.javaFileObject = javaFileObject; |
|
227 |
} |
|
228 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
229 |
@DefinedBy(Api.COMPILER) |
10 | 230 |
public JavaFileObject.Kind getKind() { |
231 |
return javaFileObject.getKind(); |
|
232 |
} |
|
233 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
234 |
@DefinedBy(Api.COMPILER) |
10 | 235 |
public boolean isNameCompatible(String simpleName, |
236 |
JavaFileObject.Kind kind) { |
|
237 |
return javaFileObject.isNameCompatible(simpleName, kind); |
|
238 |
} |
|
239 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
240 |
@DefinedBy(Api.COMPILER) |
10 | 241 |
public NestingKind getNestingKind() { |
242 |
return javaFileObject.getNestingKind(); |
|
243 |
} |
|
244 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
245 |
@DefinedBy(Api.COMPILER) |
10 | 246 |
public Modifier getAccessLevel() { |
247 |
return javaFileObject.getAccessLevel(); |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
||
252 |
/** |
|
253 |
* Wrap a {@code OutputStream} returned from the {@code |
|
254 |
* JavaFileManager} to properly register source or class files |
|
255 |
* when they are closed. |
|
256 |
*/ |
|
257 |
private class FilerOutputStream extends FilterOutputStream { |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
258 |
ModuleSymbol mod; |
10 | 259 |
String typeName; |
260 |
FileObject fileObject; |
|
261 |
boolean closed = false; |
|
262 |
||
263 |
/** |
|
264 |
* @param typeName name of class or {@code null} if just a |
|
265 |
* binary file |
|
266 |
*/ |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
267 |
FilerOutputStream(ModuleSymbol mod, String typeName, FileObject fileObject) throws IOException { |
10 | 268 |
super(fileObject.openOutputStream()); |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
269 |
this.mod = mod; |
10 | 270 |
this.typeName = typeName; |
271 |
this.fileObject = fileObject; |
|
272 |
} |
|
273 |
||
274 |
public synchronized void close() throws IOException { |
|
275 |
if (!closed) { |
|
276 |
closed = true; |
|
277 |
/* |
|
278 |
* If an IOException occurs when closing the underlying |
|
279 |
* stream, still try to process the file. |
|
280 |
*/ |
|
281 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
282 |
closeFileObject(mod, typeName, fileObject); |
10 | 283 |
out.close(); |
284 |
} |
|
285 |
} |
|
286 |
} |
|
287 |
||
288 |
/** |
|
289 |
* Wrap a {@code Writer} returned from the {@code JavaFileManager} |
|
290 |
* to properly register source or class files when they are |
|
291 |
* closed. |
|
292 |
*/ |
|
293 |
private class FilerWriter extends FilterWriter { |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
294 |
ModuleSymbol mod; |
10 | 295 |
String typeName; |
296 |
FileObject fileObject; |
|
297 |
boolean closed = false; |
|
298 |
||
299 |
/** |
|
300 |
* @param fileObject the fileObject to be written to |
|
301 |
* @param typeName name of source file or {@code null} if just a |
|
302 |
* text file |
|
303 |
*/ |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
304 |
FilerWriter(ModuleSymbol mod, String typeName, FileObject fileObject) throws IOException { |
10 | 305 |
super(fileObject.openWriter()); |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
306 |
this.mod = mod; |
10 | 307 |
this.typeName = typeName; |
308 |
this.fileObject = fileObject; |
|
309 |
} |
|
310 |
||
311 |
public synchronized void close() throws IOException { |
|
312 |
if (!closed) { |
|
313 |
closed = true; |
|
314 |
/* |
|
315 |
* If an IOException occurs when closing the underlying |
|
316 |
* Writer, still try to process the file. |
|
317 |
*/ |
|
318 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
319 |
closeFileObject(mod, typeName, fileObject); |
10 | 320 |
out.close(); |
321 |
} |
|
322 |
} |
|
323 |
} |
|
324 |
||
325 |
JavaFileManager fileManager; |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
326 |
JavacElements elementUtils; |
10 | 327 |
Log log; |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
328 |
Modules modules; |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
329 |
Names names; |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
330 |
Symtab syms; |
10 | 331 |
Context context; |
332 |
boolean lastRound; |
|
333 |
||
334 |
private final boolean lint; |
|
335 |
||
336 |
/** |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
337 |
* Initial inputs passed to the tool. This set must be |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
338 |
* synchronized. |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
339 |
*/ |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
340 |
private final Set<FileObject> initialInputs; |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
341 |
|
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
342 |
/** |
10 | 343 |
* Logical names of all created files. This set must be |
344 |
* synchronized. |
|
345 |
*/ |
|
346 |
private final Set<FileObject> fileObjectHistory; |
|
347 |
||
348 |
/** |
|
349 |
* Names of types that have had files created but not closed. |
|
350 |
*/ |
|
351 |
private final Set<String> openTypeNames; |
|
352 |
||
353 |
/** |
|
354 |
* Names of source files closed in this round. This set must be |
|
355 |
* synchronized. Its iterators should preserve insertion order. |
|
356 |
*/ |
|
357 |
private Set<String> generatedSourceNames; |
|
358 |
||
359 |
/** |
|
360 |
* Names and class files of the class files closed in this round. |
|
361 |
* This set must be synchronized. Its iterators should preserve |
|
362 |
* insertion order. |
|
363 |
*/ |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
364 |
private final Map<ModuleSymbol, Map<String, JavaFileObject>> generatedClasses; |
10 | 365 |
|
366 |
/** |
|
367 |
* JavaFileObjects for source files closed in this round. This |
|
368 |
* set must be synchronized. Its iterators should preserve |
|
369 |
* insertion order. |
|
370 |
*/ |
|
371 |
private Set<JavaFileObject> generatedSourceFileObjects; |
|
372 |
||
373 |
/** |
|
374 |
* Names of all created source files. Its iterators should |
|
375 |
* preserve insertion order. |
|
376 |
*/ |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
377 |
private final Set<Pair<ModuleSymbol, String>> aggregateGeneratedSourceNames; |
10 | 378 |
|
379 |
/** |
|
380 |
* Names of all created class files. Its iterators should |
|
381 |
* preserve insertion order. |
|
382 |
*/ |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
383 |
private final Set<Pair<ModuleSymbol, String>> aggregateGeneratedClassNames; |
10 | 384 |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
385 |
private final Set<String> initialClassNames; |
10 | 386 |
|
387 |
JavacFiler(Context context) { |
|
388 |
this.context = context; |
|
389 |
fileManager = context.get(JavaFileManager.class); |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
390 |
elementUtils = JavacElements.instance(context); |
10 | 391 |
|
392 |
log = Log.instance(context); |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
393 |
modules = Modules.instance(context); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
394 |
names = Names.instance(context); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
395 |
syms = Symtab.instance(context); |
10 | 396 |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
397 |
initialInputs = synchronizedSet(new LinkedHashSet<>()); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
398 |
fileObjectHistory = synchronizedSet(new LinkedHashSet<>()); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
399 |
generatedSourceNames = synchronizedSet(new LinkedHashSet<>()); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
400 |
generatedSourceFileObjects = synchronizedSet(new LinkedHashSet<>()); |
10 | 401 |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
402 |
generatedClasses = synchronizedMap(new LinkedHashMap<>()); |
10 | 403 |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
404 |
openTypeNames = synchronizedSet(new LinkedHashSet<>()); |
10 | 405 |
|
22163 | 406 |
aggregateGeneratedSourceNames = new LinkedHashSet<>(); |
407 |
aggregateGeneratedClassNames = new LinkedHashSet<>(); |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
408 |
initialClassNames = new LinkedHashSet<>(); |
10 | 409 |
|
6721 | 410 |
lint = (Lint.instance(context)).isEnabled(PROCESSING); |
10 | 411 |
} |
412 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
413 |
@Override @DefinedBy(Api.ANNOTATION_PROCESSING) |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
414 |
public JavaFileObject createSourceFile(CharSequence nameAndModule, |
10 | 415 |
Element... originatingElements) throws IOException { |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
416 |
Pair<ModuleSymbol, String> moduleAndClass = checkOrInferModule(nameAndModule); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
417 |
return createSourceOrClassFile(moduleAndClass.fst, true, moduleAndClass.snd); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
418 |
} |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
419 |
|
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
420 |
@Override @DefinedBy(Api.ANNOTATION_PROCESSING) |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
421 |
public JavaFileObject createClassFile(CharSequence nameAndModule, |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
422 |
Element... originatingElements) throws IOException { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
423 |
Pair<ModuleSymbol, String> moduleAndClass = checkOrInferModule(nameAndModule); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
424 |
return createSourceOrClassFile(moduleAndClass.fst, false, moduleAndClass.snd); |
10 | 425 |
} |
426 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
427 |
private Pair<ModuleSymbol, String> checkOrInferModule(CharSequence moduleAndPkg) throws FilerException { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
428 |
String moduleAndPkgString = moduleAndPkg.toString(); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
429 |
int slash = moduleAndPkgString.indexOf('/'); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
430 |
|
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
431 |
if (slash != (-1)) { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
432 |
//module name specified: |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
433 |
String module = moduleAndPkgString.substring(0, slash); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
434 |
|
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
435 |
ModuleSymbol explicitModule = syms.getModule(names.fromString(module)); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
436 |
|
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
437 |
if (explicitModule == null) { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
438 |
throw new FilerException("Module: " + module + " does not exist."); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
439 |
} |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
440 |
|
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
441 |
if (!modules.isRootModule(explicitModule)) { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
442 |
throw new FilerException("Cannot write to the given module!"); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
443 |
} |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
444 |
|
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
445 |
return Pair.of(explicitModule, moduleAndPkgString.substring(slash + 1)); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
446 |
} else { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
447 |
if (modules.multiModuleMode) { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
448 |
throw new FilerException("No module to write to specified!"); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
449 |
} |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
450 |
|
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
451 |
return Pair.of(modules.getDefaultModule(), moduleAndPkgString); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
452 |
} |
10 | 453 |
} |
454 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
455 |
private JavaFileObject createSourceOrClassFile(ModuleSymbol mod, boolean isSourceFile, String name) throws IOException { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
456 |
Assert.checkNonNull(mod); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
457 |
|
6157
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
458 |
if (lint) { |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
459 |
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
|
460 |
if (periodIndex != -1) { |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
461 |
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
|
462 |
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
|
463 |
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
|
464 |
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
|
465 |
} |
218670cd9b1c
6972556: warning for using a file name instead of a binary name for Filer.createSourceFile
jjg
parents:
5847
diff
changeset
|
466 |
} |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
467 |
checkNameAndExistence(mod, name, isSourceFile); |
10 | 468 |
Location loc = (isSourceFile ? SOURCE_OUTPUT : CLASS_OUTPUT); |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
469 |
|
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
470 |
if (modules.multiModuleMode) { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
471 |
loc = this.fileManager.getLocationForModule(loc, mod.name.toString()); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
472 |
} |
10 | 473 |
JavaFileObject.Kind kind = (isSourceFile ? |
474 |
JavaFileObject.Kind.SOURCE : |
|
475 |
JavaFileObject.Kind.CLASS); |
|
476 |
||
477 |
JavaFileObject fileObject = |
|
478 |
fileManager.getJavaFileForOutput(loc, name, kind, null); |
|
479 |
checkFileReopening(fileObject, true); |
|
480 |
||
481 |
if (lastRound) |
|
482 |
log.warning("proc.file.create.last.round", name); |
|
483 |
||
484 |
if (isSourceFile) |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
485 |
aggregateGeneratedSourceNames.add(Pair.of(mod, name)); |
10 | 486 |
else |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
487 |
aggregateGeneratedClassNames.add(Pair.of(mod, name)); |
10 | 488 |
openTypeNames.add(name); |
489 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
490 |
return new FilerOutputJavaFileObject(mod, name, fileObject); |
10 | 491 |
} |
492 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
493 |
@Override @DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 494 |
public FileObject createResource(JavaFileManager.Location location, |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
495 |
CharSequence moduleAndPkg, |
10 | 496 |
CharSequence relativeName, |
497 |
Element... originatingElements) throws IOException { |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
498 |
Pair<ModuleSymbol, String> moduleAndPackage = checkOrInferModule(moduleAndPkg); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
499 |
ModuleSymbol msym = moduleAndPackage.fst; |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
500 |
String pkg = moduleAndPackage.snd; |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
501 |
|
10 | 502 |
locationCheck(location); |
503 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
504 |
if (modules.multiModuleMode) { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
505 |
Assert.checkNonNull(msym); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
506 |
location = this.fileManager.getLocationForModule(location, msym.name.toString()); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
507 |
} |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
508 |
|
10 | 509 |
String strPkg = pkg.toString(); |
510 |
if (strPkg.length() > 0) |
|
511 |
checkName(strPkg); |
|
512 |
||
513 |
FileObject fileObject = |
|
514 |
fileManager.getFileForOutput(location, strPkg, |
|
515 |
relativeName.toString(), null); |
|
516 |
checkFileReopening(fileObject, true); |
|
517 |
||
518 |
if (fileObject instanceof JavaFileObject) |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
519 |
return new FilerOutputJavaFileObject(msym, null, (JavaFileObject)fileObject); |
10 | 520 |
else |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
521 |
return new FilerOutputFileObject(msym, null, fileObject); |
10 | 522 |
} |
523 |
||
524 |
private void locationCheck(JavaFileManager.Location location) { |
|
525 |
if (location instanceof StandardLocation) { |
|
526 |
StandardLocation stdLoc = (StandardLocation) location; |
|
527 |
if (!stdLoc.isOutputLocation()) |
|
528 |
throw new IllegalArgumentException("Resource creation not supported in location " + |
|
529 |
stdLoc); |
|
530 |
} |
|
531 |
} |
|
532 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
533 |
@Override @DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 534 |
public FileObject getResource(JavaFileManager.Location location, |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
535 |
CharSequence moduleAndPkg, |
10 | 536 |
CharSequence relativeName) throws IOException { |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
537 |
Pair<ModuleSymbol, String> moduleAndPackage = checkOrInferModule(moduleAndPkg); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
538 |
ModuleSymbol msym = moduleAndPackage.fst; |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
539 |
String pkg = moduleAndPackage.snd; |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
540 |
|
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
541 |
if (modules.multiModuleMode) { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
542 |
Assert.checkNonNull(msym); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
543 |
location = this.fileManager.getLocationForModule(location, msym.name.toString()); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
544 |
} |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
545 |
|
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
546 |
if (pkg.length() > 0) |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
547 |
checkName(pkg); |
10 | 548 |
|
549 |
// TODO: Only support reading resources in selected output |
|
550 |
// locations? Only allow reading of non-source, non-class |
|
551 |
// 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
|
552 |
|
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
553 |
// 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
|
554 |
// 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
|
555 |
// 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
|
556 |
// 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
|
557 |
// 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
|
558 |
// invocation. |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
559 |
FileObject fileObject; |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
560 |
if (location.isOutputLocation()) { |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
561 |
fileObject = fileManager.getFileForOutput(location, |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
562 |
pkg, |
10633
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
563 |
relativeName.toString(), |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
564 |
null); |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
565 |
} else { |
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
566 |
fileObject = fileManager.getFileForInput(location, |
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
567 |
pkg, |
6573
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
568 |
relativeName.toString()); |
10633
5dd595ab058e
7068437: Regression: Filer.getResource(SOURCE_OUTPUT, ...) no longer works in JDK 7 w/o -s
jjg
parents:
7681
diff
changeset
|
569 |
} |
6573
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
570 |
if (fileObject == null) { |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
571 |
String name = (pkg.length() == 0) |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
572 |
? relativeName.toString() : (pkg + "/" + relativeName); |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
573 |
throw new FileNotFoundException(name); |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
574 |
} |
6bad8be140fc
6929404: Filer.getResource(SOURCE_PATH, ...) does not work when -sourcepath contains >1 entry
jjg
parents:
6159
diff
changeset
|
575 |
|
10 | 576 |
// If the path was already opened for writing, throw an exception. |
577 |
checkFileReopening(fileObject, false); |
|
578 |
return new FilerInputFileObject(fileObject); |
|
579 |
} |
|
580 |
||
581 |
private void checkName(String name) throws FilerException { |
|
582 |
checkName(name, false); |
|
583 |
} |
|
584 |
||
585 |
private void checkName(String name, boolean allowUnnamedPackageInfo) throws FilerException { |
|
586 |
if (!SourceVersion.isName(name) && !isPackageInfo(name, allowUnnamedPackageInfo)) { |
|
587 |
if (lint) |
|
588 |
log.warning("proc.illegal.file.name", name); |
|
589 |
throw new FilerException("Illegal name " + name); |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
private boolean isPackageInfo(String name, boolean allowUnnamedPackageInfo) { |
|
594 |
// Is the name of the form "package-info" or |
|
595 |
// "foo.bar.package-info"? |
|
596 |
final String PKG_INFO = "package-info"; |
|
597 |
int periodIndex = name.lastIndexOf("."); |
|
598 |
if (periodIndex == -1) { |
|
599 |
return allowUnnamedPackageInfo ? name.equals(PKG_INFO) : false; |
|
600 |
} else { |
|
601 |
// "foo.bar.package-info." illegal |
|
602 |
String prefix = name.substring(0, periodIndex); |
|
603 |
String simple = name.substring(periodIndex+1); |
|
604 |
return SourceVersion.isName(prefix) && simple.equals(PKG_INFO); |
|
605 |
} |
|
606 |
} |
|
607 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
608 |
private void checkNameAndExistence(ModuleSymbol mod, String typename, boolean allowUnnamedPackageInfo) throws FilerException { |
10 | 609 |
// TODO: Check if type already exists on source or class path? |
610 |
// If so, use warning message key proc.type.already.exists |
|
611 |
checkName(typename, allowUnnamedPackageInfo); |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
612 |
ClassSymbol existing; |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
613 |
boolean alreadySeen = aggregateGeneratedSourceNames.contains(Pair.of(mod, typename)) || |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
614 |
aggregateGeneratedClassNames.contains(Pair.of(mod, typename)) || |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
615 |
initialClassNames.contains(typename) || |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
616 |
((existing = elementUtils.getTypeElement(typename)) != null && |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
617 |
initialInputs.contains(existing.sourcefile)); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
618 |
if (alreadySeen) { |
10 | 619 |
if (lint) |
620 |
log.warning("proc.type.recreate", typename); |
|
621 |
throw new FilerException("Attempt to recreate a file for type " + typename); |
|
622 |
} |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
623 |
if (!mod.isUnnamed() && !typename.contains(".")) { |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
624 |
throw new FilerException("Attempt to create a type in unnamed package of a named module: " + typename); |
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
625 |
} |
10 | 626 |
} |
627 |
||
628 |
/** |
|
629 |
* Check to see if the file has already been opened; if so, throw |
|
630 |
* an exception, otherwise add it to the set of files. |
|
631 |
*/ |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
632 |
private void checkFileReopening(FileObject fileObject, boolean forWriting) throws FilerException { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
633 |
if (isInFileObjectHistory(fileObject, forWriting)) { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
634 |
if (lint) |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
635 |
log.warning("proc.file.reopening", fileObject.getName()); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
636 |
throw new FilerException("Attempt to reopen a file for path " + fileObject.getName()); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
637 |
} |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
638 |
if (forWriting) |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
639 |
fileObjectHistory.add(fileObject); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
640 |
} |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
641 |
|
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
642 |
private boolean isInFileObjectHistory(FileObject fileObject, boolean forWriting) { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
643 |
if (forWriting) { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
644 |
for(FileObject veteran : initialInputs) { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
645 |
try { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
646 |
if (fileManager.isSameFile(veteran, fileObject)) { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
647 |
return true; |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
648 |
} |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
649 |
} catch (IllegalArgumentException e) { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
650 |
//ignore... |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
651 |
} |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
652 |
} |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
653 |
for (String className : initialClassNames) { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
654 |
try { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
655 |
ClassSymbol existing = elementUtils.getTypeElement(className); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
656 |
if ( existing != null |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
657 |
&& ( (existing.sourcefile != null && fileManager.isSameFile(existing.sourcefile, fileObject)) |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
658 |
|| (existing.classfile != null && fileManager.isSameFile(existing.classfile, fileObject)))) { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
659 |
return true; |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
660 |
} |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
661 |
} catch (IllegalArgumentException e) { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
662 |
//ignore... |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
663 |
} |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
664 |
} |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
665 |
} |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
666 |
|
10 | 667 |
for(FileObject veteran : fileObjectHistory) { |
668 |
if (fileManager.isSameFile(veteran, fileObject)) { |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
669 |
return true; |
10 | 670 |
} |
671 |
} |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
672 |
|
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
673 |
return false; |
10 | 674 |
} |
675 |
||
676 |
public boolean newFiles() { |
|
677 |
return (!generatedSourceNames.isEmpty()) |
|
678 |
|| (!generatedClasses.isEmpty()); |
|
679 |
} |
|
680 |
||
681 |
public Set<String> getGeneratedSourceNames() { |
|
682 |
return generatedSourceNames; |
|
683 |
} |
|
684 |
||
685 |
public Set<JavaFileObject> getGeneratedSourceFileObjects() { |
|
686 |
return generatedSourceFileObjects; |
|
687 |
} |
|
688 |
||
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
689 |
public Map<ModuleSymbol, Map<String, JavaFileObject>> getGeneratedClasses() { |
10 | 690 |
return generatedClasses; |
691 |
} |
|
692 |
||
693 |
public void warnIfUnclosedFiles() { |
|
694 |
if (!openTypeNames.isEmpty()) |
|
695 |
log.warning("proc.unclosed.type.files", openTypeNames.toString()); |
|
696 |
} |
|
697 |
||
698 |
/** |
|
699 |
* Update internal state for a new round. |
|
700 |
*/ |
|
23810
b92eb80925f0
8038455: Use single Context for all rounds of annotation processing
jlahoda
parents:
22165
diff
changeset
|
701 |
public void newRound() { |
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6157
diff
changeset
|
702 |
clearRoundState(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6157
diff
changeset
|
703 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6157
diff
changeset
|
704 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6157
diff
changeset
|
705 |
void setLastRound(boolean lastRound) { |
10 | 706 |
this.lastRound = lastRound; |
707 |
} |
|
708 |
||
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
709 |
public void setInitialState(Collection<? extends JavaFileObject> initialInputs, |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
710 |
Collection<String> initialClassNames) { |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
711 |
this.initialInputs.addAll(initialInputs); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
712 |
this.initialClassNames.addAll(initialClassNames); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
713 |
} |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
714 |
|
10 | 715 |
public void close() { |
716 |
clearRoundState(); |
|
717 |
// Cross-round state |
|
43368
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
718 |
initialClassNames.clear(); |
cabe410a7a5c
8067747: javac throws exception during compilation when annotation processing is enabled
jlahoda
parents:
42815
diff
changeset
|
719 |
initialInputs.clear(); |
10 | 720 |
fileObjectHistory.clear(); |
721 |
openTypeNames.clear(); |
|
722 |
aggregateGeneratedSourceNames.clear(); |
|
723 |
aggregateGeneratedClassNames.clear(); |
|
724 |
} |
|
725 |
||
726 |
private void clearRoundState() { |
|
727 |
generatedSourceNames.clear(); |
|
728 |
generatedSourceFileObjects.clear(); |
|
729 |
generatedClasses.clear(); |
|
730 |
} |
|
731 |
||
732 |
/** |
|
733 |
* Debugging function to display internal state. |
|
734 |
*/ |
|
735 |
public void displayState() { |
|
40232
4995ab1a4558
8162359: javac should use stdout for --help and --version
jjg
parents:
26266
diff
changeset
|
736 |
PrintWriter xout = context.get(Log.logKey).getWriter(Log.WriterKind.STDERR); |
10 | 737 |
xout.println("File Object History : " + fileObjectHistory); |
738 |
xout.println("Open Type Names : " + openTypeNames); |
|
739 |
xout.println("Gen. Src Names : " + generatedSourceNames); |
|
740 |
xout.println("Gen. Cls Names : " + generatedClasses.keySet()); |
|
741 |
xout.println("Agg. Gen. Src Names : " + aggregateGeneratedSourceNames); |
|
742 |
xout.println("Agg. Gen. Cls Names : " + aggregateGeneratedClassNames); |
|
743 |
} |
|
744 |
||
745 |
public String toString() { |
|
746 |
return "javac Filer"; |
|
747 |
} |
|
748 |
||
749 |
/** |
|
750 |
* Upon close, register files opened by create{Source, Class}File |
|
751 |
* for annotation processing. |
|
752 |
*/ |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
753 |
private void closeFileObject(ModuleSymbol mod, String typeName, FileObject fileObject) { |
10 | 754 |
/* |
755 |
* If typeName is non-null, the file object was opened as a |
|
756 |
* source or class file by the user. If a file was opened as |
|
757 |
* a resource, typeName will be null and the file is *not* |
|
758 |
* subject to annotation processing. |
|
759 |
*/ |
|
760 |
if ((typeName != null)) { |
|
761 |
if (!(fileObject instanceof JavaFileObject)) |
|
762 |
throw new AssertionError("JavaFileOject not found for " + fileObject); |
|
763 |
JavaFileObject javaFileObject = (JavaFileObject)fileObject; |
|
764 |
switch(javaFileObject.getKind()) { |
|
765 |
case SOURCE: |
|
766 |
generatedSourceNames.add(typeName); |
|
767 |
generatedSourceFileObjects.add(javaFileObject); |
|
768 |
openTypeNames.remove(typeName); |
|
769 |
break; |
|
770 |
||
771 |
case CLASS: |
|
42815
050370edaade
8072988: Update javax.annotation.processing for modules
jlahoda
parents:
40232
diff
changeset
|
772 |
generatedClasses.computeIfAbsent(mod, m -> Collections.synchronizedMap(new LinkedHashMap<>())).put(typeName, javaFileObject); |
10 | 773 |
openTypeNames.remove(typeName); |
774 |
break; |
|
775 |
||
776 |
default: |
|
777 |
break; |
|
778 |
} |
|
779 |
} |
|
780 |
} |
|
781 |
||
782 |
} |