author | jjg |
Thu, 29 Jul 2010 19:30:35 -0700 | |
changeset 6159 | 88930a1c409e |
parent 6153 | 277c719ff46e |
child 6162 | 9d0f96b4befa |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 2005, 2009, 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 |
||
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
28 |
import java.lang.reflect.*; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
29 |
import java.util.*; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
30 |
import java.util.regex.*; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
31 |
|
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
32 |
import java.net.URL; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
33 |
import java.io.Closeable; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
34 |
import java.io.File; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
35 |
import java.io.PrintWriter; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
36 |
import java.io.IOException; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
37 |
import java.net.MalformedURLException; |
10 | 38 |
import java.io.StringWriter; |
39 |
||
40 |
import javax.annotation.processing.*; |
|
41 |
import javax.lang.model.SourceVersion; |
|
42 |
import javax.lang.model.element.AnnotationMirror; |
|
43 |
import javax.lang.model.element.Element; |
|
44 |
import javax.lang.model.element.TypeElement; |
|
45 |
import javax.lang.model.element.PackageElement; |
|
46 |
import javax.lang.model.util.*; |
|
47 |
import javax.tools.JavaFileManager; |
|
48 |
import javax.tools.StandardJavaFileManager; |
|
49 |
import javax.tools.JavaFileObject; |
|
50 |
import javax.tools.DiagnosticListener; |
|
51 |
||
3151
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
52 |
import com.sun.source.util.AbstractTypeProcessor; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
53 |
import com.sun.source.util.TaskEvent; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
54 |
import com.sun.source.util.TaskListener; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
55 |
import com.sun.tools.javac.api.JavacTaskImpl; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
56 |
import com.sun.tools.javac.code.*; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
57 |
import com.sun.tools.javac.code.Symbol.*; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
58 |
import com.sun.tools.javac.file.JavacFileManager; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
59 |
import com.sun.tools.javac.jvm.*; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
60 |
import com.sun.tools.javac.main.JavaCompiler; |
3151
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
61 |
import com.sun.tools.javac.main.JavaCompiler.CompileState; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
62 |
import com.sun.tools.javac.model.JavacElements; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
63 |
import com.sun.tools.javac.model.JavacTypes; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
64 |
import com.sun.tools.javac.parser.*; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
65 |
import com.sun.tools.javac.tree.*; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
66 |
import com.sun.tools.javac.tree.JCTree.*; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
67 |
import com.sun.tools.javac.util.Abort; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
68 |
import com.sun.tools.javac.util.Context; |
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
69 |
import com.sun.tools.javac.util.Convert; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
70 |
import com.sun.tools.javac.util.List; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
71 |
import com.sun.tools.javac.util.Log; |
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1260
diff
changeset
|
72 |
import com.sun.tools.javac.util.JavacMessages; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
73 |
import com.sun.tools.javac.util.Name; |
1260
a772ba9ba43d
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents:
735
diff
changeset
|
74 |
import com.sun.tools.javac.util.Names; |
731
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
75 |
import com.sun.tools.javac.util.Options; |
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
76 |
|
1dd22bdb9ca5
6714364: refactor javac File handling code into new javac.file package
jjg
parents:
169
diff
changeset
|
77 |
import static javax.tools.StandardLocation.*; |
10 | 78 |
|
79 |
/** |
|
80 |
* Objects of this class hold and manage the state needed to support |
|
81 |
* annotation processing. |
|
82 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5841
diff
changeset
|
83 |
* <p><b>This is NOT part of any supported API. |
10 | 84 |
* If you write code that depends on this, you do so at your own risk. |
85 |
* This code and its internal interfaces are subject to change or |
|
86 |
* deletion without notice.</b> |
|
87 |
*/ |
|
88 |
public class JavacProcessingEnvironment implements ProcessingEnvironment, Closeable { |
|
89 |
Options options; |
|
90 |
||
91 |
private final boolean printProcessorInfo; |
|
92 |
private final boolean printRounds; |
|
93 |
private final boolean verbose; |
|
94 |
private final boolean lint; |
|
95 |
private final boolean procOnly; |
|
96 |
private final boolean fatalErrors; |
|
6153
277c719ff46e
6403456: -Werror should work with annotation processing
jjg
parents:
6143
diff
changeset
|
97 |
private final boolean werror; |
3151
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
98 |
private boolean foundTypeProcessors; |
10 | 99 |
|
100 |
private final JavacFiler filer; |
|
101 |
private final JavacMessager messager; |
|
102 |
private final JavacElements elementUtils; |
|
103 |
private final JavacTypes typeUtils; |
|
104 |
||
105 |
/** |
|
106 |
* Holds relevant state history of which processors have been |
|
107 |
* used. |
|
108 |
*/ |
|
109 |
private DiscoveredProcessors discoveredProcs; |
|
110 |
||
111 |
/** |
|
112 |
* Map of processor-specific options. |
|
113 |
*/ |
|
114 |
private final Map<String, String> processorOptions; |
|
115 |
||
116 |
/** |
|
117 |
*/ |
|
118 |
private final Set<String> unmatchedProcessorOptions; |
|
119 |
||
120 |
/** |
|
121 |
* Annotations implicitly processed and claimed by javac. |
|
122 |
*/ |
|
123 |
private final Set<String> platformAnnotations; |
|
124 |
||
125 |
/** |
|
126 |
* Set of packages given on command line. |
|
127 |
*/ |
|
128 |
private Set<PackageSymbol> specifiedPackages = Collections.emptySet(); |
|
129 |
||
130 |
/** The log to be used for error reporting. |
|
131 |
*/ |
|
132 |
Log log; |
|
133 |
||
134 |
/** |
|
135 |
* Source level of the compile. |
|
136 |
*/ |
|
137 |
Source source; |
|
138 |
||
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3300
diff
changeset
|
139 |
private ClassLoader processorClassLoader; |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3300
diff
changeset
|
140 |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1260
diff
changeset
|
141 |
/** |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1260
diff
changeset
|
142 |
* JavacMessages object used for localization |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1260
diff
changeset
|
143 |
*/ |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1260
diff
changeset
|
144 |
private JavacMessages messages; |
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1260
diff
changeset
|
145 |
|
10 | 146 |
private Context context; |
147 |
||
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1260
diff
changeset
|
148 |
public JavacProcessingEnvironment(Context context, Iterable<? extends Processor> processors) { |
10 | 149 |
options = Options.instance(context); |
150 |
this.context = context; |
|
151 |
log = Log.instance(context); |
|
152 |
source = Source.instance(context); |
|
153 |
printProcessorInfo = options.get("-XprintProcessorInfo") != null; |
|
154 |
printRounds = options.get("-XprintRounds") != null; |
|
155 |
verbose = options.get("-verbose") != null; |
|
156 |
lint = options.lint("processing"); |
|
157 |
procOnly = options.get("-proc:only") != null || |
|
158 |
options.get("-Xprint") != null; |
|
159 |
fatalErrors = options.get("fatalEnterError") != null; |
|
6153
277c719ff46e
6403456: -Werror should work with annotation processing
jjg
parents:
6143
diff
changeset
|
160 |
werror = options.get("-Werror") != null; |
10 | 161 |
platformAnnotations = initPlatformAnnotations(); |
3151
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
162 |
foundTypeProcessors = false; |
10 | 163 |
|
164 |
// Initialize services before any processors are initialzied |
|
165 |
// in case processors use them. |
|
166 |
filer = new JavacFiler(context); |
|
167 |
messager = new JavacMessager(context, this); |
|
168 |
elementUtils = new JavacElements(context); |
|
169 |
typeUtils = new JavacTypes(context); |
|
170 |
processorOptions = initProcessorOptions(context); |
|
171 |
unmatchedProcessorOptions = initUnmatchedProcessorOptions(); |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1260
diff
changeset
|
172 |
messages = JavacMessages.instance(context); |
10 | 173 |
initProcessorIterator(context, processors); |
174 |
} |
|
175 |
||
176 |
private Set<String> initPlatformAnnotations() { |
|
177 |
Set<String> platformAnnotations = new HashSet<String>(); |
|
178 |
platformAnnotations.add("java.lang.Deprecated"); |
|
179 |
platformAnnotations.add("java.lang.Override"); |
|
180 |
platformAnnotations.add("java.lang.SuppressWarnings"); |
|
181 |
platformAnnotations.add("java.lang.annotation.Documented"); |
|
182 |
platformAnnotations.add("java.lang.annotation.Inherited"); |
|
183 |
platformAnnotations.add("java.lang.annotation.Retention"); |
|
184 |
platformAnnotations.add("java.lang.annotation.Target"); |
|
185 |
return Collections.unmodifiableSet(platformAnnotations); |
|
186 |
} |
|
187 |
||
188 |
private void initProcessorIterator(Context context, Iterable<? extends Processor> processors) { |
|
189 |
Log log = Log.instance(context); |
|
190 |
Iterator<? extends Processor> processorIterator; |
|
191 |
||
192 |
if (options.get("-Xprint") != null) { |
|
193 |
try { |
|
194 |
Processor processor = PrintingProcessor.class.newInstance(); |
|
195 |
processorIterator = List.of(processor).iterator(); |
|
196 |
} catch (Throwable t) { |
|
197 |
AssertionError assertError = |
|
198 |
new AssertionError("Problem instantiating PrintingProcessor."); |
|
199 |
assertError.initCause(t); |
|
200 |
throw assertError; |
|
201 |
} |
|
202 |
} else if (processors != null) { |
|
203 |
processorIterator = processors.iterator(); |
|
204 |
} else { |
|
205 |
String processorNames = options.get("-processor"); |
|
206 |
JavaFileManager fileManager = context.get(JavaFileManager.class); |
|
207 |
try { |
|
208 |
// If processorpath is not explicitly set, use the classpath. |
|
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3300
diff
changeset
|
209 |
processorClassLoader = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) |
10 | 210 |
? fileManager.getClassLoader(ANNOTATION_PROCESSOR_PATH) |
211 |
: fileManager.getClassLoader(CLASS_PATH); |
|
212 |
||
213 |
/* |
|
214 |
* If the "-processor" option is used, search the appropriate |
|
215 |
* path for the named class. Otherwise, use a service |
|
216 |
* provider mechanism to create the processor iterator. |
|
217 |
*/ |
|
218 |
if (processorNames != null) { |
|
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3300
diff
changeset
|
219 |
processorIterator = new NameProcessIterator(processorNames, processorClassLoader, log); |
10 | 220 |
} else { |
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3300
diff
changeset
|
221 |
processorIterator = new ServiceIterator(processorClassLoader, log); |
10 | 222 |
} |
223 |
} catch (SecurityException e) { |
|
224 |
/* |
|
225 |
* A security exception will occur if we can't create a classloader. |
|
226 |
* Ignore the exception if, with hindsight, we didn't need it anyway |
|
227 |
* (i.e. no processor was specified either explicitly, or implicitly, |
|
228 |
* in service configuration file.) Otherwise, we cannot continue. |
|
229 |
*/ |
|
230 |
processorIterator = handleServiceLoaderUnavailability("proc.cant.create.loader", e); |
|
231 |
} |
|
232 |
} |
|
233 |
discoveredProcs = new DiscoveredProcessors(processorIterator); |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* Returns an empty processor iterator if no processors are on the |
|
238 |
* relevant path, otherwise if processors are present, logs an |
|
239 |
* error. Called when a service loader is unavailable for some |
|
240 |
* reason, either because a service loader class cannot be found |
|
241 |
* or because a security policy prevents class loaders from being |
|
242 |
* created. |
|
243 |
* |
|
244 |
* @param key The resource key to use to log an error message |
|
245 |
* @param e If non-null, pass this exception to Abort |
|
246 |
*/ |
|
247 |
private Iterator<Processor> handleServiceLoaderUnavailability(String key, Exception e) { |
|
248 |
JavaFileManager fileManager = context.get(JavaFileManager.class); |
|
249 |
||
250 |
if (fileManager instanceof JavacFileManager) { |
|
251 |
StandardJavaFileManager standardFileManager = (JavacFileManager) fileManager; |
|
252 |
Iterable<? extends File> workingPath = fileManager.hasLocation(ANNOTATION_PROCESSOR_PATH) |
|
253 |
? standardFileManager.getLocation(ANNOTATION_PROCESSOR_PATH) |
|
254 |
: standardFileManager.getLocation(CLASS_PATH); |
|
255 |
||
256 |
if (needClassLoader(options.get("-processor"), workingPath) ) |
|
257 |
handleException(key, e); |
|
258 |
||
259 |
} else { |
|
260 |
handleException(key, e); |
|
261 |
} |
|
262 |
||
263 |
java.util.List<Processor> pl = Collections.emptyList(); |
|
264 |
return pl.iterator(); |
|
265 |
} |
|
266 |
||
267 |
/** |
|
268 |
* Handle a security exception thrown during initializing the |
|
269 |
* Processor iterator. |
|
270 |
*/ |
|
271 |
private void handleException(String key, Exception e) { |
|
272 |
if (e != null) { |
|
273 |
log.error(key, e.getLocalizedMessage()); |
|
274 |
throw new Abort(e); |
|
275 |
} else { |
|
276 |
log.error(key); |
|
277 |
throw new Abort(); |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
/** |
|
282 |
* Use a service loader appropriate for the platform to provide an |
|
283 |
* iterator over annotations processors. If |
|
284 |
* java.util.ServiceLoader is present use it, otherwise, use |
|
285 |
* sun.misc.Service, otherwise fail if a loader is needed. |
|
286 |
*/ |
|
287 |
private class ServiceIterator implements Iterator<Processor> { |
|
288 |
// The to-be-wrapped iterator. |
|
289 |
private Iterator<?> iterator; |
|
290 |
private Log log; |
|
3764
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
291 |
private Class<?> loaderClass; |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
292 |
private boolean jusl; |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
293 |
private Object loader; |
10 | 294 |
|
295 |
ServiceIterator(ClassLoader classLoader, Log log) { |
|
296 |
String loadMethodName; |
|
297 |
||
298 |
this.log = log; |
|
299 |
try { |
|
300 |
try { |
|
301 |
loaderClass = Class.forName("java.util.ServiceLoader"); |
|
302 |
loadMethodName = "load"; |
|
303 |
jusl = true; |
|
304 |
} catch (ClassNotFoundException cnfe) { |
|
305 |
try { |
|
306 |
loaderClass = Class.forName("sun.misc.Service"); |
|
307 |
loadMethodName = "providers"; |
|
308 |
jusl = false; |
|
309 |
} catch (ClassNotFoundException cnfe2) { |
|
310 |
// Fail softly if a loader is not actually needed. |
|
311 |
this.iterator = handleServiceLoaderUnavailability("proc.no.service", |
|
312 |
null); |
|
313 |
return; |
|
314 |
} |
|
315 |
} |
|
316 |
||
317 |
// java.util.ServiceLoader.load or sun.misc.Service.providers |
|
318 |
Method loadMethod = loaderClass.getMethod(loadMethodName, |
|
319 |
Class.class, |
|
320 |
ClassLoader.class); |
|
321 |
||
322 |
Object result = loadMethod.invoke(null, |
|
323 |
Processor.class, |
|
324 |
classLoader); |
|
325 |
||
326 |
// For java.util.ServiceLoader, we have to call another |
|
327 |
// method to get the iterator. |
|
328 |
if (jusl) { |
|
3764
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
329 |
loader = result; // Store ServiceLoader to call reload later |
10 | 330 |
Method m = loaderClass.getMethod("iterator"); |
331 |
result = m.invoke(result); // serviceLoader.iterator(); |
|
332 |
} |
|
333 |
||
334 |
// The result should now be an iterator. |
|
335 |
this.iterator = (Iterator<?>) result; |
|
336 |
} catch (Throwable t) { |
|
337 |
log.error("proc.service.problem"); |
|
338 |
throw new Abort(t); |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
public boolean hasNext() { |
|
343 |
try { |
|
344 |
return iterator.hasNext(); |
|
345 |
} catch (Throwable t) { |
|
346 |
if ("ServiceConfigurationError". |
|
347 |
equals(t.getClass().getSimpleName())) { |
|
348 |
log.error("proc.bad.config.file", t.getLocalizedMessage()); |
|
349 |
} |
|
350 |
throw new Abort(t); |
|
351 |
} |
|
352 |
} |
|
353 |
||
354 |
public Processor next() { |
|
355 |
try { |
|
356 |
return (Processor)(iterator.next()); |
|
357 |
} catch (Throwable t) { |
|
358 |
if ("ServiceConfigurationError". |
|
359 |
equals(t.getClass().getSimpleName())) { |
|
360 |
log.error("proc.bad.config.file", t.getLocalizedMessage()); |
|
361 |
} else { |
|
362 |
log.error("proc.processor.constructor.error", t.getLocalizedMessage()); |
|
363 |
} |
|
364 |
throw new Abort(t); |
|
365 |
} |
|
366 |
} |
|
367 |
||
368 |
public void remove() { |
|
369 |
throw new UnsupportedOperationException(); |
|
370 |
} |
|
3764
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
371 |
|
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
372 |
public void close() { |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
373 |
if (jusl) { |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
374 |
try { |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
375 |
// Call java.util.ServiceLoader.reload |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
376 |
Method reloadMethod = loaderClass.getMethod("reload"); |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
377 |
reloadMethod.invoke(loader); |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
378 |
} catch(Exception e) { |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
379 |
; // Ignore problems during a call to reload. |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
380 |
} |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
381 |
} |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
382 |
} |
10 | 383 |
} |
384 |
||
385 |
||
386 |
private static class NameProcessIterator implements Iterator<Processor> { |
|
387 |
Processor nextProc = null; |
|
388 |
Iterator<String> names; |
|
389 |
ClassLoader processorCL; |
|
390 |
Log log; |
|
391 |
||
392 |
NameProcessIterator(String names, ClassLoader processorCL, Log log) { |
|
393 |
this.names = Arrays.asList(names.split(",")).iterator(); |
|
394 |
this.processorCL = processorCL; |
|
395 |
this.log = log; |
|
396 |
} |
|
397 |
||
398 |
public boolean hasNext() { |
|
399 |
if (nextProc != null) |
|
400 |
return true; |
|
401 |
else { |
|
402 |
if (!names.hasNext()) |
|
403 |
return false; |
|
404 |
else { |
|
405 |
String processorName = names.next(); |
|
406 |
||
407 |
Processor processor; |
|
408 |
try { |
|
409 |
try { |
|
410 |
processor = |
|
411 |
(Processor) (processorCL.loadClass(processorName).newInstance()); |
|
412 |
} catch (ClassNotFoundException cnfe) { |
|
413 |
log.error("proc.processor.not.found", processorName); |
|
414 |
return false; |
|
415 |
} catch (ClassCastException cce) { |
|
416 |
log.error("proc.processor.wrong.type", processorName); |
|
417 |
return false; |
|
418 |
} catch (Exception e ) { |
|
419 |
log.error("proc.processor.cant.instantiate", processorName); |
|
420 |
return false; |
|
421 |
} |
|
422 |
} catch(Throwable t) { |
|
423 |
throw new AnnotationProcessingError(t); |
|
424 |
} |
|
425 |
nextProc = processor; |
|
426 |
return true; |
|
427 |
} |
|
428 |
||
429 |
} |
|
430 |
} |
|
431 |
||
432 |
public Processor next() { |
|
433 |
if (hasNext()) { |
|
434 |
Processor p = nextProc; |
|
435 |
nextProc = null; |
|
436 |
return p; |
|
437 |
} else |
|
438 |
throw new NoSuchElementException(); |
|
439 |
} |
|
440 |
||
441 |
public void remove () { |
|
442 |
throw new UnsupportedOperationException(); |
|
443 |
} |
|
444 |
} |
|
445 |
||
446 |
public boolean atLeastOneProcessor() { |
|
447 |
return discoveredProcs.iterator().hasNext(); |
|
448 |
} |
|
449 |
||
450 |
private Map<String, String> initProcessorOptions(Context context) { |
|
451 |
Options options = Options.instance(context); |
|
452 |
Set<String> keySet = options.keySet(); |
|
453 |
Map<String, String> tempOptions = new LinkedHashMap<String, String>(); |
|
454 |
||
455 |
for(String key : keySet) { |
|
456 |
if (key.startsWith("-A") && key.length() > 2) { |
|
457 |
int sepIndex = key.indexOf('='); |
|
458 |
String candidateKey = null; |
|
459 |
String candidateValue = null; |
|
460 |
||
461 |
if (sepIndex == -1) |
|
462 |
candidateKey = key.substring(2); |
|
463 |
else if (sepIndex >= 3) { |
|
464 |
candidateKey = key.substring(2, sepIndex); |
|
465 |
candidateValue = (sepIndex < key.length()-1)? |
|
466 |
key.substring(sepIndex+1) : null; |
|
467 |
} |
|
468 |
tempOptions.put(candidateKey, candidateValue); |
|
469 |
} |
|
470 |
} |
|
471 |
||
472 |
return Collections.unmodifiableMap(tempOptions); |
|
473 |
} |
|
474 |
||
475 |
private Set<String> initUnmatchedProcessorOptions() { |
|
476 |
Set<String> unmatchedProcessorOptions = new HashSet<String>(); |
|
477 |
unmatchedProcessorOptions.addAll(processorOptions.keySet()); |
|
478 |
return unmatchedProcessorOptions; |
|
479 |
} |
|
480 |
||
481 |
/** |
|
482 |
* State about how a processor has been used by the tool. If a |
|
483 |
* processor has been used on a prior round, its process method is |
|
484 |
* called on all subsequent rounds, perhaps with an empty set of |
|
485 |
* annotations to process. The {@code annotatedSupported} method |
|
486 |
* caches the supported annotation information from the first (and |
|
487 |
* only) getSupportedAnnotationTypes call to the processor. |
|
488 |
*/ |
|
489 |
static class ProcessorState { |
|
490 |
public Processor processor; |
|
491 |
public boolean contributed; |
|
492 |
private ArrayList<Pattern> supportedAnnotationPatterns; |
|
493 |
private ArrayList<String> supportedOptionNames; |
|
494 |
||
495 |
ProcessorState(Processor p, Log log, Source source, ProcessingEnvironment env) { |
|
496 |
processor = p; |
|
497 |
contributed = false; |
|
498 |
||
499 |
try { |
|
500 |
processor.init(env); |
|
501 |
||
502 |
checkSourceVersionCompatibility(source, log); |
|
503 |
||
504 |
supportedAnnotationPatterns = new ArrayList<Pattern>(); |
|
505 |
for (String importString : processor.getSupportedAnnotationTypes()) { |
|
506 |
supportedAnnotationPatterns.add(importStringToPattern(importString, |
|
507 |
processor, |
|
508 |
log)); |
|
509 |
} |
|
510 |
||
511 |
supportedOptionNames = new ArrayList<String>(); |
|
512 |
for (String optionName : processor.getSupportedOptions() ) { |
|
513 |
if (checkOptionName(optionName, log)) |
|
514 |
supportedOptionNames.add(optionName); |
|
515 |
} |
|
516 |
||
517 |
} catch (Throwable t) { |
|
518 |
throw new AnnotationProcessingError(t); |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Checks whether or not a processor's source version is |
|
524 |
* compatible with the compilation source version. The |
|
525 |
* processor's source version needs to be greater than or |
|
526 |
* equal to the source version of the compile. |
|
527 |
*/ |
|
528 |
private void checkSourceVersionCompatibility(Source source, Log log) { |
|
529 |
SourceVersion procSourceVersion = processor.getSupportedSourceVersion(); |
|
530 |
||
531 |
if (procSourceVersion.compareTo(Source.toSourceVersion(source)) < 0 ) { |
|
532 |
log.warning("proc.processor.incompatible.source.version", |
|
533 |
procSourceVersion, |
|
534 |
processor.getClass().getName(), |
|
535 |
source.name); |
|
536 |
} |
|
537 |
} |
|
538 |
||
539 |
private boolean checkOptionName(String optionName, Log log) { |
|
540 |
boolean valid = isValidOptionName(optionName); |
|
541 |
if (!valid) |
|
542 |
log.error("proc.processor.bad.option.name", |
|
543 |
optionName, |
|
544 |
processor.getClass().getName()); |
|
545 |
return valid; |
|
546 |
} |
|
547 |
||
548 |
public boolean annotationSupported(String annotationName) { |
|
549 |
for(Pattern p: supportedAnnotationPatterns) { |
|
550 |
if (p.matcher(annotationName).matches()) |
|
551 |
return true; |
|
552 |
} |
|
553 |
return false; |
|
554 |
} |
|
555 |
||
556 |
/** |
|
557 |
* Remove options that are matched by this processor. |
|
558 |
*/ |
|
559 |
public void removeSupportedOptions(Set<String> unmatchedProcessorOptions) { |
|
560 |
unmatchedProcessorOptions.removeAll(supportedOptionNames); |
|
561 |
} |
|
562 |
} |
|
563 |
||
564 |
// TODO: These two classes can probably be rewritten better... |
|
565 |
/** |
|
566 |
* This class holds information about the processors that have |
|
567 |
* been discoverd so far as well as the means to discover more, if |
|
568 |
* necessary. A single iterator should be used per round of |
|
569 |
* annotation processing. The iterator first visits already |
|
3764
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
570 |
* discovered processors then fails over to the service provider |
10 | 571 |
* mechanism if additional queries are made. |
572 |
*/ |
|
573 |
class DiscoveredProcessors implements Iterable<ProcessorState> { |
|
574 |
||
575 |
class ProcessorStateIterator implements Iterator<ProcessorState> { |
|
576 |
DiscoveredProcessors psi; |
|
577 |
Iterator<ProcessorState> innerIter; |
|
578 |
boolean onProcInterator; |
|
579 |
||
580 |
ProcessorStateIterator(DiscoveredProcessors psi) { |
|
581 |
this.psi = psi; |
|
582 |
this.innerIter = psi.procStateList.iterator(); |
|
583 |
this.onProcInterator = false; |
|
584 |
} |
|
585 |
||
586 |
public ProcessorState next() { |
|
587 |
if (!onProcInterator) { |
|
588 |
if (innerIter.hasNext()) |
|
589 |
return innerIter.next(); |
|
590 |
else |
|
591 |
onProcInterator = true; |
|
592 |
} |
|
593 |
||
594 |
if (psi.processorIterator.hasNext()) { |
|
595 |
ProcessorState ps = new ProcessorState(psi.processorIterator.next(), |
|
596 |
log, source, JavacProcessingEnvironment.this); |
|
597 |
psi.procStateList.add(ps); |
|
598 |
return ps; |
|
599 |
} else |
|
600 |
throw new NoSuchElementException(); |
|
601 |
} |
|
602 |
||
603 |
public boolean hasNext() { |
|
604 |
if (onProcInterator) |
|
605 |
return psi.processorIterator.hasNext(); |
|
606 |
else |
|
607 |
return innerIter.hasNext() || psi.processorIterator.hasNext(); |
|
608 |
} |
|
609 |
||
610 |
public void remove () { |
|
611 |
throw new UnsupportedOperationException(); |
|
612 |
} |
|
613 |
||
614 |
/** |
|
615 |
* Run all remaining processors on the procStateList that |
|
616 |
* have not already run this round with an empty set of |
|
617 |
* annotations. |
|
618 |
*/ |
|
619 |
public void runContributingProcs(RoundEnvironment re) { |
|
620 |
if (!onProcInterator) { |
|
621 |
Set<TypeElement> emptyTypeElements = Collections.emptySet(); |
|
622 |
while(innerIter.hasNext()) { |
|
623 |
ProcessorState ps = innerIter.next(); |
|
624 |
if (ps.contributed) |
|
625 |
callProcessor(ps.processor, emptyTypeElements, re); |
|
626 |
} |
|
627 |
} |
|
628 |
} |
|
629 |
} |
|
630 |
||
631 |
Iterator<? extends Processor> processorIterator; |
|
632 |
ArrayList<ProcessorState> procStateList; |
|
633 |
||
634 |
public ProcessorStateIterator iterator() { |
|
635 |
return new ProcessorStateIterator(this); |
|
636 |
} |
|
637 |
||
638 |
DiscoveredProcessors(Iterator<? extends Processor> processorIterator) { |
|
639 |
this.processorIterator = processorIterator; |
|
640 |
this.procStateList = new ArrayList<ProcessorState>(); |
|
641 |
} |
|
3764
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
642 |
|
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
643 |
/** |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
644 |
* Free jar files, etc. if using a service loader. |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
645 |
*/ |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
646 |
public void close() { |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
647 |
if (processorIterator != null && |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
648 |
processorIterator instanceof ServiceIterator) { |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
649 |
((ServiceIterator) processorIterator).close(); |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
650 |
} |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
651 |
} |
10 | 652 |
} |
653 |
||
654 |
private void discoverAndRunProcs(Context context, |
|
655 |
Set<TypeElement> annotationsPresent, |
|
656 |
List<ClassSymbol> topLevelClasses, |
|
657 |
List<PackageSymbol> packageInfoFiles) { |
|
658 |
Map<String, TypeElement> unmatchedAnnotations = |
|
659 |
new HashMap<String, TypeElement>(annotationsPresent.size()); |
|
660 |
||
661 |
for(TypeElement a : annotationsPresent) { |
|
662 |
unmatchedAnnotations.put(a.getQualifiedName().toString(), |
|
663 |
a); |
|
664 |
} |
|
665 |
||
666 |
// Give "*" processors a chance to match |
|
667 |
if (unmatchedAnnotations.size() == 0) |
|
668 |
unmatchedAnnotations.put("", null); |
|
669 |
||
670 |
DiscoveredProcessors.ProcessorStateIterator psi = discoveredProcs.iterator(); |
|
671 |
// TODO: Create proper argument values; need past round |
|
672 |
// information to fill in this constructor. Note that the 1 |
|
673 |
// st round of processing could be the last round if there |
|
674 |
// were parse errors on the initial source files; however, we |
|
675 |
// are not doing processing in that case. |
|
676 |
||
677 |
Set<Element> rootElements = new LinkedHashSet<Element>(); |
|
678 |
rootElements.addAll(topLevelClasses); |
|
679 |
rootElements.addAll(packageInfoFiles); |
|
680 |
rootElements = Collections.unmodifiableSet(rootElements); |
|
681 |
||
682 |
RoundEnvironment renv = new JavacRoundEnvironment(false, |
|
683 |
false, |
|
684 |
rootElements, |
|
685 |
JavacProcessingEnvironment.this); |
|
686 |
||
687 |
while(unmatchedAnnotations.size() > 0 && psi.hasNext() ) { |
|
688 |
ProcessorState ps = psi.next(); |
|
689 |
Set<String> matchedNames = new HashSet<String>(); |
|
690 |
Set<TypeElement> typeElements = new LinkedHashSet<TypeElement>(); |
|
5003
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
5001
diff
changeset
|
691 |
|
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
5001
diff
changeset
|
692 |
for (Map.Entry<String, TypeElement> entry: unmatchedAnnotations.entrySet()) { |
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
5001
diff
changeset
|
693 |
String unmatchedAnnotationName = entry.getKey(); |
10 | 694 |
if (ps.annotationSupported(unmatchedAnnotationName) ) { |
695 |
matchedNames.add(unmatchedAnnotationName); |
|
5003
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
5001
diff
changeset
|
696 |
TypeElement te = entry.getValue(); |
10 | 697 |
if (te != null) |
698 |
typeElements.add(te); |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
if (matchedNames.size() > 0 || ps.contributed) { |
|
3151
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
703 |
foundTypeProcessors = foundTypeProcessors || (ps.processor instanceof AbstractTypeProcessor); |
10 | 704 |
boolean processingResult = callProcessor(ps.processor, typeElements, renv); |
705 |
ps.contributed = true; |
|
706 |
ps.removeSupportedOptions(unmatchedProcessorOptions); |
|
707 |
||
708 |
if (printProcessorInfo || verbose) { |
|
6143
79b7dee406cc
6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents:
5847
diff
changeset
|
709 |
log.printNoteLines("x.print.processor.info", |
79b7dee406cc
6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents:
5847
diff
changeset
|
710 |
ps.processor.getClass().getName(), |
79b7dee406cc
6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents:
5847
diff
changeset
|
711 |
matchedNames.toString(), |
79b7dee406cc
6966732: replace use of static Log.getLocalizedString with non-static alternative where possible
jjg
parents:
5847
diff
changeset
|
712 |
processingResult); |
10 | 713 |
} |
714 |
||
715 |
if (processingResult) { |
|
716 |
unmatchedAnnotations.keySet().removeAll(matchedNames); |
|
717 |
} |
|
718 |
||
719 |
} |
|
720 |
} |
|
721 |
unmatchedAnnotations.remove(""); |
|
722 |
||
723 |
if (lint && unmatchedAnnotations.size() > 0) { |
|
724 |
// Remove annotations processed by javac |
|
725 |
unmatchedAnnotations.keySet().removeAll(platformAnnotations); |
|
726 |
if (unmatchedAnnotations.size() > 0) { |
|
727 |
log = Log.instance(context); |
|
728 |
log.warning("proc.annotations.without.processors", |
|
729 |
unmatchedAnnotations.keySet()); |
|
730 |
} |
|
731 |
} |
|
732 |
||
733 |
// Run contributing processors that haven't run yet |
|
734 |
psi.runContributingProcs(renv); |
|
735 |
||
736 |
// Debugging |
|
737 |
if (options.get("displayFilerState") != null) |
|
738 |
filer.displayState(); |
|
739 |
} |
|
740 |
||
741 |
/** |
|
742 |
* Computes the set of annotations on the symbol in question. |
|
743 |
* Leave class public for external testing purposes. |
|
744 |
*/ |
|
745 |
public static class ComputeAnnotationSet extends |
|
5841
7a8448425bb7
6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
darcy
parents:
5520
diff
changeset
|
746 |
ElementScanner7<Set<TypeElement>, Set<TypeElement>> { |
10 | 747 |
final Elements elements; |
748 |
||
749 |
public ComputeAnnotationSet(Elements elements) { |
|
750 |
super(); |
|
751 |
this.elements = elements; |
|
752 |
} |
|
753 |
||
754 |
@Override |
|
755 |
public Set<TypeElement> visitPackage(PackageElement e, Set<TypeElement> p) { |
|
756 |
// Don't scan enclosed elements of a package |
|
757 |
return p; |
|
758 |
} |
|
759 |
||
760 |
@Override |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
761 |
public Set<TypeElement> scan(Element e, Set<TypeElement> p) { |
10 | 762 |
for (AnnotationMirror annotationMirror : |
763 |
elements.getAllAnnotationMirrors(e) ) { |
|
764 |
Element e2 = annotationMirror.getAnnotationType().asElement(); |
|
765 |
p.add((TypeElement) e2); |
|
766 |
} |
|
767 |
return super.scan(e, p); |
|
768 |
} |
|
769 |
} |
|
770 |
||
771 |
private boolean callProcessor(Processor proc, |
|
772 |
Set<? extends TypeElement> tes, |
|
773 |
RoundEnvironment renv) { |
|
774 |
try { |
|
775 |
return proc.process(tes, renv); |
|
776 |
} catch (CompletionFailure ex) { |
|
777 |
StringWriter out = new StringWriter(); |
|
778 |
ex.printStackTrace(new PrintWriter(out)); |
|
169 | 779 |
log.error("proc.cant.access", ex.sym, ex.getDetailValue(), out.toString()); |
10 | 780 |
return false; |
781 |
} catch (Throwable t) { |
|
782 |
throw new AnnotationProcessingError(t); |
|
783 |
} |
|
784 |
} |
|
785 |
||
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
786 |
/** |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
787 |
* Helper object for a single round of annotation processing. |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
788 |
*/ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
789 |
class Round { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
790 |
/** The round number. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
791 |
final int number; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
792 |
/** The context for the round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
793 |
final Context context; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
794 |
/** The compiler for the round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
795 |
final JavaCompiler compiler; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
796 |
/** The log for the round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
797 |
final Log log; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
798 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
799 |
/** The set of annotations to be processed this round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
800 |
Set<TypeElement> annotationsPresent; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
801 |
/** The set of top level classes to be processed this round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
802 |
List<ClassSymbol> topLevelClasses; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
803 |
/** The set of package-info files to be processed this round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
804 |
List<PackageSymbol> packageInfoFiles; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
805 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
806 |
/** Create a round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
807 |
Round(Context context, int number) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
808 |
this.context = context; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
809 |
this.number = number; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
810 |
compiler = JavaCompiler.instance(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
811 |
log = Log.instance(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
812 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
813 |
// the following is for the benefit of JavacProcessingEnvironment.getContext() |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
814 |
JavacProcessingEnvironment.this.context = context; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
815 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
816 |
// the following will be populated as needed |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
817 |
annotationsPresent = new LinkedHashSet<TypeElement>(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
818 |
topLevelClasses = List.nil(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
819 |
packageInfoFiles = List.nil(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
820 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
821 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
822 |
/** Create the next round to be used. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
823 |
Round next() { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
824 |
compiler.close(false); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
825 |
return new Round(contextForNextRound(), number + 1); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
826 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
827 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
828 |
/** Return the number of errors found so far in this round. |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
829 |
* This may include uncoverable errors, such as parse errors, |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
830 |
* and transient errors, such as missing symbols. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
831 |
int errorCount() { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
832 |
return compiler.errorCount(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
833 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
834 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
835 |
/** Return the number of warnings found so far in this round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
836 |
int warningCount() { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
837 |
return compiler.warningCount(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
838 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
839 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
840 |
/** Return whether or not an unrecoverable error has occurred. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
841 |
boolean unrecoverableError() { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
842 |
return log.unrecoverableError; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
843 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
844 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
845 |
/** Find the set of annotations present in the set of top level |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
846 |
* classes and package info files to be processed this round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
847 |
void findAnnotationsPresent(ComputeAnnotationSet annotationComputer) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
848 |
// Use annotation processing to compute the set of annotations present |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
849 |
annotationsPresent = new LinkedHashSet<TypeElement>(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
850 |
for (ClassSymbol classSym : topLevelClasses) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
851 |
annotationComputer.scan(classSym, annotationsPresent); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
852 |
for (PackageSymbol pkgSym : packageInfoFiles) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
853 |
annotationComputer.scan(pkgSym, annotationsPresent); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
854 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
855 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
856 |
/** |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
857 |
* Parse the latest set of generated source files created by the filer. |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
858 |
*/ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
859 |
List<JCCompilationUnit> parseNewSourceFiles() |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
860 |
throws IOException { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
861 |
List<JavaFileObject> fileObjects = List.nil(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
862 |
for (JavaFileObject jfo : filer.getGeneratedSourceFileObjects() ) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
863 |
fileObjects = fileObjects.prepend(jfo); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
864 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
865 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
866 |
return compiler.parseFiles(fileObjects); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
867 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
868 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
869 |
/** Enter the latest set of generated class files created by the filer. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
870 |
List<ClassSymbol> enterNewClassFiles() { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
871 |
ClassReader reader = ClassReader.instance(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
872 |
Names names = Names.instance(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
873 |
List<ClassSymbol> list = List.nil(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
874 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
875 |
for (Map.Entry<String,JavaFileObject> entry : filer.getGeneratedClasses().entrySet()) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
876 |
Name name = names.fromString(entry.getKey()); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
877 |
JavaFileObject file = entry.getValue(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
878 |
if (file.getKind() != JavaFileObject.Kind.CLASS) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
879 |
throw new AssertionError(file); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
880 |
ClassSymbol cs; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
881 |
if (isPkgInfo(file, JavaFileObject.Kind.CLASS)) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
882 |
Name packageName = Convert.packagePart(name); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
883 |
PackageSymbol p = reader.enterPackage(packageName); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
884 |
if (p.package_info == null) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
885 |
p.package_info = reader.enterClass(Convert.shortName(name), p); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
886 |
cs = p.package_info; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
887 |
if (cs.classfile == null) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
888 |
cs.classfile = file; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
889 |
} else |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
890 |
cs = reader.enterClass(name, file); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
891 |
list = list.prepend(cs); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
892 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
893 |
return list.reverse(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
894 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
895 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
896 |
/** Enter a set of syntax trees. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
897 |
void enterTrees(List<JCCompilationUnit> roots) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
898 |
compiler.enterTrees(roots); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
899 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
900 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
901 |
/** Run a processing round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
902 |
void run(boolean lastRound, boolean errorStatus) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
903 |
assert lastRound |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
904 |
? (topLevelClasses.size() == 0 && annotationsPresent.size() == 0) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
905 |
: (errorStatus == false); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
906 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
907 |
printRoundInfo(topLevelClasses, annotationsPresent, lastRound); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
908 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
909 |
TaskListener taskListener = context.get(TaskListener.class); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
910 |
if (taskListener != null) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
911 |
taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND)); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
912 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
913 |
try { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
914 |
if (lastRound) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
915 |
filer.setLastRound(true); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
916 |
Set<Element> emptyRootElements = Collections.emptySet(); // immutable |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
917 |
RoundEnvironment renv = new JavacRoundEnvironment(true, |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
918 |
errorStatus, |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
919 |
emptyRootElements, |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
920 |
JavacProcessingEnvironment.this); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
921 |
discoveredProcs.iterator().runContributingProcs(renv); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
922 |
} else { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
923 |
discoverAndRunProcs(context, annotationsPresent, topLevelClasses, packageInfoFiles); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
924 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
925 |
} finally { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
926 |
if (taskListener != null) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
927 |
taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND)); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
928 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
929 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
930 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
931 |
/** Update the processing state for the current context. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
932 |
// Question: should this not be part of next()? |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
933 |
// Note: Calling it from next() breaks some tests. There is an issue |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
934 |
// whether the annotationComputer is using elementUtils with the |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
935 |
// correct context. |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
936 |
void updateProcessingState() { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
937 |
filer.newRound(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
938 |
messager.newRound(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
939 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
940 |
elementUtils.setContext(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
941 |
typeUtils.setContext(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
942 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
943 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
944 |
/** Print info about this round. */ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
945 |
private void printRoundInfo(List<ClassSymbol> topLevelClasses, |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
946 |
Set<TypeElement> annotationsPresent, |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
947 |
boolean lastRound) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
948 |
if (printRounds || verbose) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
949 |
log.printNoteLines("x.print.rounds", |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
950 |
number, |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
951 |
"{" + topLevelClasses.toString(", ") + "}", |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
952 |
annotationsPresent, |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
953 |
lastRound); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
954 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
955 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
956 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
957 |
/** Get the context for the next round of processing. |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
958 |
* Important values are propogated from round to round; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
959 |
* other values are implicitly reset. |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
960 |
*/ |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
961 |
private Context contextForNextRound() { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
962 |
Context next = new Context(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
963 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
964 |
Options options = Options.instance(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
965 |
assert options != null; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
966 |
next.put(Options.optionsKey, options); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
967 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
968 |
PrintWriter out = context.get(Log.outKey); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
969 |
assert out != null; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
970 |
next.put(Log.outKey, out); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
971 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
972 |
final boolean shareNames = true; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
973 |
if (shareNames) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
974 |
Names names = Names.instance(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
975 |
assert names != null; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
976 |
next.put(Names.namesKey, names); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
977 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
978 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
979 |
DiagnosticListener<?> dl = context.get(DiagnosticListener.class); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
980 |
if (dl != null) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
981 |
next.put(DiagnosticListener.class, dl); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
982 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
983 |
TaskListener tl = context.get(TaskListener.class); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
984 |
if (tl != null) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
985 |
next.put(TaskListener.class, tl); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
986 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
987 |
JavaFileManager jfm = context.get(JavaFileManager.class); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
988 |
assert jfm != null; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
989 |
next.put(JavaFileManager.class, jfm); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
990 |
if (jfm instanceof JavacFileManager) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
991 |
((JavacFileManager)jfm).setContext(next); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
992 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
993 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
994 |
Names names = Names.instance(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
995 |
assert names != null; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
996 |
next.put(Names.namesKey, names); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
997 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
998 |
Keywords keywords = Keywords.instance(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
999 |
assert(keywords != null); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1000 |
next.put(Keywords.keywordsKey, keywords); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1001 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1002 |
JavaCompiler oldCompiler = JavaCompiler.instance(context); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1003 |
JavaCompiler nextCompiler = JavaCompiler.instance(next); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1004 |
nextCompiler.initRound(oldCompiler); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1005 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1006 |
JavacTaskImpl task = context.get(JavacTaskImpl.class); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1007 |
if (task != null) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1008 |
next.put(JavacTaskImpl.class, task); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1009 |
task.updateContext(next); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1010 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1011 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1012 |
context.clear(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1013 |
return next; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1014 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1015 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1016 |
|
10 | 1017 |
|
1018 |
// TODO: internal catch clauses?; catch and rethrow an annotation |
|
1019 |
// processing error |
|
1020 |
public JavaCompiler doProcessing(Context context, |
|
1021 |
List<JCCompilationUnit> roots, |
|
1022 |
List<ClassSymbol> classSymbols, |
|
1023 |
Iterable<? extends PackageSymbol> pckSymbols) |
|
5003
fd0b30cdbe5c
6929645: Address various findbugs warnings in langtools
darcy
parents:
5001
diff
changeset
|
1024 |
throws IOException { |
10 | 1025 |
|
1026 |
log = Log.instance(context); |
|
1027 |
||
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1028 |
Round round = new Round(context, 1); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1029 |
round.compiler.todo.clear(); // free the compiler's resources |
10 | 1030 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1031 |
// The reverse() in the following line is to maintain behavioural |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1032 |
// compatibility with the previous revision of the code. Strictly speaking, |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1033 |
// it should not be necessary, but a javah golden file test fails without it. |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1034 |
round.topLevelClasses = |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1035 |
getTopLevelClasses(roots).prependList(classSymbols.reverse()); |
10 | 1036 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1037 |
round.packageInfoFiles = getPackageInfoFiles(roots); |
10 | 1038 |
|
1039 |
Set<PackageSymbol> specifiedPackages = new LinkedHashSet<PackageSymbol>(); |
|
1040 |
for (PackageSymbol psym : pckSymbols) |
|
1041 |
specifiedPackages.add(psym); |
|
1042 |
this.specifiedPackages = Collections.unmodifiableSet(specifiedPackages); |
|
1043 |
||
1044 |
ComputeAnnotationSet annotationComputer = new ComputeAnnotationSet(elementUtils); |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1045 |
round.findAnnotationsPresent(annotationComputer); |
10 | 1046 |
|
1047 |
boolean errorStatus = false; |
|
1048 |
||
1049 |
runAround: |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1050 |
while (true) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1051 |
if ((fatalErrors && round.errorCount() != 0) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1052 |
|| (werror && round.warningCount() != 0)) { |
10 | 1053 |
errorStatus = true; |
1054 |
break runAround; |
|
1055 |
} |
|
1056 |
||
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1057 |
round.run(false, false); |
10 | 1058 |
|
1059 |
/* |
|
1060 |
* Processors for round n have run to completion. Prepare |
|
1061 |
* for round (n+1) by checked for errors raised by |
|
1062 |
* annotation processors and then checking for syntax |
|
1063 |
* errors on any generated source files. |
|
1064 |
*/ |
|
1065 |
if (messager.errorRaised()) { |
|
1066 |
errorStatus = true; |
|
1067 |
break runAround; |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1068 |
} |
10 | 1069 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1070 |
if (!moreToDo()) |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1071 |
break runAround; // No new files |
10 | 1072 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1073 |
round = round.next(); |
10 | 1074 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1075 |
List<JCCompilationUnit> parsedFiles = round.parseNewSourceFiles(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1076 |
roots = cleanTrees(roots).appendList(parsedFiles); |
10 | 1077 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1078 |
// Check for errors after parsing |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1079 |
if (round.unrecoverableError()) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1080 |
errorStatus = true; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1081 |
break runAround; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1082 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1083 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1084 |
List<ClassSymbol> newClasses = round.enterNewClassFiles(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1085 |
round.enterTrees(roots); |
10 | 1086 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1087 |
round.topLevelClasses = join( |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1088 |
getTopLevelClasses(parsedFiles), |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1089 |
getTopLevelClassesFromClasses(newClasses)); |
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1090 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1091 |
round.packageInfoFiles = join( |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1092 |
getPackageInfoFiles(parsedFiles), |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1093 |
getPackageInfoFilesFromClasses(newClasses)); |
10 | 1094 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1095 |
round.findAnnotationsPresent(annotationComputer); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1096 |
round.updateProcessingState(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1097 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1098 |
|
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1099 |
// run last round |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1100 |
round.run(true, errorStatus); |
10 | 1101 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1102 |
// Add any sources generated during the last round to the set |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1103 |
// of files to be compiled. |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1104 |
if (moreToDo()) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1105 |
List<JCCompilationUnit> parsedFiles = round.parseNewSourceFiles(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1106 |
roots = cleanTrees(roots).appendList(parsedFiles); |
10 | 1107 |
} |
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1108 |
|
4934
c6a2cf3b6b82
6634138: Source generated in last round not compiled
darcy
parents:
4877
diff
changeset
|
1109 |
// Set error status for any files compiled and generated in |
c6a2cf3b6b82
6634138: Source generated in last round not compiled
darcy
parents:
4877
diff
changeset
|
1110 |
// the last round |
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1111 |
if (round.unrecoverableError() || (werror && round.warningCount() != 0)) |
4934
c6a2cf3b6b82
6634138: Source generated in last round not compiled
darcy
parents:
4877
diff
changeset
|
1112 |
errorStatus = true; |
10 | 1113 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1114 |
round = round.next(); |
4934
c6a2cf3b6b82
6634138: Source generated in last round not compiled
darcy
parents:
4877
diff
changeset
|
1115 |
|
10 | 1116 |
filer.warnIfUnclosedFiles(); |
1117 |
warnIfUnmatchedOptions(); |
|
1118 |
||
1119 |
/* |
|
1120 |
* If an annotation processor raises an error in a round, |
|
1121 |
* that round runs to completion and one last round occurs. |
|
1122 |
* The last round may also occur because no more source or |
|
1123 |
* class files have been generated. Therefore, if an error |
|
1124 |
* was raised on either of the last *two* rounds, the compile |
|
1125 |
* should exit with a nonzero exit code. The current value of |
|
1126 |
* errorStatus holds whether or not an error was raised on the |
|
1127 |
* second to last round; errorRaised() gives the error status |
|
1128 |
* of the last round. |
|
1129 |
*/ |
|
3764
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
1130 |
errorStatus = errorStatus || messager.errorRaised(); |
10 | 1131 |
|
1132 |
// Free resources |
|
1133 |
this.close(); |
|
1134 |
||
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1135 |
TaskListener taskListener = this.context.get(TaskListener.class); |
10 | 1136 |
if (taskListener != null) |
1137 |
taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING)); |
|
1138 |
||
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1139 |
JavaCompiler compiler; |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1140 |
|
10 | 1141 |
if (errorStatus) { |
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1142 |
compiler = round.compiler; |
6153
277c719ff46e
6403456: -Werror should work with annotation processing
jjg
parents:
6143
diff
changeset
|
1143 |
compiler.log.nwarnings += messager.warningCount(); |
10 | 1144 |
compiler.log.nerrors += messager.errorCount(); |
1145 |
if (compiler.errorCount() == 0) |
|
1146 |
compiler.log.nerrors++; |
|
3151
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
1147 |
} else if (procOnly && !foundTypeProcessors) { |
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1148 |
compiler = round.compiler; |
10 | 1149 |
compiler.todo.clear(); |
1150 |
} else { // Final compilation |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1151 |
round = round.next(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1152 |
round.updateProcessingState(); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1153 |
compiler = round.compiler; |
3151
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
1154 |
if (procOnly && foundTypeProcessors) |
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
1155 |
compiler.shouldStopPolicy = CompileState.FLOW; |
10 | 1156 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1157 |
compiler.enterTrees(cleanTrees(roots)); |
10 | 1158 |
} |
1159 |
||
1160 |
return compiler; |
|
1161 |
} |
|
1162 |
||
1163 |
private void warnIfUnmatchedOptions() { |
|
1164 |
if (!unmatchedProcessorOptions.isEmpty()) { |
|
1165 |
log.warning("proc.unmatched.processor.options", unmatchedProcessorOptions.toString()); |
|
1166 |
} |
|
1167 |
} |
|
1168 |
||
1169 |
/** |
|
1170 |
* Free resources related to annotation processing. |
|
1171 |
*/ |
|
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3300
diff
changeset
|
1172 |
public void close() throws IOException { |
10 | 1173 |
filer.close(); |
3764
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
1174 |
if (discoveredProcs != null) // Make calling close idempotent |
f0077f165983
6548708: Annotation processing should free service loader if there are no processors
darcy
parents:
3656
diff
changeset
|
1175 |
discoveredProcs.close(); |
10 | 1176 |
discoveredProcs = null; |
3656
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3300
diff
changeset
|
1177 |
if (processorClassLoader != null && processorClassLoader instanceof Closeable) |
d4e34b76b0c3
6558476: com/sun/tools/javac/Main.compile don't release file handles on return
jjg
parents:
3300
diff
changeset
|
1178 |
((Closeable) processorClassLoader).close(); |
10 | 1179 |
} |
1180 |
||
1181 |
private List<ClassSymbol> getTopLevelClasses(List<? extends JCCompilationUnit> units) { |
|
1182 |
List<ClassSymbol> classes = List.nil(); |
|
1183 |
for (JCCompilationUnit unit : units) { |
|
1184 |
for (JCTree node : unit.defs) { |
|
1185 |
if (node.getTag() == JCTree.CLASSDEF) { |
|
1186 |
classes = classes.prepend(((JCClassDecl) node).sym); |
|
1187 |
} |
|
1188 |
} |
|
1189 |
} |
|
1190 |
return classes.reverse(); |
|
1191 |
} |
|
1192 |
||
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1193 |
private List<ClassSymbol> getTopLevelClassesFromClasses(List<? extends ClassSymbol> syms) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1194 |
List<ClassSymbol> classes = List.nil(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1195 |
for (ClassSymbol sym : syms) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1196 |
if (!isPkgInfo(sym)) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1197 |
classes = classes.prepend(sym); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1198 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1199 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1200 |
return classes.reverse(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1201 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1202 |
|
10 | 1203 |
private List<PackageSymbol> getPackageInfoFiles(List<? extends JCCompilationUnit> units) { |
1204 |
List<PackageSymbol> packages = List.nil(); |
|
1205 |
for (JCCompilationUnit unit : units) { |
|
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1206 |
if (isPkgInfo(unit.sourcefile, JavaFileObject.Kind.SOURCE)) { |
10 | 1207 |
packages = packages.prepend(unit.packge); |
1208 |
} |
|
1209 |
} |
|
1210 |
return packages.reverse(); |
|
1211 |
} |
|
1212 |
||
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1213 |
private List<PackageSymbol> getPackageInfoFilesFromClasses(List<? extends ClassSymbol> syms) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1214 |
List<PackageSymbol> packages = List.nil(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1215 |
for (ClassSymbol sym : syms) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1216 |
if (isPkgInfo(sym)) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1217 |
packages = packages.prepend((PackageSymbol) sym.owner); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1218 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1219 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1220 |
return packages.reverse(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1221 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1222 |
|
6159
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1223 |
// avoid unchecked warning from use of varargs |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1224 |
private static <T> List<T> join(List<T> list1, List<T> list2) { |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1225 |
return list1.appendList(list2); |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1226 |
} |
88930a1c409e
6966604: JavacFiler not correctly notified of lastRound
jjg
parents:
6153
diff
changeset
|
1227 |
|
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1228 |
private boolean isPkgInfo(JavaFileObject fo, JavaFileObject.Kind kind) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1229 |
return fo.isNameCompatible("package-info", kind); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1230 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1231 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1232 |
private boolean isPkgInfo(ClassSymbol sym) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1233 |
return isPkgInfo(sym.classfile, JavaFileObject.Kind.CLASS) && (sym.packge().package_info == sym); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1234 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
3764
diff
changeset
|
1235 |
|
10 | 1236 |
/* |
1237 |
* Called retroactively to determine if a class loader was required, |
|
1238 |
* after we have failed to create one. |
|
1239 |
*/ |
|
1240 |
private boolean needClassLoader(String procNames, Iterable<? extends File> workingpath) { |
|
1241 |
if (procNames != null) |
|
1242 |
return true; |
|
1243 |
||
1244 |
String procPath; |
|
1245 |
URL[] urls = new URL[1]; |
|
1246 |
for(File pathElement : workingpath) { |
|
1247 |
try { |
|
1248 |
urls[0] = pathElement.toURI().toURL(); |
|
1249 |
if (ServiceProxy.hasService(Processor.class, urls)) |
|
1250 |
return true; |
|
1251 |
} catch (MalformedURLException ex) { |
|
1252 |
throw new AssertionError(ex); |
|
1253 |
} |
|
1254 |
catch (ServiceProxy.ServiceConfigurationError e) { |
|
1255 |
log.error("proc.bad.config.file", e.getLocalizedMessage()); |
|
1256 |
return true; |
|
1257 |
} |
|
1258 |
} |
|
1259 |
||
1260 |
return false; |
|
1261 |
} |
|
1262 |
||
1263 |
private static <T extends JCTree> List<T> cleanTrees(List<T> nodes) { |
|
1264 |
for (T node : nodes) |
|
1265 |
treeCleaner.scan(node); |
|
1266 |
return nodes; |
|
1267 |
} |
|
1268 |
||
1269 |
private static TreeScanner treeCleaner = new TreeScanner() { |
|
1270 |
public void scan(JCTree node) { |
|
1271 |
super.scan(node); |
|
1272 |
if (node != null) |
|
1273 |
node.type = null; |
|
1274 |
} |
|
1275 |
public void visitTopLevel(JCCompilationUnit node) { |
|
1276 |
node.packge = null; |
|
1277 |
super.visitTopLevel(node); |
|
1278 |
} |
|
1279 |
public void visitClassDef(JCClassDecl node) { |
|
1280 |
node.sym = null; |
|
1281 |
super.visitClassDef(node); |
|
1282 |
} |
|
1283 |
public void visitMethodDef(JCMethodDecl node) { |
|
1284 |
node.sym = null; |
|
1285 |
super.visitMethodDef(node); |
|
1286 |
} |
|
1287 |
public void visitVarDef(JCVariableDecl node) { |
|
1288 |
node.sym = null; |
|
1289 |
super.visitVarDef(node); |
|
1290 |
} |
|
1291 |
public void visitNewClass(JCNewClass node) { |
|
1292 |
node.constructor = null; |
|
1293 |
super.visitNewClass(node); |
|
1294 |
} |
|
1295 |
public void visitAssignop(JCAssignOp node) { |
|
1296 |
node.operator = null; |
|
1297 |
super.visitAssignop(node); |
|
1298 |
} |
|
1299 |
public void visitUnary(JCUnary node) { |
|
1300 |
node.operator = null; |
|
1301 |
super.visitUnary(node); |
|
1302 |
} |
|
1303 |
public void visitBinary(JCBinary node) { |
|
1304 |
node.operator = null; |
|
1305 |
super.visitBinary(node); |
|
1306 |
} |
|
1307 |
public void visitSelect(JCFieldAccess node) { |
|
1308 |
node.sym = null; |
|
1309 |
super.visitSelect(node); |
|
1310 |
} |
|
1311 |
public void visitIdent(JCIdent node) { |
|
1312 |
node.sym = null; |
|
1313 |
super.visitIdent(node); |
|
1314 |
} |
|
1315 |
}; |
|
1316 |
||
1317 |
||
1318 |
private boolean moreToDo() { |
|
1319 |
return filer.newFiles(); |
|
1320 |
} |
|
1321 |
||
1322 |
/** |
|
1323 |
* {@inheritdoc} |
|
1324 |
* |
|
1325 |
* Command line options suitable for presenting to annotation |
|
1326 |
* processors. "-Afoo=bar" should be "-Afoo" => "bar". |
|
1327 |
*/ |
|
1328 |
public Map<String,String> getOptions() { |
|
1329 |
return processorOptions; |
|
1330 |
} |
|
1331 |
||
1332 |
public Messager getMessager() { |
|
1333 |
return messager; |
|
1334 |
} |
|
1335 |
||
1336 |
public Filer getFiler() { |
|
1337 |
return filer; |
|
1338 |
} |
|
1339 |
||
1340 |
public JavacElements getElementUtils() { |
|
1341 |
return elementUtils; |
|
1342 |
} |
|
1343 |
||
1344 |
public JavacTypes getTypeUtils() { |
|
1345 |
return typeUtils; |
|
1346 |
} |
|
1347 |
||
1348 |
public SourceVersion getSourceVersion() { |
|
1349 |
return Source.toSourceVersion(source); |
|
1350 |
} |
|
1351 |
||
1352 |
public Locale getLocale() { |
|
1471
57506cdfb7b4
6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents:
1260
diff
changeset
|
1353 |
return messages.getCurrentLocale(); |
10 | 1354 |
} |
1355 |
||
1356 |
public Set<Symbol.PackageSymbol> getSpecifiedPackages() { |
|
1357 |
return specifiedPackages; |
|
1358 |
} |
|
1359 |
||
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1360 |
private static final Pattern allMatches = Pattern.compile(".*"); |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1361 |
public static final Pattern noMatches = Pattern.compile("(\\P{all})+"); |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1362 |
|
10 | 1363 |
/** |
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1364 |
* Convert import-style string for supported annotations into a |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1365 |
* regex matching that string. If the string is a valid |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1366 |
* import-style string, return a regex that won't match anything. |
10 | 1367 |
*/ |
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1368 |
private static Pattern importStringToPattern(String s, Processor p, Log log) { |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1369 |
if (isValidImportString(s)) { |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1370 |
return validImportStringToPattern(s); |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1371 |
} else { |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1372 |
log.warning("proc.malformed.supported.string", s, p.getClass().getName()); |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1373 |
return noMatches; // won't match any valid identifier |
10 | 1374 |
} |
1375 |
} |
|
1376 |
||
1377 |
/** |
|
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1378 |
* Return true if the argument string is a valid import-style |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1379 |
* string specifying claimed annotations; return false otherwise. |
10 | 1380 |
*/ |
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1381 |
public static boolean isValidImportString(String s) { |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1382 |
if (s.equals("*")) |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1383 |
return true; |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1384 |
|
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1385 |
boolean valid = true; |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1386 |
String t = s; |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1387 |
int index = t.indexOf('*'); |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1388 |
|
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1389 |
if (index != -1) { |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1390 |
// '*' must be last character... |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1391 |
if (index == t.length() -1) { |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1392 |
// ... any and preceding character must be '.' |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1393 |
if ( index-1 >= 0 ) { |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1394 |
valid = t.charAt(index-1) == '.'; |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1395 |
// Strip off ".*$" for identifier checks |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1396 |
t = t.substring(0, t.length()-2); |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1397 |
} |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1398 |
} else |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1399 |
return false; |
10 | 1400 |
} |
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1401 |
|
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1402 |
// Verify string is off the form (javaId \.)+ or javaId |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1403 |
if (valid) { |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1404 |
String[] javaIds = t.split("\\.", t.length()+2); |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1405 |
for(String javaId: javaIds) |
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1406 |
valid &= SourceVersion.isIdentifier(javaId); |
10 | 1407 |
} |
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1408 |
return valid; |
10 | 1409 |
} |
1410 |
||
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1411 |
public static Pattern validImportStringToPattern(String s) { |
10 | 1412 |
if (s.equals("*")) { |
1413 |
return allMatches; |
|
1414 |
} else { |
|
4937
2fc03fb01efa
6927061: Refactor apt implemenation to use code from JSR 269
darcy
parents:
4934
diff
changeset
|
1415 |
String s_prime = s.replace(".", "\\."); |
10 | 1416 |
|
1417 |
if (s_prime.endsWith("*")) { |
|
1418 |
s_prime = s_prime.substring(0, s_prime.length() - 1) + ".+"; |
|
1419 |
} |
|
1420 |
||
1421 |
return Pattern.compile(s_prime); |
|
1422 |
} |
|
1423 |
} |
|
1424 |
||
1425 |
/** |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5841
diff
changeset
|
1426 |
* For internal use only. This method will be |
10 | 1427 |
* removed without warning. |
1428 |
*/ |
|
1429 |
public Context getContext() { |
|
1430 |
return context; |
|
1431 |
} |
|
1432 |
||
1433 |
public String toString() { |
|
1434 |
return "javac ProcessingEnvironment"; |
|
1435 |
} |
|
1436 |
||
1437 |
public static boolean isValidOptionName(String optionName) { |
|
1438 |
for(String s : optionName.split("\\.", -1)) { |
|
1439 |
if (!SourceVersion.isIdentifier(s)) |
|
1440 |
return false; |
|
1441 |
} |
|
1442 |
return true; |
|
1443 |
} |
|
1444 |
} |