RoundEnvironment.java revision 3475:d0c742ddfb01
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
26package javax.annotation.processing;
27
28import javax.lang.model.element.Element;
29import javax.lang.model.element.TypeElement;
30import java.util.LinkedHashSet;
31import java.util.Collections;
32import java.util.Set;
33import 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 */
46public 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 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 in a package are not considered included simply
85     * because a {@code package-info} file for that package was
86     * created.
87     *
88     * @param a  annotation type being requested
89     * @return the elements annotated with the given annotation type,
90     * or an empty set if there are none
91     * @throws IllegalArgumentException if the argument does not
92     * represent an annotation type
93     */
94    Set<? extends Element> getElementsAnnotatedWith(TypeElement a);
95
96    /**
97     * Returns the elements annotated with one or more of the given
98     * annotation types.
99     *
100     * @apiNote This method may be useful when processing repeating
101     * annotations by looking for an annotation type and its
102     * containing annotation type at the same time.
103     *
104     * @implSpec The default implementation of this method creates an
105     * empty result set, iterates over the annotations in the argument
106     * array calling {@link #getElementsAnnotatedWith(TypeElement)} on
107     * each annotation and adding those results to the result
108     * set. Finally, the contents of the result set are returned as an
109     * unmodifiable set.
110     *
111     * @param annotations  annotation types being requested
112     * @return the elements annotated with one or more of the given
113     * annotation types, or an empty set if there are none
114     * @throws IllegalArgumentException if the any elements of the
115     * argument set do not represent an annotation type
116     * @jls 9.6.3 Repeatable Annotation Types
117     * @since 9
118     */
119    default Set<? extends Element> getElementsAnnotatedWithAny(TypeElement... annotations){
120        // Use LinkedHashSet rather than HashSet for predictability
121        Set<Element> result = new LinkedHashSet<>();
122        for (TypeElement annotation : annotations) {
123            result.addAll(getElementsAnnotatedWith(annotation));
124        }
125        return Collections.unmodifiableSet(result);
126    }
127
128    /**
129     * Returns the elements annotated with the given annotation type.
130     * The annotation may appear directly or be inherited.  Only
131     * package elements and type elements <i>included</i> in this
132     * round of annotation processing, or declarations of members,
133     * constructors, parameters, or type parameters declared within
134     * those, are returned.  Included type elements are {@linkplain
135     * #getRootElements root types} and any member types nested within
136     * them.  Elements in a package are not considered included simply
137     * because a {@code package-info} file for that package was
138     * created.
139     *
140     * @param a  annotation type being requested
141     * @return the elements annotated with the given annotation type,
142     * or an empty set if there are none
143     * @throws IllegalArgumentException if the argument does not
144     * represent an annotation type
145     */
146    Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a);
147
148    /**
149     * Returns the elements annotated with one or more of the given
150     * annotation types.
151     *
152     * @apiNote This method may be useful when processing repeating
153     * annotations by looking for an annotation type and its
154     * containing annotation type at the same time.
155     *
156     * @implSpec The default implementation of this method creates an
157     * empty result set, iterates over the annotations in the argument
158     * set calling {@link #getElementsAnnotatedWith(Class)} on
159     * each annotation and adding those results to the result
160     * set. Finally, the contents of the result set are returned as an
161     * unmodifiable set.
162     *
163     * @param annotations  annotation types being requested
164     * @return the elements annotated with one or more of the given
165     * annotation types, or an empty set if there are none
166     * @throws IllegalArgumentException if the any elements of the
167     * argument set do not represent an annotation type
168     * @jls 9.6.3 Repeatable Annotation Types
169     * @since 9
170     */
171    default Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> annotations){
172        // Use LinkedHashSet rather than HashSet for predictability
173        Set<Element> result = new LinkedHashSet<>();
174        for (Class<? extends Annotation> annotation : annotations) {
175            result.addAll(getElementsAnnotatedWith(annotation));
176        }
177        return Collections.unmodifiableSet(result);
178    }
179}
180