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