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