src/share/classes/javax/annotation/processing/Processor.java

Thu, 17 Oct 2013 19:10:19 -0700

author
darcy
date
Thu, 17 Oct 2013 19:10:19 -0700
changeset 2145
7af634b1fc5b
parent 1876
1908e86ee49a
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8026838: Fix new doclint issues in javax.annotation.processing
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2005, 2013, 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  */
    26 package javax.annotation.processing;
    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;
    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 {@code 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 types of the annotations <em>{@linkplain AnnotatedConstruct present}</em>
    94  * on the {@linkplain
    95  * RoundEnvironment#getRootElements root elements}, what {@linkplain
    96  * #getSupportedAnnotationTypes annotation types a processor
    97  * supports}, and whether or not a processor {@linkplain #process
    98  * claims the annotation types it processes}.  A processor will be asked to
    99  * process a subset of the annotation types it supports, possibly an
   100  * empty set.
   101  *
   102  * For a given round, the tool computes the set of annotation types
   103  * that are present on the elements enclosed within the root elements.
   104  * If there is at least one annotation type present, then as
   105  * processors claim annotation types, they are removed from the set of
   106  * unmatched annotation types.  When the set is empty or no more
   107  * processors are available, the round has run to completion.  If
   108  * there are no annotation types present, annotation processing still
   109  * occurs but only <i>universal processors</i> which support
   110  * processing all annotation types, {@code "*"}, can claim the (empty)
   111  * set of annotation types.
   112  *
   113  * <p>An annotation type is considered present if there is at least
   114  * one annotation of that type 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}. Annotations on {@linkplain
   119  * java.lang.annotation.ElementType#TYPE_USE type uses}, as opposed to
   120  * annotations on elements, are ignored when computing whether or not
   121  * an annotation type is present.
   122  *
   123  * <p>An annotation is present if it meets the definition of being
   124  * present given in {@link AnnotatedConstruct}. In brief, an
   125  * annotation is considered present for the purposes of discovery if
   126  * it is directly present or present via inheritance. An annotation is
   127  * <em>not</em> considered present by virtue of being wrapped by a
   128  * container annotation. Operationally, this is equivalent to an
   129  * annotation being present on an element if and only if it would be
   130  * included in the results of {@link
   131  * Elements#getAllAnnotationMirrors(Element)} called on that element. Since
   132  * annotations inside container annotations are not considered
   133  * present, to properly process {@linkplain
   134  * java.lang.annotation.Repeatable repeatable annotation types},
   135  * processors are advised to include both the repeatable annotation
   136  * type and its containing annotation type in the set of {@linkplain
   137  * #getSupportedAnnotationTypes() supported annotation types} of a
   138  * processor.
   139  *
   140  * <p>Note that if a processor supports {@code "*"} and returns {@code
   141  * true}, all annotations are claimed.  Therefore, a universal
   142  * processor being used to, for example, implement additional validity
   143  * checks should return {@code false} so as to not prevent other such
   144  * checkers from being able to run.
   145  *
   146  * <p>If a processor throws an uncaught exception, the tool may cease
   147  * other active annotation processors.  If a processor raises an
   148  * error, the current round will run to completion and the subsequent
   149  * round will indicate an {@linkplain RoundEnvironment#errorRaised
   150  * error was raised}.  Since annotation processors are run in a
   151  * cooperative environment, a processor should throw an uncaught
   152  * exception only in situations where no error recovery or reporting
   153  * is feasible.
   154  *
   155  * <p>The tool environment is not required to support annotation
   156  * processors that access environmental resources, either {@linkplain
   157  * RoundEnvironment per round} or {@linkplain ProcessingEnvironment
   158  * cross-round}, in a multi-threaded fashion.
   159  *
   160  * <p>If the methods that return configuration information about the
   161  * annotation processor return {@code null}, return other invalid
   162  * input, or throw an exception, the tool infrastructure must treat
   163  * this as an error condition.
   164  *
   165  * <p>To be robust when running in different tool implementations, an
   166  * annotation processor should have the following properties:
   167  *
   168  * <ol>
   169  *
   170  * <li>The result of processing a given input is not a function of the presence or absence
   171  * of other inputs (orthogonality).
   172  *
   173  * <li>Processing the same input produces the same output (consistency).
   174  *
   175  * <li>Processing input <i>A</i> followed by processing input <i>B</i>
   176  * is equivalent to processing <i>B</i> then <i>A</i>
   177  * (commutativity)
   178  *
   179  * <li>Processing an input does not rely on the presence of the output
   180  * of other annotation processors (independence)
   181  *
   182  * </ol>
   183  *
   184  * <p>The {@link Filer} interface discusses restrictions on how
   185  * processors can operate on files.
   186  *
   187  * <p>Note that implementors of this interface may find it convenient
   188  * to extend {@link AbstractProcessor} rather than implementing this
   189  * interface directly.
   190  *
   191  * @author Joseph D. Darcy
   192  * @author Scott Seligman
   193  * @author Peter von der Ah&eacute;
   194  * @since 1.6
   195  */
   196 public interface Processor {
   197     /**
   198      * Returns the options recognized by this processor.  An
   199      * implementation of the processing tool must provide a way to
   200      * pass processor-specific options distinctly from options passed
   201      * to the tool itself, see {@link ProcessingEnvironment#getOptions
   202      * getOptions}.
   203      *
   204      * <p>Each string returned in the set must be a period separated
   205      * sequence of {@linkplain
   206      * javax.lang.model.SourceVersion#isIdentifier identifiers}:
   207      *
   208      * <blockquote>
   209      * <dl>
   210      * <dt><i>SupportedOptionString:</i>
   211      * <dd><i>Identifiers</i>
   212      *
   213      * <dt><i>Identifiers:</i>
   214      * <dd> <i>Identifier</i>
   215      * <dd> <i>Identifier</i> {@code .} <i>Identifiers</i>
   216      *
   217      * <dt><i>Identifier:</i>
   218      * <dd>Syntactic identifier, including keywords and literals
   219      * </dl>
   220      * </blockquote>
   221      *
   222      * <p> A tool might use this information to determine if any
   223      * options provided by a user are unrecognized by any processor,
   224      * in which case it may wish to report a warning.
   225      *
   226      * @return the options recognized by this processor or an
   227      *         empty collection if none
   228      * @see javax.annotation.processing.SupportedOptions
   229      */
   230     Set<String> getSupportedOptions();
   232     /**
   233      * Returns the names of the annotation types supported by this
   234      * processor.  An element of the result may be the canonical
   235      * (fully qualified) name of a supported annotation type.
   236      * Alternately it may be of the form &quot;<tt><i>name</i>.*</tt>&quot;
   237      * representing the set of all annotation types with canonical
   238      * names beginning with &quot;<tt><i>name.</i></tt>&quot;.  Finally, {@code
   239      * "*"} by itself represents the set of all annotation types,
   240      * including the empty set.  Note that a processor should not
   241      * claim {@code "*"} unless it is actually processing all files;
   242      * claiming unnecessary annotations may cause a performance
   243      * slowdown in some environments.
   244      *
   245      * <p>Each string returned in the set must be accepted by the
   246      * following grammar:
   247      *
   248      * <blockquote>
   249      * <dl>
   250      * <dt><i>SupportedAnnotationTypeString:</i>
   251      * <dd><i>TypeName</i> <i>DotStar</i><sub><i>opt</i></sub>
   252      * <dd><tt>*</tt>
   253      *
   254      * <dt><i>DotStar:</i>
   255      * <dd><tt>.</tt> <tt>*</tt>
   256      * </dl>
   257      * </blockquote>
   258      *
   259      * where <i>TypeName</i> is as defined in
   260      * <cite>The Java&trade; Language Specification</cite>.
   261      *
   262      * @return the names of the annotation types supported by this processor
   263      * @see javax.annotation.processing.SupportedAnnotationTypes
   264      * @jls 3.8 Identifiers
   265      * @jls 6.5.5 Meaning of Type Names
   266      */
   267     Set<String> getSupportedAnnotationTypes();
   269     /**
   270      * Returns the latest source version supported by this annotation
   271      * processor.
   272      *
   273      * @return the latest source version supported by this annotation
   274      * processor.
   275      * @see javax.annotation.processing.SupportedSourceVersion
   276      * @see ProcessingEnvironment#getSourceVersion
   277      */
   278     SourceVersion getSupportedSourceVersion();
   280     /**
   281      * Initializes the processor with the processing environment.
   282      *
   283      * @param processingEnv environment for facilities the tool framework
   284      * provides to the processor
   285      */
   286     void init(ProcessingEnvironment processingEnv);
   288     /**
   289      * Processes a set of annotation types on type elements
   290      * originating from the prior round and returns whether or not
   291      * these annotation types are claimed by this processor.  If {@code
   292      * true} is returned, the annotation types are claimed and subsequent
   293      * processors will not be asked to process them; if {@code false}
   294      * is returned, the annotation types are unclaimed and subsequent
   295      * processors may be asked to process them.  A processor may
   296      * always return the same boolean value or may vary the result
   297      * based on chosen criteria.
   298      *
   299      * <p>The input set will be empty if the processor supports {@code
   300      * "*"} and the root elements have no annotations.  A {@code
   301      * Processor} must gracefully handle an empty set of annotations.
   302      *
   303      * @param annotations the annotation types requested to be processed
   304      * @param roundEnv  environment for information about the current and prior round
   305      * @return whether or not the set of annotation types are claimed by this processor
   306      */
   307     boolean process(Set<? extends TypeElement> annotations,
   308                     RoundEnvironment roundEnv);
   310    /**
   311     * Returns to the tool infrastructure an iterable of suggested
   312     * completions to an annotation.  Since completions are being asked
   313     * for, the information provided about the annotation may be
   314     * incomplete, as if for a source code fragment. A processor may
   315     * return an empty iterable.  Annotation processors should focus
   316     * their efforts on providing completions for annotation members
   317     * with additional validity constraints known to the processor, for
   318     * example an {@code int} member whose value should lie between 1
   319     * and 10 or a string member that should be recognized by a known
   320     * grammar, such as a regular expression or a URL.
   321     *
   322     * <p>Since incomplete programs are being modeled, some of the
   323     * parameters may only have partial information or may be {@code
   324     * null}.  At least one of {@code element} and {@code userText}
   325     * must be non-{@code null}.  If {@code element} is non-{@code
   326     * null}, {@code annotation} and {@code member} may be {@code
   327     * null}.  Processors may not throw a {@code NullPointerException}
   328     * if some parameters are {@code null}; if a processor has no
   329     * completions to offer based on the provided information, an
   330     * empty iterable can be returned.  The processor may also return
   331     * a single completion with an empty value string and a message
   332     * describing why there are no completions.
   333     *
   334     * <p>Completions are informative and may reflect additional
   335     * validity checks performed by annotation processors.  For
   336     * example, consider the simple annotation:
   337     *
   338     * <blockquote>
   339     * <pre>
   340     * &#064;MersennePrime {
   341     *    int value();
   342     * }
   343     * </pre>
   344     * </blockquote>
   345     *
   346     * (A Mersenne prime is prime number of the form
   347     * 2<sup><i>n</i></sup> - 1.) Given an {@code AnnotationMirror}
   348     * for this annotation type, a list of all such primes in the
   349     * {@code int} range could be returned without examining any other
   350     * arguments to {@code getCompletions}:
   351     *
   352     * <blockquote>
   353     * <pre>
   354     * import static javax.annotation.processing.Completions.*;
   355     * ...
   356     * return Arrays.asList({@link Completions#of(String) of}(&quot;3&quot;),
   357     *                      of(&quot;7&quot;),
   358     *                      of(&quot;31&quot;),
   359     *                      of(&quot;127&quot;),
   360     *                      of(&quot;8191&quot;),
   361     *                      of(&quot;131071&quot;),
   362     *                      of(&quot;524287&quot;),
   363     *                      of(&quot;2147483647&quot;));
   364     * </pre>
   365     * </blockquote>
   366     *
   367     * A more informative set of completions would include the number
   368     * of each prime:
   369     *
   370     * <blockquote>
   371     * <pre>
   372     * return Arrays.asList({@link Completions#of(String, String) of}(&quot;3&quot;,          &quot;M2&quot;),
   373     *                      of(&quot;7&quot;,          &quot;M3&quot;),
   374     *                      of(&quot;31&quot;,         &quot;M5&quot;),
   375     *                      of(&quot;127&quot;,        &quot;M7&quot;),
   376     *                      of(&quot;8191&quot;,       &quot;M13&quot;),
   377     *                      of(&quot;131071&quot;,     &quot;M17&quot;),
   378     *                      of(&quot;524287&quot;,     &quot;M19&quot;),
   379     *                      of(&quot;2147483647&quot;, &quot;M31&quot;));
   380     * </pre>
   381     * </blockquote>
   382     *
   383     * However, if the {@code userText} is available, it can be checked
   384     * to see if only a subset of the Mersenne primes are valid.  For
   385     * example, if the user has typed
   386     *
   387     * <blockquote>
   388     * <code>
   389     * &#064;MersennePrime(1
   390     * </code>
   391     * </blockquote>
   392     *
   393     * the value of {@code userText} will be {@code "1"}; and only
   394     * two of the primes are possible completions:
   395     *
   396     * <blockquote>
   397     * <pre>
   398     * return Arrays.asList(of(&quot;127&quot;,        &quot;M7&quot;),
   399     *                      of(&quot;131071&quot;,     &quot;M17&quot;));
   400     * </pre>
   401     * </blockquote>
   402     *
   403     * Sometimes no valid completion is possible.  For example, there
   404     * is no in-range Mersenne prime starting with 9:
   405     *
   406     * <blockquote>
   407     * <code>
   408     * &#064;MersennePrime(9
   409     * </code>
   410     * </blockquote>
   411     *
   412     * An appropriate response in this case is to either return an
   413     * empty list of completions,
   414     *
   415     * <blockquote>
   416     * <pre>
   417     * return Collections.emptyList();
   418     * </pre>
   419     * </blockquote>
   420     *
   421     * or a single empty completion with a helpful message
   422     *
   423     * <blockquote>
   424     * <pre>
   425     * return Arrays.asList(of(&quot;&quot;, &quot;No in-range Mersenne primes start with 9&quot;));
   426     * </pre>
   427     * </blockquote>
   428     *
   429     * @param element the element being annotated
   430     * @param annotation the (perhaps partial) annotation being
   431     *                   applied to the element
   432     * @param member the annotation member to return possible completions for
   433     * @param userText source code text to be completed
   434     *
   435     * @return suggested completions to the annotation
   436     */
   437     Iterable<? extends Completion> getCompletions(Element element,
   438                                                   AnnotationMirror annotation,
   439                                                   ExecutableElement member,
   440                                                   String userText);
   441 }

mercurial