author | darcy |
Wed, 25 May 2016 22:32:16 -0700 | |
changeset 38606 | 6b708cd90241 |
parent 31751 | ec251536a004 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
2 |
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.processing; |
|
27 |
||
28 |
import java.lang.annotation.Annotation; |
|
29 |
import javax.annotation.processing.*; |
|
30 |
import javax.lang.model.element.*; |
|
31 |
import javax.lang.model.util.*; |
|
32 |
import java.util.*; |
|
33 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
34 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
35 |
import com.sun.tools.javac.util.DefinedBy.Api; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
36 |
|
10 | 37 |
/** |
38 |
* Object providing state about a prior round of annotation processing. |
|
39 |
* |
|
3151
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
40 |
* <p>The methods in this class do not take type annotations into account, |
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
41 |
* as target types, not java elements. |
41800a86aad3
6854796: update JSR308 impl with latest code from type-annotations repo
jjg
parents:
3149
diff
changeset
|
42 |
* |
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5841
diff
changeset
|
43 |
* <p><b>This is NOT part of any supported API. |
10 | 44 |
* If you write code that depends on this, you do so at your own risk. |
45 |
* This code and its internal interfaces are subject to change or |
|
46 |
* deletion without notice.</b> |
|
47 |
*/ |
|
48 |
public class JavacRoundEnvironment implements RoundEnvironment { |
|
49 |
// Default equals and hashCode methods are okay. |
|
50 |
||
51 |
private final boolean processingOver; |
|
52 |
private final boolean errorRaised; |
|
53 |
private final ProcessingEnvironment processingEnv; |
|
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
54 |
private final Elements eltUtils; |
10 | 55 |
|
56 |
// Caller must pass in an immutable set |
|
57 |
private final Set<? extends Element> rootElements; |
|
58 |
||
59 |
JavacRoundEnvironment(boolean processingOver, |
|
60 |
boolean errorRaised, |
|
61 |
Set<? extends Element> rootElements, |
|
62 |
ProcessingEnvironment processingEnv) { |
|
63 |
this.processingOver = processingOver; |
|
64 |
this.errorRaised = errorRaised; |
|
65 |
this.rootElements = rootElements; |
|
66 |
this.processingEnv = processingEnv; |
|
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
67 |
this.eltUtils = processingEnv.getElementUtils(); |
10 | 68 |
} |
69 |
||
70 |
public String toString() { |
|
71 |
return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]", |
|
72 |
errorRaised, |
|
73 |
rootElements, |
|
74 |
processingOver); |
|
75 |
} |
|
76 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
77 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 78 |
public boolean processingOver() { |
79 |
return processingOver; |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Returns {@code true} if an error was raised in the prior round |
|
84 |
* of processing; returns {@code false} otherwise. |
|
85 |
* |
|
86 |
* @return {@code true} if an error was raised in the prior round |
|
87 |
* of processing; returns {@code false} otherwise. |
|
88 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
89 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 90 |
public boolean errorRaised() { |
91 |
return errorRaised; |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Returns the type elements specified by the prior round. |
|
96 |
* |
|
97 |
* @return the types elements specified by the prior round, or an |
|
98 |
* empty set if there were none |
|
99 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
100 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 101 |
public Set<? extends Element> getRootElements() { |
102 |
return rootElements; |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Returns the elements annotated with the given annotation type. |
|
107 |
* Only type elements <i>included</i> in this round of annotation |
|
108 |
* processing, or declarations of members, parameters, or type |
|
109 |
* parameters declared within those, are returned. Included type |
|
14259 | 110 |
* elements are {@linkplain #getRootElements specified |
10 | 111 |
* types} and any types nested within them. |
112 |
* |
|
113 |
* @param a annotation type being requested |
|
114 |
* @return the elements annotated with the given annotation type, |
|
115 |
* or an empty set if there are none |
|
116 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
117 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 118 |
public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { |
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
119 |
throwIfNotAnnotation(a); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
120 |
|
10 | 121 |
Set<Element> result = Collections.emptySet(); |
25690
b1dac768ab79
8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9
darcy
parents:
23795
diff
changeset
|
122 |
ElementScanner9<Set<Element>, TypeElement> scanner = |
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
123 |
new AnnotationSetScanner(result); |
10 | 124 |
|
125 |
for (Element element : rootElements) |
|
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
126 |
result = scanner.scan(element, a); |
10 | 127 |
|
128 |
return result; |
|
129 |
} |
|
130 |
||
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
131 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
132 |
public Set<? extends Element> getElementsAnnotatedWithAny(TypeElement... annotations) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
133 |
// Don't bother to special-case annotations.length == 1 as |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
134 |
// return getElementsAnnotatedWith(annotations[0]); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
135 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
136 |
Set<TypeElement> annotationSet = new LinkedHashSet<>(annotations.length); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
137 |
for (TypeElement annotation : annotations) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
138 |
throwIfNotAnnotation(annotation); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
139 |
annotationSet.add(annotation); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
140 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
141 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
142 |
Set<Element> result = Collections.emptySet(); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
143 |
ElementScanner9<Set<Element>, Set<TypeElement>> scanner = |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
144 |
new AnnotationSetMultiScanner(result); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
145 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
146 |
for (Element element : rootElements) |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
147 |
result = scanner.scan(element, annotationSet); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
148 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
149 |
return result; |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
150 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
151 |
|
10 | 152 |
// Could be written as a local class inside getElementsAnnotatedWith |
153 |
private class AnnotationSetScanner extends |
|
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
154 |
ElementScanningIncludingTypeParameters<Set<Element>, TypeElement> { |
10 | 155 |
// Insertion-order preserving set |
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
156 |
private Set<Element> annotatedElements = new LinkedHashSet<>(); |
10 | 157 |
|
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
158 |
AnnotationSetScanner(Set<Element> defaultSet) { |
10 | 159 |
super(defaultSet); |
160 |
} |
|
161 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
162 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
163 |
public Set<Element> scan(Element e, TypeElement annotation) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
164 |
for (AnnotationMirror annotMirror : eltUtils.getAllAnnotationMirrors(e)) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
165 |
if (annotation.equals(mirrorAsElement(annotMirror))) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
166 |
annotatedElements.add(e); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
167 |
break; |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
168 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
169 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
170 |
e.accept(this, annotation); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
171 |
return annotatedElements; |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
172 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
173 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
174 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
175 |
// Could be written as a local class inside getElementsAnnotatedWithAny |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
176 |
private class AnnotationSetMultiScanner extends |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
177 |
ElementScanningIncludingTypeParameters<Set<Element>, Set<TypeElement>> { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
178 |
// Insertion-order preserving set |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
179 |
private Set<Element> annotatedElements = new LinkedHashSet<>(); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
180 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
181 |
AnnotationSetMultiScanner(Set<Element> defaultSet) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
182 |
super(defaultSet); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
183 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
184 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
185 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
186 |
public Set<Element> scan(Element e, Set<TypeElement> annotations) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
187 |
for (AnnotationMirror annotMirror : eltUtils.getAllAnnotationMirrors(e)) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
188 |
if (annotations.contains(mirrorAsElement(annotMirror))) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
189 |
annotatedElements.add(e); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
190 |
break; |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
191 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
192 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
193 |
e.accept(this, annotations); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
194 |
return annotatedElements; |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
195 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
196 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
197 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
198 |
private static abstract class ElementScanningIncludingTypeParameters<R, P> |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
199 |
extends ElementScanner9<R, P> { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
200 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
201 |
protected ElementScanningIncludingTypeParameters(R defaultValue) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
202 |
super(defaultValue); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
203 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
204 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
205 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
206 |
public R visitType(TypeElement e, P p) { |
18669
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
207 |
// Type parameters are not considered to be enclosed by a type |
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
208 |
scan(e.getTypeParameters(), p); |
23795
62509b72088e
8038080: annotation processors don't visit declaration parameter annotations
jfranck
parents:
22449
diff
changeset
|
209 |
return super.visitType(e, p); |
18669
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
210 |
} |
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
211 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
212 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
213 |
public R visitExecutable(ExecutableElement e, P p) { |
18669
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
214 |
// Type parameters are not considered to be enclosed by an executable |
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
215 |
scan(e.getTypeParameters(), p); |
23795
62509b72088e
8038080: annotation processors don't visit declaration parameter annotations
jfranck
parents:
22449
diff
changeset
|
216 |
return super.visitExecutable(e, p); |
18669
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
217 |
} |
10 | 218 |
} |
219 |
||
220 |
/** |
|
31751 | 221 |
* {@inheritDoc} |
10 | 222 |
*/ |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
223 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 224 |
public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) { |
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
225 |
throwIfNotAnnotation(a); |
10 | 226 |
String name = a.getCanonicalName(); |
227 |
if (name == null) |
|
228 |
return Collections.emptySet(); |
|
229 |
else { |
|
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
230 |
TypeElement annotationType = eltUtils.getTypeElement(name); |
10 | 231 |
if (annotationType == null) |
232 |
return Collections.emptySet(); |
|
233 |
else |
|
234 |
return getElementsAnnotatedWith(annotationType); |
|
235 |
} |
|
236 |
} |
|
38606
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
237 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
238 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
239 |
public Set<? extends Element> getElementsAnnotatedWithAny(Set<Class<? extends Annotation>> annotations) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
240 |
List<TypeElement> annotationsAsElements = new ArrayList<>(annotations.size()); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
241 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
242 |
for (Class<? extends Annotation> annotation : annotations) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
243 |
throwIfNotAnnotation(annotation); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
244 |
String name = annotation.getCanonicalName(); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
245 |
if (name == null) |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
246 |
continue; |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
247 |
annotationsAsElements.add(eltUtils.getTypeElement(name)); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
248 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
249 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
250 |
return getElementsAnnotatedWithAny(annotationsAsElements.toArray(new TypeElement[0])); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
251 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
252 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
253 |
private Element mirrorAsElement(AnnotationMirror annotationMirror) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
254 |
return annotationMirror.getAnnotationType().asElement(); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
255 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
256 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
257 |
private static final String NOT_AN_ANNOTATION_TYPE = |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
258 |
"The argument does not represent an annotation type: "; |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
259 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
260 |
private void throwIfNotAnnotation(Class<? extends Annotation> a) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
261 |
if (!a.isAnnotation()) |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
262 |
throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
263 |
} |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
264 |
|
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
265 |
private void throwIfNotAnnotation(TypeElement a) { |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
266 |
if (a.getKind() != ElementKind.ANNOTATION_TYPE) |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
267 |
throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); |
6b708cd90241
8032230: Enhance javax.a.p.RoundEnvironment after repeating annotations
darcy
parents:
31751
diff
changeset
|
268 |
} |
10 | 269 |
} |