1 /*
   2  * Copyright (c) 2005, 2018, 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 javax.lang.model.element.Element;
  29 import javax.lang.model.element.TypeElement;
  30 import java.util.LinkedHashSet;
  31 import java.util.Collections;
  32 import java.util.Set;
  33 import java.lang.annotation.Annotation;
  34 
  35 /**
  36  * An annotation processing tool framework will {@linkplain
  37  * Processor#process provide an annotation processor with an object
  38  * implementing this interface} so that the processor can query for
  39  * information about a round of annotation processing.
  40  *
  41  * @author Joseph D. Darcy
  42  * @author Scott Seligman
  43  * @author Peter von der Ahé
  44  * @since 1.6
  45  */
  46 public interface RoundEnvironment {
  47     /**
  48      * Returns {@code true} if types generated by this round will not
  49      * be subject to a subsequent round of annotation processing;
  50      * returns {@code false} otherwise.
  51      *
  52      * @return {@code true} if types generated by this round will not
  53      * be subject to a subsequent round of annotation processing;
  54      * returns {@code false} otherwise
  55      */
  56     boolean processingOver();
  57 
  58     /**
  59      * Returns {@code true} if an error was raised in the prior round
  60      * of processing; returns {@code false} otherwise.
  61      *
  62      * @return {@code true} if an error was raised in the prior round
  63      * of processing; returns {@code false} otherwise
  64      */
  65     boolean errorRaised();
  66 
  67     /**
  68      * Returns the {@linkplain Processor root elements} for annotation processing generated
  69      * by the prior round.
  70      *
  71      * @return the root elements for annotation processing generated
  72      * by the prior round, or an empty set if there were none
  73      */
  74     Set<? extends Element> getRootElements();
  75 
  76     /**
  77      * Returns the elements annotated with the given annotation type.
  78      * The annotation may appear directly or be inherited.  Only
  79      * package elements, module elements, and type elements <i>included</i> in this
  80      * round of annotation processing, or declarations of members,
  81      * constructors, parameters, or type parameters declared within
  82      * those, are returned.  Included type elements are {@linkplain
  83      * #getRootElements root types} and any member types nested within
  84      * them.  Elements of a package are not considered included simply
  85      * because a {@code package-info} file for that package was
  86      * created.
  87      * Likewise, elements of a module are not considered included
  88      * simply because a {@code module-info} file for that module was
  89      * created.
  90      *
  91      * @param a  annotation type being requested
  92      * @return the elements annotated with the given annotation type,
  93      * or an empty set if there are none
  94      * @throws IllegalArgumentException if the argument does not
  95      * represent an annotation type
  96      */
  97     Set<? extends Element> getElementsAnnotatedWith(TypeElement a);
  98 
  99     /**
 100      * Returns the elements annotated with one or more of the given
 101      * annotation types.
 102      *
 103      * @apiNote This method may be useful when processing repeating
 104      * annotations by looking for an annotation type and its
 105      * containing annotation type at the same time.
 106      *
 107      * @implSpec The default implementation of this method creates an
 108      * empty result set, iterates over the annotations in the argument
 109      * array calling {@link #getElementsAnnotatedWith(TypeElement)} on
 110      * each annotation and adding those results to the result
 111      * set. Finally, the contents of the result set are returned as an
 112      * unmodifiable set.
 113      *
 114      * @param annotations  annotation types being requested
 115      * @return the elements annotated with one or more of the given
 116      * annotation types, or an empty set if there are none
 117      * @throws IllegalArgumentException if the any elements of the
 118      * argument set do not represent an annotation type
 119      * @jls 9.6.3 Repeatable Annotation Types
 120      * @since 9
 121      */
 122     default Set<? extends Element> getElementsAnnotatedWithAny(TypeElement... annotations){
 123         // Use LinkedHashSet rather than HashSet for predictability
 124         Set<Element> result = new LinkedHashSet<>();
 125         for (TypeElement annotation : annotations) {
 126             result.addAll(getElementsAnnotatedWith(annotation));
 127         }
 128         return Collections.unmodifiableSet(result);
 129     }
 130 
 131     /**
 132      * Returns the elements annotated with the given annotation type.
 133      * The annotation may appear directly or be inherited.  Only
 134      * package elements, module elements, and type elements <i>included</i> in this
 135      * round of annotation processing, or declarations of members,
 136      * constructors, parameters, or type parameters declared within
 137      * those, are returned.  Included type elements are {@linkplain
 138      * #getRootElements root types} and any member types nested within
 139      * them.  Elements in a package are not considered included simply
 140      * because a {@code package-info} file for that package was
 141      * created.
 142      * Likewise, elements of a module are not considered included
 143      * simply because a {@code module-info} file for that module was
 144      * created.
 145      *
 146      * <p> Note: An implementation of this method typically performs
 147      * an internal conversion from the runtime reflective
 148      * representation of an annotation type as a {@code Class} object
 149      * to a different representation used for annotation
 150      * processing. The set of annotation types present in the runtime
 151      * context may differ from the set of annotation types present in
 152      * the context of annotation processing in a particular
 153      * environmental configuration. If an runtime annotation type is
 154      * not present in the annotation processing context, the situation
 155      * is not treated as an error and no elements are found for that
 156      * annotation type.
 157      *
 158      * @param a  annotation type being requested
 159      * @return the elements annotated with the given annotation type,
 160      * or an empty set if there are none
 161      * @throws IllegalArgumentException if the argument does not
 162      * represent an annotation type
 163      *
 164      * @see AnnotatedConstruct.getAnnotation(Class)
 165      * @see AnnotatedConstruct.getAnnotationsByType(Class)
 166      */
 167     Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a);
 168 
 169     /**
 170      * Returns the elements annotated with one or more of the given
 171      * annotation types.
 172      *
 173      * <p> Note: An implementation of this method typically performs
 174      * an internal conversion from the runtime reflective
 175      * representation of an annotation type as a {@code Class} object
 176      * to a different representation used for annotation
 177      * processing. The set of annotation types present in the runtime
 178      * context may differ from the set of annotation types present in
 179      * the context of annotation processing in a particular
 180      * environmental configuration. If an runtime annotation type is
 181      * not present in the annotation processing context, the situation
 182      * is not treated as an error and no elements are found for that
 183      * annotation type.
 184      *
 185      * @apiNote This method may be useful when processing repeating
 186      * annotations by looking for an annotation type and its
 187      * containing annotation type at the same time.
 188      *
 189      * @implSpec The default implementation of this method creates an
 190      * empty result set, iterates over the annotations in the argument
 191      * set calling {@link #getElementsAnnotatedWith(Class)} on
 192      * each annotation and adding those results to the result
 193      * set. Finally, the contents of the result set are returned as an
 194      * unmodifiable set.
 195      *
 196      * @param annotations  annotation types being requested
 197      * @return the elements annotated with one or more of the given
 198      * annotation types, or an empty set if there are none
 199      * @throws IllegalArgumentException if the any elements of the
 200      * argument set do not represent an annotation type
 201      * @jls 9.6.3 Repeatable Annotation Types
 202      *
 203      * @see AnnotatedConstruct.getAnnotation(Class)
 204      * @see AnnotatedConstruct.getAnnotationsByType(Class)
 205      *
 206      * @since 9
 207      */
 208     default Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> annotations){
 209         // Use LinkedHashSet rather than HashSet for predictability
 210         Set<Element> result = new LinkedHashSet<>();
 211         for (Class<? extends Annotation> annotation : annotations) {
 212             result.addAll(getElementsAnnotatedWith(annotation));
 213         }
 214         return Collections.unmodifiableSet(result);
 215     }
 216 }