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

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

mercurial