Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.compiler/share/classes/javax/annotation/processing/Processor.java
41159 views
1
/*
2
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package javax.annotation.processing;
27
28
import java.util.Set;
29
import javax.lang.model.util.Elements;
30
import javax.lang.model.AnnotatedConstruct;
31
import javax.lang.model.element.*;
32
import javax.lang.model.SourceVersion;
33
34
/**
35
* The interface for an annotation processor.
36
*
37
* <p>Annotation processing happens in a sequence of {@linkplain
38
* javax.annotation.processing.RoundEnvironment rounds}. On each
39
* round, a processor may be asked to {@linkplain #process process} a
40
* subset of the annotations found on the source and class files
41
* produced by a prior round. The inputs to the first round of
42
* processing are the initial inputs to a run of the tool; these
43
* initial inputs can be regarded as the output of a virtual zeroth
44
* round of processing. If a processor was asked to process on a
45
* given round, it will be asked to process on subsequent rounds,
46
* including the last round, even if there are no annotations for it
47
* to process. The tool infrastructure may also ask a processor to
48
* process files generated implicitly by the tool's operation.
49
*
50
* <p> Each implementation of a {@code Processor} must provide a
51
* public no-argument constructor to be used by tools to instantiate
52
* the processor. The tool infrastructure will interact with classes
53
* implementing this interface as follows:
54
*
55
* <ol>
56
*
57
* <li>If an existing {@code Processor} object is not being used, to
58
* create an instance of a processor the tool calls the no-arg
59
* constructor of the processor class.
60
*
61
* <li>Next, the tool calls the {@link #init init} method with
62
* an appropriate {@link ProcessingEnvironment}.
63
*
64
* <li>Afterwards, the tool calls {@link #getSupportedAnnotationTypes
65
* getSupportedAnnotationTypes}, {@link #getSupportedOptions
66
* getSupportedOptions}, and {@link #getSupportedSourceVersion
67
* getSupportedSourceVersion}. These methods are only called once per
68
* run, not on each round.
69
*
70
* <li>As appropriate, the tool calls the {@link #process process}
71
* method on the {@code Processor} object; a new {@code Processor}
72
* object is <em>not</em> created for each round.
73
*
74
* </ol>
75
*
76
* If a processor object is created and used without the above
77
* protocol being followed, then the processor's behavior is not
78
* defined by this interface specification.
79
*
80
* <p> The tool uses a <i>discovery process</i> to find annotation
81
* processors and decide whether or not they should be run. By
82
* configuring the tool, the set of potential processors can be
83
* controlled. For example, for a {@link javax.tools.JavaCompiler
84
* JavaCompiler} the list of candidate processors to run can be
85
* {@linkplain javax.tools.JavaCompiler.CompilationTask#setProcessors
86
* set directly} or controlled by a {@linkplain
87
* javax.tools.StandardLocation#ANNOTATION_PROCESSOR_PATH search path}
88
* used for a {@linkplain java.util.ServiceLoader service-style}
89
* lookup. Other tool implementations may have different
90
* configuration mechanisms, such as command line options; for
91
* details, refer to the particular tool's documentation. Which
92
* processors the tool asks to {@linkplain #process run} is a function
93
* of the interfaces of the annotations <em>{@linkplain
94
* AnnotatedConstruct present}</em> on the {@linkplain
95
* RoundEnvironment#getRootElements root elements}, what {@linkplain
96
* #getSupportedAnnotationTypes annotation interfaces a processor
97
* supports}, and whether or not a processor {@linkplain #process
98
* claims the annotation interfaces it processes}. A processor will
99
* be asked to process a subset of the annotation interfaces it
100
* supports, possibly an empty set.
101
*
102
* For a given round, the tool computes the set of annotation
103
* interfaces that are present on the elements enclosed within the
104
* root elements. If there is at least one annotation interface
105
* present, then as processors claim annotation interfaces, they are
106
* removed from the set of unmatched annotation interfaces. When the
107
* set is empty or no more processors are available, the round has run
108
* to completion. If there are no annotation interfaces present,
109
* annotation processing still occurs but only <i>universal
110
* processors</i> which support processing all annotation interfaces,
111
* {@code "*"}, can claim the (empty) set of annotation interfaces.
112
*
113
* <p>An annotation interface is considered present if there is at least
114
* one annotation of that interface present on an element enclosed within
115
* the root elements of a round. For this purpose, a type parameter is
116
* considered to be enclosed by its {@linkplain
117
* TypeParameterElement#getGenericElement generic
118
* element}.
119
120
* For this purpose, a package element is <em>not</em> considered to
121
* enclose the top-level classes and interfaces within that
122
* package. (A root element representing a package is created when a
123
* {@code package-info} file is processed.) Likewise, for this
124
* purpose, a module element is <em>not</em> considered to enclose the
125
* packages within that module. (A root element representing a module
126
* is created when a {@code module-info} file is processed.)
127
*
128
* Annotations on {@linkplain
129
* java.lang.annotation.ElementType#TYPE_USE type uses}, as opposed to
130
* annotations on elements, are ignored when computing whether or not
131
* an annotation interface is present.
132
*
133
* <p>An annotation is <em>present</em> if it meets the definition of being
134
* present given in {@link AnnotatedConstruct}. In brief, an
135
* annotation is considered present for the purposes of discovery if
136
* it is directly present or present via inheritance. An annotation is
137
* <em>not</em> considered present by virtue of being wrapped by a
138
* container annotation. Operationally, this is equivalent to an
139
* annotation being present on an element if and only if it would be
140
* included in the results of {@link
141
* Elements#getAllAnnotationMirrors(Element)} called on that element. Since
142
* annotations inside container annotations are not considered
143
* present, to properly process {@linkplain
144
* java.lang.annotation.Repeatable repeatable annotation interfaces},
145
* processors are advised to include both the repeatable annotation
146
* interface and its containing annotation interface in the set of {@linkplain
147
* #getSupportedAnnotationTypes() supported annotation interfaces} of a
148
* processor.
149
*
150
* <p>Note that if a processor supports {@code "*"} and returns {@code
151
* true}, all annotations are claimed. Therefore, a universal
152
* processor being used to, for example, implement additional validity
153
* checks should return {@code false} so as to not prevent other such
154
* checkers from being able to run.
155
*
156
* <p>If a processor throws an uncaught exception, the tool may cease
157
* other active annotation processors. If a processor raises an
158
* error, the current round will run to completion and the subsequent
159
* round will indicate an {@linkplain RoundEnvironment#errorRaised
160
* error was raised}. Since annotation processors are run in a
161
* cooperative environment, a processor should throw an uncaught
162
* exception only in situations where no error recovery or reporting
163
* is feasible.
164
*
165
* <p>The tool environment is not required to support annotation
166
* processors that access environmental resources, either {@linkplain
167
* RoundEnvironment per round} or {@linkplain ProcessingEnvironment
168
* cross-round}, in a multi-threaded fashion.
169
*
170
* <p>If the methods that return configuration information about the
171
* annotation processor return {@code null}, return other invalid
172
* input, or throw an exception, the tool infrastructure must treat
173
* this as an error condition.
174
*
175
* <p>To be robust when running in different tool implementations, an
176
* annotation processor should have the following properties:
177
*
178
* <ol>
179
*
180
* <li>The result of processing a given input is not a function of the presence or absence
181
* of other inputs (orthogonality).
182
*
183
* <li>Processing the same input produces the same output (consistency).
184
*
185
* <li>Processing input <i>A</i> followed by processing input <i>B</i>
186
* is equivalent to processing <i>B</i> then <i>A</i>
187
* (commutativity)
188
*
189
* <li>Processing an input does not rely on the presence of the output
190
* of other annotation processors (independence)
191
*
192
* </ol>
193
*
194
* <p>The {@link Filer} interface discusses restrictions on how
195
* processors can operate on files.
196
*
197
* @apiNote Implementors of this interface may find it convenient
198
* to extend {@link AbstractProcessor} rather than implementing this
199
* interface directly.
200
*
201
* @author Joseph D. Darcy
202
* @author Scott Seligman
203
* @author Peter von der Ah&eacute;
204
* @since 1.6
205
*/
206
public interface Processor {
207
/**
208
* Returns the options recognized by this processor. An
209
* implementation of the processing tool must provide a way to
210
* pass processor-specific options distinctly from options passed
211
* to the tool itself, see {@link ProcessingEnvironment#getOptions
212
* getOptions}.
213
*
214
* <p>Each string returned in the set must be a period separated
215
* sequence of {@linkplain
216
* javax.lang.model.SourceVersion#isIdentifier identifiers}:
217
*
218
* <blockquote>
219
* <dl>
220
* <dt><i>SupportedOptionString:</i>
221
* <dd><i>Identifiers</i>
222
*
223
* <dt><i>Identifiers:</i>
224
* <dd> <i>Identifier</i>
225
* <dd> <i>Identifier</i> {@code .} <i>Identifiers</i>
226
*
227
* <dt><i>Identifier:</i>
228
* <dd>Syntactic identifier, including keywords and literals
229
* </dl>
230
* </blockquote>
231
*
232
* <p> A tool might use this information to determine if any
233
* options provided by a user are unrecognized by any processor,
234
* in which case it may wish to report a warning.
235
*
236
* @return the options recognized by this processor or an
237
* empty set if none
238
* @see javax.annotation.processing.SupportedOptions
239
*/
240
Set<String> getSupportedOptions();
241
242
/**
243
* Returns the names of the annotation interfaces supported by this
244
* processor. An element of the result may be the canonical
245
* (fully qualified) name of a supported annotation interface.
246
* Alternately it may be of the form &quot;<code><i>name</i>.*</code>&quot;
247
* representing the set of all annotation interfaces with canonical
248
* names beginning with &quot;<code><i>name.</i></code>&quot;.
249
*
250
* In either of those cases, the name of the annotation interface can
251
* be optionally preceded by a module name followed by a {@code
252
* "/"} character. For example, if a processor supports {@code
253
* "a.B"}, this can include multiple annotation interfaces named {@code
254
* a.B} which reside in different modules. To only support {@code
255
* a.B} in the {@code foo} module, instead use {@code "foo/a.B"}.
256
*
257
* If a module name is included, only an annotation in that module
258
* is matched. In particular, if a module name is given in an
259
* environment where modules are not supported, such as an
260
* annotation processing environment configured for a {@linkplain
261
* javax.annotation.processing.ProcessingEnvironment#getSourceVersion
262
* source version} without modules, then the annotation interfaces with
263
* a module name do <em>not</em> match.
264
*
265
* Finally, {@code "*"} by itself represents the set of all
266
* annotation interfaces, including the empty set. Note that a
267
* processor should not claim {@code "*"} unless it is actually
268
* processing all files; claiming unnecessary annotations may
269
* cause a performance slowdown in some environments.
270
*
271
* <p>Each string returned in the set must be accepted by the
272
* following grammar:
273
*
274
* <blockquote>
275
* <dl>
276
* <dt><i>SupportedAnnotationTypeString:</i>
277
* <dd><i>ModulePrefix</i><sub><i>opt</i></sub> <i>TypeName</i> <i>DotStar</i><sub><i>opt</i></sub>
278
* <dd><code>*</code>
279
*
280
* <dt><i>ModulePrefix:</i>
281
* <dd><i>ModuleName</i> <code>/</code>
282
*
283
* <dt><i>DotStar:</i>
284
* <dd><code>.</code> <code>*</code>
285
* </dl>
286
* </blockquote>
287
*
288
* where <i>TypeName</i> and <i>ModuleName</i> are as defined in
289
* <cite>The Java Language Specification</cite>
290
* ({@jls 6.5 Determining the Meaning of a Name}).
291
*
292
* @apiNote When running in an environment which supports modules,
293
* processors are encouraged to include the module prefix when
294
* describing their supported annotation interfaces. The method {@link
295
* AbstractProcessor#getSupportedAnnotationTypes
296
* AbstractProcessor.getSupportedAnnotationTypes} provides support
297
* for stripping off the module prefix when running in an
298
* environment without modules.
299
*
300
* @return the names of the annotation interfaces supported by this processor
301
* or an empty set if none
302
* @see javax.annotation.processing.SupportedAnnotationTypes
303
* @jls 3.8 Identifiers
304
*/
305
Set<String> getSupportedAnnotationTypes();
306
307
/**
308
* {@return the latest source version supported by this annotation
309
* processor}
310
*
311
* @see javax.annotation.processing.SupportedSourceVersion
312
* @see ProcessingEnvironment#getSourceVersion
313
*/
314
SourceVersion getSupportedSourceVersion();
315
316
/**
317
* Initializes the processor with the processing environment.
318
*
319
* @param processingEnv environment for facilities the tool framework
320
* provides to the processor
321
*/
322
void init(ProcessingEnvironment processingEnv);
323
324
/**
325
* Processes a set of annotation interfaces on type elements
326
* originating from the prior round and returns whether or not
327
* these annotation interfaces are claimed by this processor. If {@code
328
* true} is returned, the annotation interfaces are claimed and subsequent
329
* processors will not be asked to process them; if {@code false}
330
* is returned, the annotation interfaces are unclaimed and subsequent
331
* processors may be asked to process them. A processor may
332
* always return the same boolean value or may vary the result
333
* based on its own chosen criteria.
334
*
335
* <p>The input set will be empty if the processor supports {@code
336
* "*"} and the root elements have no annotations. A {@code
337
* Processor} must gracefully handle an empty set of annotations.
338
*
339
* @param annotations the annotation interfaces requested to be processed
340
* @param roundEnv environment for information about the current and prior round
341
* @return whether or not the set of annotation interfaces are claimed by this processor
342
*/
343
boolean process(Set<? extends TypeElement> annotations,
344
RoundEnvironment roundEnv);
345
346
/**
347
* Returns to the tool infrastructure an iterable of suggested
348
* completions to an annotation. Since completions are being asked
349
* for, the information provided about the annotation may be
350
* incomplete, as if for a source code fragment. A processor may
351
* return an empty iterable. Annotation processors should focus
352
* their efforts on providing completions for annotation members
353
* with additional validity constraints known to the processor, for
354
* example an {@code int} member whose value should lie between 1
355
* and 10 or a string member that should be recognized by a known
356
* grammar, such as a regular expression or a URL.
357
*
358
* <p>Since incomplete programs are being modeled, some of the
359
* parameters may only have partial information or may be {@code
360
* null}. At least one of {@code element} and {@code userText}
361
* must be non-{@code null}. If {@code element} is non-{@code null},
362
* {@code annotation} and {@code member} may be {@code
363
* null}. Processors may not throw a {@code NullPointerException}
364
* if some parameters are {@code null}; if a processor has no
365
* completions to offer based on the provided information, an
366
* empty iterable can be returned. The processor may also return
367
* a single completion with an empty value string and a message
368
* describing why there are no completions.
369
*
370
* <p>Completions are informative and may reflect additional
371
* validity checks performed by annotation processors. For
372
* example, consider the simple annotation:
373
*
374
* <blockquote>
375
* <pre>
376
* &#064;MersennePrime {
377
* int value();
378
* }
379
* </pre>
380
* </blockquote>
381
*
382
* (A Mersenne prime is prime number of the form
383
* 2<sup><i>n</i></sup> - 1.) Given an {@code AnnotationMirror}
384
* for this annotation interface, a list of all such primes in the
385
* {@code int} range could be returned without examining any other
386
* arguments to {@code getCompletions}:
387
*
388
* <blockquote>
389
* <pre>
390
* import static javax.annotation.processing.Completions.*;
391
* ...
392
* return List.of({@link Completions#of(String) of}(&quot;3&quot;),
393
* of(&quot;7&quot;),
394
* of(&quot;31&quot;),
395
* of(&quot;127&quot;),
396
* of(&quot;8191&quot;),
397
* of(&quot;131071&quot;),
398
* of(&quot;524287&quot;),
399
* of(&quot;2147483647&quot;));
400
* </pre>
401
* </blockquote>
402
*
403
* A more informative set of completions would include the number
404
* of each prime:
405
*
406
* <blockquote>
407
* <pre>
408
* return List.of({@link Completions#of(String, String) of}(&quot;3&quot;, &quot;M2&quot;),
409
* of(&quot;7&quot;, &quot;M3&quot;),
410
* of(&quot;31&quot;, &quot;M5&quot;),
411
* of(&quot;127&quot;, &quot;M7&quot;),
412
* of(&quot;8191&quot;, &quot;M13&quot;),
413
* of(&quot;131071&quot;, &quot;M17&quot;),
414
* of(&quot;524287&quot;, &quot;M19&quot;),
415
* of(&quot;2147483647&quot;, &quot;M31&quot;));
416
* </pre>
417
* </blockquote>
418
*
419
* However, if the {@code userText} is available, it can be checked
420
* to see if only a subset of the Mersenne primes are valid. For
421
* example, if the user has typed
422
*
423
* <blockquote>
424
* <code>
425
* &#064;MersennePrime(1
426
* </code>
427
* </blockquote>
428
*
429
* the value of {@code userText} will be {@code "1"}; and only
430
* two of the primes are possible completions:
431
*
432
* <blockquote>
433
* <pre>
434
* return Arrays.asList(of(&quot;127&quot;, &quot;M7&quot;),
435
* of(&quot;131071&quot;, &quot;M17&quot;));
436
* </pre>
437
* </blockquote>
438
*
439
* Sometimes no valid completion is possible. For example, there
440
* is no in-range Mersenne prime starting with 9:
441
*
442
* <blockquote>
443
* <code>
444
* &#064;MersennePrime(9
445
* </code>
446
* </blockquote>
447
*
448
* An appropriate response in this case is to either return an
449
* empty list of completions,
450
*
451
* <blockquote>
452
* <pre>
453
* return Collections.emptyList();
454
* </pre>
455
* </blockquote>
456
*
457
* or a single empty completion with a helpful message
458
*
459
* <blockquote>
460
* <pre>
461
* return Arrays.asList(of(&quot;&quot;, &quot;No in-range Mersenne primes start with 9&quot;));
462
* </pre>
463
* </blockquote>
464
*
465
* @param element the element being annotated
466
* @param annotation the (perhaps partial) annotation being
467
* applied to the element
468
* @param member the annotation member to return possible completions for
469
* @param userText source code text to be completed
470
*
471
* @return suggested completions to the annotation
472
*/
473
Iterable<? extends Completion> getCompletions(Element element,
474
AnnotationMirror annotation,
475
ExecutableElement member,
476
String userText);
477
}
478
479