Path: blob/master/src/java.compiler/share/classes/javax/annotation/processing/RoundEnvironment.java
41159 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.annotation.processing;2627import javax.lang.model.element.Element;28import javax.lang.model.element.TypeElement;29import java.util.LinkedHashSet;30import java.util.Collections;31import java.util.Set;32import java.lang.annotation.Annotation;3334/**35* An annotation processing tool framework will {@linkplain36* Processor#process provide an annotation processor with an object37* implementing this interface} so that the processor can query for38* information about a round of annotation processing.39*40* @author Joseph D. Darcy41* @author Scott Seligman42* @author Peter von der Ahé43* @since 1.644*/45public interface RoundEnvironment {46/**47* {@return {@code true} if types generated by this round will not48* be subject to a subsequent round of annotation processing;49* returns {@code false} otherwise}50*/51boolean processingOver();5253/**54* {@return {@code true} if an error was raised in the prior round55* of processing; returns {@code false} otherwise}56*/57boolean errorRaised();5859/**60* Returns the {@linkplain Processor root elements} for annotation processing generated61* by the prior round.62*63* @return the root elements for annotation processing generated64* by the prior round, or an empty set if there were none65*/66Set<? extends Element> getRootElements();6768/**69* Returns the elements annotated with the given annotation type.70* The annotation may appear directly or be inherited. Only71* package elements, module elements, and type elements <i>included</i> in this72* round of annotation processing, or declarations of members,73* constructors, parameters, type parameters, or record components74* declared within those, are returned. Included type elements are {@linkplain75* #getRootElements root types} and any member types nested within76* them. Elements of a package are not considered included simply77* because a {@code package-info} file for that package was78* created.79* Likewise, elements of a module are not considered included80* simply because a {@code module-info} file for that module was81* created.82*83* @param a annotation type being requested84* @return the elements annotated with the given annotation type,85* or an empty set if there are none86* @throws IllegalArgumentException if the argument does not87* represent an annotation type88*/89Set<? extends Element> getElementsAnnotatedWith(TypeElement a);9091/**92* Returns the elements annotated with one or more of the given93* annotation types.94*95* @apiNote This method may be useful when processing repeating96* annotations by looking for an annotation type and its97* containing annotation type at the same time.98*99* @implSpec The default implementation of this method creates an100* empty result set, iterates over the annotations in the argument101* array calling {@link #getElementsAnnotatedWith(TypeElement)} on102* each annotation and adding those results to the result103* set. Finally, the contents of the result set are returned as an104* unmodifiable set.105*106* @param annotations annotation types being requested107* @return the elements annotated with one or more of the given108* annotation types, or an empty set if there are none109* @throws IllegalArgumentException if the any elements of the110* argument set do not represent an annotation type111* @jls 9.6.3 Repeatable Annotation Interfaces112* @since 9113*/114default Set<? extends Element> getElementsAnnotatedWithAny(TypeElement... annotations){115// Use LinkedHashSet rather than HashSet for predictability116Set<Element> result = new LinkedHashSet<>();117for (TypeElement annotation : annotations) {118result.addAll(getElementsAnnotatedWith(annotation));119}120return Collections.unmodifiableSet(result);121}122123/**124* Returns the elements annotated with the given annotation type.125* The annotation may appear directly or be inherited. Only126* package elements, module elements, and type elements <i>included</i> in this127* round of annotation processing, or declarations of members,128* constructors, parameters, type parameters, or record components129* declared within those, are returned. Included type elements are {@linkplain130* #getRootElements root types} and any member types nested within131* them. Elements in a package are not considered included simply132* because a {@code package-info} file for that package was133* created.134* Likewise, elements of a module are not considered included135* simply because a {@code module-info} file for that module was136* created.137*138* <p> Note: An implementation of this method typically performs139* an internal conversion from the runtime reflective140* representation of an annotation type as a {@code Class} object141* to a different representation used for annotation142* processing. The set of annotation types present in the runtime143* context may differ from the set of annotation types present in144* the context of annotation processing in a particular145* environmental configuration. If an runtime annotation type is146* not present in the annotation processing context, the situation147* is not treated as an error and no elements are found for that148* annotation type.149*150* @param a annotation type being requested151* @return the elements annotated with the given annotation type,152* or an empty set if there are none153* @throws IllegalArgumentException if the argument does not154* represent an annotation type155*156* @see javax.lang.model.AnnotatedConstruct#getAnnotation(Class)157* @see javax.lang.model.AnnotatedConstruct#getAnnotationsByType(Class)158*/159Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a);160161/**162* Returns the elements annotated with one or more of the given163* annotation types.164*165* <p> Note: An implementation of this method typically performs166* an internal conversion from the runtime reflective167* representation of an annotation type as a {@code Class} object168* to a different representation used for annotation169* processing. The set of annotation types present in the runtime170* context may differ from the set of annotation types present in171* the context of annotation processing in a particular172* environmental configuration. If an runtime annotation type is173* not present in the annotation processing context, the situation174* is not treated as an error and no elements are found for that175* annotation type.176*177* @apiNote This method may be useful when processing repeating178* annotations by looking for an annotation type and its179* containing annotation type at the same time.180*181* @implSpec The default implementation of this method creates an182* empty result set, iterates over the annotations in the argument183* set calling {@link #getElementsAnnotatedWith(Class)} on184* each annotation and adding those results to the result185* set. Finally, the contents of the result set are returned as an186* unmodifiable set.187*188* @param annotations annotation types being requested189* @return the elements annotated with one or more of the given190* annotation types, or an empty set if there are none191* @throws IllegalArgumentException if the any elements of the192* argument set do not represent an annotation type193* @jls 9.6.3 Repeatable Annotation Interfaces194*195* @see javax.lang.model.AnnotatedConstruct#getAnnotation(Class)196* @see javax.lang.model.AnnotatedConstruct#getAnnotationsByType(Class)197*198* @since 9199*/200default Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> annotations){201// Use LinkedHashSet rather than HashSet for predictability202Set<Element> result = new LinkedHashSet<>();203for (Class<? extends Annotation> annotation : annotations) {204result.addAll(getElementsAnnotatedWith(annotation));205}206return Collections.unmodifiableSet(result);207}208}209210211