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