1 /*
   2  * Copyright (c) 2005, 2016, 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 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      * @param a  annotation type being requested
 147      * @return the elements annotated with the given annotation type,
 148      * or an empty set if there are none
 149      * @throws IllegalArgumentException if the argument does not
 150      * represent an annotation type
 151      */
 152     Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a);
 153 
 154     /**
 155      * Returns the elements annotated with one or more of the given
 156      * annotation types.
 157      *
 158      * @apiNote This method may be useful when processing repeating
 159      * annotations by looking for an annotation type and its
 160      * containing annotation type at the same time.
 161      *
 162      * @implSpec The default implementation of this method creates an
 163      * empty result set, iterates over the annotations in the argument
 164      * set calling {@link #getElementsAnnotatedWith(Class)} on
 165      * each annotation and adding those results to the result
 166      * set. Finally, the contents of the result set are returned as an
 167      * unmodifiable set.
 168      *
 169      * @param annotations  annotation types being requested
 170      * @return the elements annotated with one or more of the given
 171      * annotation types, or an empty set if there are none
 172      * @throws IllegalArgumentException if the any elements of the
 173      * argument set do not represent an annotation type
 174      * @jls 9.6.3 Repeatable Annotation Types
 175      * @since 9
 176      */
 177     default Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> annotations){
 178         // Use LinkedHashSet rather than HashSet for predictability
 179         Set<Element> result = new LinkedHashSet<>();
 180         for (Class<? extends Annotation> annotation : annotations) {
 181             result.addAll(getElementsAnnotatedWith(annotation));
 182         }
 183         return Collections.unmodifiableSet(result);
 184     }
 185 }