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

Tue, 09 Oct 2012 19:10:00 -0700

author
jjg
date
Tue, 09 Oct 2012 19:10:00 -0700
changeset 1357
c75be5bc5283
parent 972
694ff82ca68e
child 1876
1908e86ee49a
permissions
-rw-r--r--

8000663: clean up langtools imports
Reviewed-by: darcy

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

mercurial