Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/javax/annotation/processing/Processor.java
38913 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.annotation.processing;2627import java.util.Set;28import javax.lang.model.util.Elements;29import javax.lang.model.AnnotatedConstruct;30import javax.lang.model.element.*;31import javax.lang.model.SourceVersion;3233/**34* The interface for an annotation processor.35*36* <p>Annotation processing happens in a sequence of {@linkplain37* javax.annotation.processing.RoundEnvironment rounds}. On each38* round, a processor may be asked to {@linkplain #process process} a39* subset of the annotations found on the source and class files40* produced by a prior round. The inputs to the first round of41* processing are the initial inputs to a run of the tool; these42* initial inputs can be regarded as the output of a virtual zeroth43* round of processing. If a processor was asked to process on a44* given round, it will be asked to process on subsequent rounds,45* including the last round, even if there are no annotations for it46* to process. The tool infrastructure may also ask a processor to47* process files generated implicitly by the tool's operation.48*49* <p> Each implementation of a {@code Processor} must provide a50* public no-argument constructor to be used by tools to instantiate51* the processor. The tool infrastructure will interact with classes52* implementing this interface as follows:53*54* <ol>55*56* <li>If an existing {@code Processor} object is not being used, to57* create an instance of a processor the tool calls the no-arg58* constructor of the processor class.59*60* <li>Next, the tool calls the {@link #init init} method with61* an appropriate {@code ProcessingEnvironment}.62*63* <li>Afterwards, the tool calls {@link #getSupportedAnnotationTypes64* getSupportedAnnotationTypes}, {@link #getSupportedOptions65* getSupportedOptions}, and {@link #getSupportedSourceVersion66* getSupportedSourceVersion}. These methods are only called once per67* run, not on each round.68*69* <li>As appropriate, the tool calls the {@link #process process}70* method on the {@code Processor} object; a new {@code Processor}71* object is <em>not</em> created for each round.72*73* </ol>74*75* If a processor object is created and used without the above76* protocol being followed, then the processor's behavior is not77* defined by this interface specification.78*79* <p> The tool uses a <i>discovery process</i> to find annotation80* processors and decide whether or not they should be run. By81* configuring the tool, the set of potential processors can be82* controlled. For example, for a {@link javax.tools.JavaCompiler83* JavaCompiler} the list of candidate processors to run can be84* {@linkplain javax.tools.JavaCompiler.CompilationTask#setProcessors85* set directly} or controlled by a {@linkplain86* javax.tools.StandardLocation#ANNOTATION_PROCESSOR_PATH search path}87* used for a {@linkplain java.util.ServiceLoader service-style}88* lookup. Other tool implementations may have different89* configuration mechanisms, such as command line options; for90* details, refer to the particular tool's documentation. Which91* processors the tool asks to {@linkplain #process run} is a function92* of the types of the annotations <em>{@linkplain AnnotatedConstruct present}</em>93* on the {@linkplain94* RoundEnvironment#getRootElements root elements}, what {@linkplain95* #getSupportedAnnotationTypes annotation types a processor96* supports}, and whether or not a processor {@linkplain #process97* claims the annotation types it processes}. A processor will be asked to98* process a subset of the annotation types it supports, possibly an99* empty set.100*101* For a given round, the tool computes the set of annotation types102* that are present on the elements enclosed within the root elements.103* If there is at least one annotation type present, then as104* processors claim annotation types, they are removed from the set of105* unmatched annotation types. When the set is empty or no more106* processors are available, the round has run to completion. If107* there are no annotation types present, annotation processing still108* occurs but only <i>universal processors</i> which support109* processing all annotation types, {@code "*"}, can claim the (empty)110* set of annotation types.111*112* <p>An annotation type is considered present if there is at least113* one annotation of that type present on an element enclosed within114* the root elements of a round. For this purpose, a type parameter is115* considered to be enclosed by its {@linkplain116* TypeParameterElement#getGenericElement generic117* element}. Annotations on {@linkplain118* java.lang.annotation.ElementType#TYPE_USE type uses}, as opposed to119* annotations on elements, are ignored when computing whether or not120* an annotation type is present.121*122* <p>An annotation is present if it meets the definition of being123* present given in {@link AnnotatedConstruct}. In brief, an124* annotation is considered present for the purposes of discovery if125* it is directly present or present via inheritance. An annotation is126* <em>not</em> considered present by virtue of being wrapped by a127* container annotation. Operationally, this is equivalent to an128* annotation being present on an element if and only if it would be129* included in the results of {@link130* Elements#getAllAnnotationMirrors(Element)} called on that element. Since131* annotations inside container annotations are not considered132* present, to properly process {@linkplain133* java.lang.annotation.Repeatable repeatable annotation types},134* processors are advised to include both the repeatable annotation135* type and its containing annotation type in the set of {@linkplain136* #getSupportedAnnotationTypes() supported annotation types} of a137* processor.138*139* <p>Note that if a processor supports {@code "*"} and returns {@code140* true}, all annotations are claimed. Therefore, a universal141* processor being used to, for example, implement additional validity142* checks should return {@code false} so as to not prevent other such143* checkers from being able to run.144*145* <p>If a processor throws an uncaught exception, the tool may cease146* other active annotation processors. If a processor raises an147* error, the current round will run to completion and the subsequent148* round will indicate an {@linkplain RoundEnvironment#errorRaised149* error was raised}. Since annotation processors are run in a150* cooperative environment, a processor should throw an uncaught151* exception only in situations where no error recovery or reporting152* is feasible.153*154* <p>The tool environment is not required to support annotation155* processors that access environmental resources, either {@linkplain156* RoundEnvironment per round} or {@linkplain ProcessingEnvironment157* cross-round}, in a multi-threaded fashion.158*159* <p>If the methods that return configuration information about the160* annotation processor return {@code null}, return other invalid161* input, or throw an exception, the tool infrastructure must treat162* this as an error condition.163*164* <p>To be robust when running in different tool implementations, an165* annotation processor should have the following properties:166*167* <ol>168*169* <li>The result of processing a given input is not a function of the presence or absence170* of other inputs (orthogonality).171*172* <li>Processing the same input produces the same output (consistency).173*174* <li>Processing input <i>A</i> followed by processing input <i>B</i>175* is equivalent to processing <i>B</i> then <i>A</i>176* (commutativity)177*178* <li>Processing an input does not rely on the presence of the output179* of other annotation processors (independence)180*181* </ol>182*183* <p>The {@link Filer} interface discusses restrictions on how184* processors can operate on files.185*186* <p>Note that implementors of this interface may find it convenient187* to extend {@link AbstractProcessor} rather than implementing this188* interface directly.189*190* @author Joseph D. Darcy191* @author Scott Seligman192* @author Peter von der Ahé193* @since 1.6194*/195public interface Processor {196/**197* Returns the options recognized by this processor. An198* implementation of the processing tool must provide a way to199* pass processor-specific options distinctly from options passed200* to the tool itself, see {@link ProcessingEnvironment#getOptions201* getOptions}.202*203* <p>Each string returned in the set must be a period separated204* sequence of {@linkplain205* javax.lang.model.SourceVersion#isIdentifier identifiers}:206*207* <blockquote>208* <dl>209* <dt><i>SupportedOptionString:</i>210* <dd><i>Identifiers</i>211*212* <dt><i>Identifiers:</i>213* <dd> <i>Identifier</i>214* <dd> <i>Identifier</i> {@code .} <i>Identifiers</i>215*216* <dt><i>Identifier:</i>217* <dd>Syntactic identifier, including keywords and literals218* </dl>219* </blockquote>220*221* <p> A tool might use this information to determine if any222* options provided by a user are unrecognized by any processor,223* in which case it may wish to report a warning.224*225* @return the options recognized by this processor or an226* empty collection if none227* @see javax.annotation.processing.SupportedOptions228*/229Set<String> getSupportedOptions();230231/**232* Returns the names of the annotation types supported by this233* processor. An element of the result may be the canonical234* (fully qualified) name of a supported annotation type.235* Alternately it may be of the form "<tt><i>name</i>.*</tt>"236* representing the set of all annotation types with canonical237* names beginning with "<tt><i>name.</i></tt>". Finally, {@code238* "*"} by itself represents the set of all annotation types,239* including the empty set. Note that a processor should not240* claim {@code "*"} unless it is actually processing all files;241* claiming unnecessary annotations may cause a performance242* slowdown in some environments.243*244* <p>Each string returned in the set must be accepted by the245* following grammar:246*247* <blockquote>248* <dl>249* <dt><i>SupportedAnnotationTypeString:</i>250* <dd><i>TypeName</i> <i>DotStar</i><sub><i>opt</i></sub>251* <dd><tt>*</tt>252*253* <dt><i>DotStar:</i>254* <dd><tt>.</tt> <tt>*</tt>255* </dl>256* </blockquote>257*258* where <i>TypeName</i> is as defined in259* <cite>The Java™ Language Specification</cite>.260*261* @return the names of the annotation types supported by this processor262* @see javax.annotation.processing.SupportedAnnotationTypes263* @jls 3.8 Identifiers264* @jls 6.5.5 Meaning of Type Names265*/266Set<String> getSupportedAnnotationTypes();267268/**269* Returns the latest source version supported by this annotation270* processor.271*272* @return the latest source version supported by this annotation273* processor.274* @see javax.annotation.processing.SupportedSourceVersion275* @see ProcessingEnvironment#getSourceVersion276*/277SourceVersion getSupportedSourceVersion();278279/**280* Initializes the processor with the processing environment.281*282* @param processingEnv environment for facilities the tool framework283* provides to the processor284*/285void init(ProcessingEnvironment processingEnv);286287/**288* Processes a set of annotation types on type elements289* originating from the prior round and returns whether or not290* these annotation types are claimed by this processor. If {@code291* true} is returned, the annotation types are claimed and subsequent292* processors will not be asked to process them; if {@code false}293* is returned, the annotation types are unclaimed and subsequent294* processors may be asked to process them. A processor may295* always return the same boolean value or may vary the result296* based on chosen criteria.297*298* <p>The input set will be empty if the processor supports {@code299* "*"} and the root elements have no annotations. A {@code300* Processor} must gracefully handle an empty set of annotations.301*302* @param annotations the annotation types requested to be processed303* @param roundEnv environment for information about the current and prior round304* @return whether or not the set of annotation types are claimed by this processor305*/306boolean process(Set<? extends TypeElement> annotations,307RoundEnvironment roundEnv);308309/**310* Returns to the tool infrastructure an iterable of suggested311* completions to an annotation. Since completions are being asked312* for, the information provided about the annotation may be313* incomplete, as if for a source code fragment. A processor may314* return an empty iterable. Annotation processors should focus315* their efforts on providing completions for annotation members316* with additional validity constraints known to the processor, for317* example an {@code int} member whose value should lie between 1318* and 10 or a string member that should be recognized by a known319* grammar, such as a regular expression or a URL.320*321* <p>Since incomplete programs are being modeled, some of the322* parameters may only have partial information or may be {@code323* null}. At least one of {@code element} and {@code userText}324* must be non-{@code null}. If {@code element} is non-{@code325* null}, {@code annotation} and {@code member} may be {@code326* null}. Processors may not throw a {@code NullPointerException}327* if some parameters are {@code null}; if a processor has no328* completions to offer based on the provided information, an329* empty iterable can be returned. The processor may also return330* a single completion with an empty value string and a message331* describing why there are no completions.332*333* <p>Completions are informative and may reflect additional334* validity checks performed by annotation processors. For335* example, consider the simple annotation:336*337* <blockquote>338* <pre>339* @MersennePrime {340* int value();341* }342* </pre>343* </blockquote>344*345* (A Mersenne prime is prime number of the form346* 2<sup><i>n</i></sup> - 1.) Given an {@code AnnotationMirror}347* for this annotation type, a list of all such primes in the348* {@code int} range could be returned without examining any other349* arguments to {@code getCompletions}:350*351* <blockquote>352* <pre>353* import static javax.annotation.processing.Completions.*;354* ...355* return Arrays.asList({@link Completions#of(String) of}("3"),356* of("7"),357* of("31"),358* of("127"),359* of("8191"),360* of("131071"),361* of("524287"),362* of("2147483647"));363* </pre>364* </blockquote>365*366* A more informative set of completions would include the number367* of each prime:368*369* <blockquote>370* <pre>371* return Arrays.asList({@link Completions#of(String, String) of}("3", "M2"),372* of("7", "M3"),373* of("31", "M5"),374* of("127", "M7"),375* of("8191", "M13"),376* of("131071", "M17"),377* of("524287", "M19"),378* of("2147483647", "M31"));379* </pre>380* </blockquote>381*382* However, if the {@code userText} is available, it can be checked383* to see if only a subset of the Mersenne primes are valid. For384* example, if the user has typed385*386* <blockquote>387* <code>388* @MersennePrime(1389* </code>390* </blockquote>391*392* the value of {@code userText} will be {@code "1"}; and only393* two of the primes are possible completions:394*395* <blockquote>396* <pre>397* return Arrays.asList(of("127", "M7"),398* of("131071", "M17"));399* </pre>400* </blockquote>401*402* Sometimes no valid completion is possible. For example, there403* is no in-range Mersenne prime starting with 9:404*405* <blockquote>406* <code>407* @MersennePrime(9408* </code>409* </blockquote>410*411* An appropriate response in this case is to either return an412* empty list of completions,413*414* <blockquote>415* <pre>416* return Collections.emptyList();417* </pre>418* </blockquote>419*420* or a single empty completion with a helpful message421*422* <blockquote>423* <pre>424* return Arrays.asList(of("", "No in-range Mersenne primes start with 9"));425* </pre>426* </blockquote>427*428* @param element the element being annotated429* @param annotation the (perhaps partial) annotation being430* applied to the element431* @param member the annotation member to return possible completions for432* @param userText source code text to be completed433*434* @return suggested completions to the annotation435*/436Iterable<? extends Completion> getCompletions(Element element,437AnnotationMirror annotation,438ExecutableElement member,439String userText);440}441442443