130
131 for (Element element : rootElements)
132 result = scanner.scan(element, annotationTypeElement);
133
134 return result;
135 }
136
137 // Could be written as a local class inside getElementsAnnotatedWith
138 private class AnnotationSetScanner extends
139 ElementScanner8<Set<Element>, DeclaredType> {
140 // Insertion-order preserving set
141 Set<Element> annotatedElements = new LinkedHashSet<Element>();
142 Types typeUtil;
143
144 AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) {
145 super(defaultSet);
146 this.typeUtil = typeUtil;
147 }
148
149 @Override
150 public Set<Element> scan(Element e, DeclaredType p) {
151 java.util.List<? extends AnnotationMirror> annotationMirrors =
152 processingEnv.getElementUtils().getAllAnnotationMirrors(e);
153 for (AnnotationMirror annotationMirror : annotationMirrors) {
154 if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p))
155 annotatedElements.add(e);
156 }
157 e.accept(this, p);
158 return annotatedElements;
159 }
160
161 }
162
163 /**
164 * {@inheritdoc}
165 */
166 public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
167 if (!a.isAnnotation())
168 throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
169 String name = a.getCanonicalName();
170 if (name == null)
171 return Collections.emptySet();
172 else {
173 TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name);
174 if (annotationType == null)
175 return Collections.emptySet();
176 else
177 return getElementsAnnotatedWith(annotationType);
178 }
179 }
180 }
|
130
131 for (Element element : rootElements)
132 result = scanner.scan(element, annotationTypeElement);
133
134 return result;
135 }
136
137 // Could be written as a local class inside getElementsAnnotatedWith
138 private class AnnotationSetScanner extends
139 ElementScanner8<Set<Element>, DeclaredType> {
140 // Insertion-order preserving set
141 Set<Element> annotatedElements = new LinkedHashSet<Element>();
142 Types typeUtil;
143
144 AnnotationSetScanner(Set<Element> defaultSet, Types typeUtil) {
145 super(defaultSet);
146 this.typeUtil = typeUtil;
147 }
148
149 @Override
150 public Set<Element> visitType(TypeElement e, DeclaredType p) {
151 // Type parameters are not considered to be enclosed by a type
152 scan(e.getTypeParameters(), p);
153 return scan(e.getEnclosedElements(), p);
154 }
155
156 @Override
157 public Set<Element> visitExecutable(ExecutableElement e, DeclaredType p) {
158 // Type parameters are not considered to be enclosed by an executable
159 scan(e.getTypeParameters(), p);
160 return scan(e.getEnclosedElements(), p);
161 }
162
163 @Override
164 public Set<Element> scan(Element e, DeclaredType p) {
165 java.util.List<? extends AnnotationMirror> annotationMirrors =
166 processingEnv.getElementUtils().getAllAnnotationMirrors(e);
167 for (AnnotationMirror annotationMirror : annotationMirrors) {
168 if (typeUtil.isSameType(annotationMirror.getAnnotationType(), p))
169 annotatedElements.add(e);
170 }
171 e.accept(this, p);
172 return annotatedElements;
173 }
174 }
175
176 /**
177 * {@inheritdoc}
178 */
179 public Set<? extends Element> getElementsAnnotatedWith(Class<? extends Annotation> a) {
180 if (!a.isAnnotation())
181 throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a);
182 String name = a.getCanonicalName();
183 if (name == null)
184 return Collections.emptySet();
185 else {
186 TypeElement annotationType = processingEnv.getElementUtils().getTypeElement(name);
187 if (annotationType == null)
188 return Collections.emptySet();
189 else
190 return getElementsAnnotatedWith(annotationType);
191 }
192 }
193 }
|