author | vromero |
Wed, 10 Feb 2016 15:11:40 -0800 | |
changeset 35810 | 9ee6e90d679c |
parent 31751 | ec251536a004 |
child 38606 | 6b708cd90241 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, 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; |
|
54 |
||
55 |
// Caller must pass in an immutable set |
|
56 |
private final Set<? extends Element> rootElements; |
|
57 |
||
58 |
JavacRoundEnvironment(boolean processingOver, |
|
59 |
boolean errorRaised, |
|
60 |
Set<? extends Element> rootElements, |
|
61 |
ProcessingEnvironment processingEnv) { |
|
62 |
this.processingOver = processingOver; |
|
63 |
this.errorRaised = errorRaised; |
|
64 |
this.rootElements = rootElements; |
|
65 |
this.processingEnv = processingEnv; |
|
66 |
} |
|
67 |
||
68 |
public String toString() { |
|
69 |
return String.format("[errorRaised=%b, rootElements=%s, processingOver=%b]", |
|
70 |
errorRaised, |
|
71 |
rootElements, |
|
72 |
processingOver); |
|
73 |
} |
|
74 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
75 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 76 |
public boolean processingOver() { |
77 |
return processingOver; |
|
78 |
} |
|
79 |
||
80 |
/** |
|
81 |
* Returns {@code true} if an error was raised in the prior round |
|
82 |
* of processing; returns {@code false} otherwise. |
|
83 |
* |
|
84 |
* @return {@code true} if an error was raised in the prior round |
|
85 |
* of processing; returns {@code false} otherwise. |
|
86 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
87 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 88 |
public boolean errorRaised() { |
89 |
return errorRaised; |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Returns the type elements specified by the prior round. |
|
94 |
* |
|
95 |
* @return the types elements specified by the prior round, or an |
|
96 |
* empty set if there were none |
|
97 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
98 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 99 |
public Set<? extends Element> getRootElements() { |
100 |
return rootElements; |
|
101 |
} |
|
102 |
||
103 |
private static final String NOT_AN_ANNOTATION_TYPE = |
|
104 |
"The argument does not represent an annotation type: "; |
|
105 |
||
106 |
/** |
|
107 |
* Returns the elements annotated with the given annotation type. |
|
108 |
* Only type elements <i>included</i> in this round of annotation |
|
109 |
* processing, or declarations of members, parameters, or type |
|
110 |
* parameters declared within those, are returned. Included type |
|
14259 | 111 |
* elements are {@linkplain #getRootElements specified |
10 | 112 |
* types} and any types nested within them. |
113 |
* |
|
114 |
* @param a annotation type being requested |
|
115 |
* @return the elements annotated with the given annotation type, |
|
116 |
* or an empty set if there are none |
|
117 |
*/ |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
118 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 119 |
public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { |
120 |
Set<Element> result = Collections.emptySet(); |
|
121 |
if (a.getKind() != ElementKind.ANNOTATION_TYPE) |
|
122 |
throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); |
|
123 |
||
25690
b1dac768ab79
8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9
darcy
parents:
23795
diff
changeset
|
124 |
ElementScanner9<Set<Element>, TypeElement> scanner = |
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
125 |
new AnnotationSetScanner(result); |
10 | 126 |
|
127 |
for (Element element : rootElements) |
|
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
128 |
result = scanner.scan(element, a); |
10 | 129 |
|
130 |
return result; |
|
131 |
} |
|
132 |
||
133 |
// Could be written as a local class inside getElementsAnnotatedWith |
|
134 |
private class AnnotationSetScanner extends |
|
25690
b1dac768ab79
8050430: Provided new utility visitors supporting SourceVersion.RELEASE_9
darcy
parents:
23795
diff
changeset
|
135 |
ElementScanner9<Set<Element>, TypeElement> { |
10 | 136 |
// Insertion-order preserving set |
22163 | 137 |
Set<Element> annotatedElements = new LinkedHashSet<>(); |
10 | 138 |
|
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
139 |
AnnotationSetScanner(Set<Element> defaultSet) { |
10 | 140 |
super(defaultSet); |
141 |
} |
|
142 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
143 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
144 |
public Set<Element> visitType(TypeElement e, TypeElement p) { |
18669
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
145 |
// 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
|
146 |
scan(e.getTypeParameters(), p); |
23795
62509b72088e
8038080: annotation processors don't visit declaration parameter annotations
jfranck
parents:
22449
diff
changeset
|
147 |
return super.visitType(e, p); |
18669
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
148 |
} |
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
149 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
150 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
151 |
public Set<Element> visitExecutable(ExecutableElement e, TypeElement p) { |
18669
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
152 |
// 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
|
153 |
scan(e.getTypeParameters(), p); |
23795
62509b72088e
8038080: annotation processors don't visit declaration parameter annotations
jfranck
parents:
22449
diff
changeset
|
154 |
return super.visitExecutable(e, p); |
18669
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
155 |
} |
99572d59c916
7162089: Add support for repeating annotations to javax.annotation.processing
darcy
parents:
14259
diff
changeset
|
156 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
157 |
@Override @DefinedBy(Api.LANGUAGE_MODEL) |
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
158 |
public Set<Element> scan(Element e, TypeElement p) { |
10 | 159 |
java.util.List<? extends AnnotationMirror> annotationMirrors = |
160 |
processingEnv.getElementUtils().getAllAnnotationMirrors(e); |
|
161 |
for (AnnotationMirror annotationMirror : annotationMirrors) { |
|
22441
05b907a2f359
8030049: RoundEnvironment.getElementsAnnotatedWith receives wrong elements
jlahoda
parents:
22163
diff
changeset
|
162 |
if (p.equals(annotationMirror.getAnnotationType().asElement())) |
10 | 163 |
annotatedElements.add(e); |
164 |
} |
|
165 |
e.accept(this, p); |
|
166 |
return annotatedElements; |
|
167 |
} |
|
168 |
} |
|
169 |
||
170 |
/** |
|
31751 | 171 |
* {@inheritDoc} |
10 | 172 |
*/ |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
173 |
@DefinedBy(Api.ANNOTATION_PROCESSING) |
10 | 174 |
public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) { |
175 |
if (!a.isAnnotation()) |
|
176 |
throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); |
|
177 |
String name = a.getCanonicalName(); |
|
178 |
if (name == null) |
|
179 |
return Collections.emptySet(); |
|
180 |
else { |
|
181 |
TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name); |
|
182 |
if (annotationType == null) |
|
183 |
return Collections.emptySet(); |
|
184 |
else |
|
185 |
return getElementsAnnotatedWith(annotationType); |
|
186 |
} |
|
187 |
} |
|
188 |
} |