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