108 * elements are {@linkplain #getSpecifiedTypeElements specified 109 * types} and any types nested within them. 110 * 111 * @param a annotation type being requested 112 * @return the elements annotated with the given annotation type, 113 * or an empty set if there are none 114 */ 115 public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { 116 Set<Element> result = Collections.emptySet(); 117 Types typeUtil = processingEnv.getTypeUtils(); 118 if (a.getKind() != ElementKind.ANNOTATION_TYPE) 119 throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); 120 121 DeclaredType annotationTypeElement; 122 TypeMirror tm = a.asType(); 123 if ( tm instanceof DeclaredType ) 124 annotationTypeElement = (DeclaredType) a.asType(); 125 else 126 throw new AssertionError("Bad implementation type for " + tm); 127 128 ElementScanner6<Set<Element>, DeclaredType> scanner = 129 new AnnotationSetScanner(result, typeUtil); 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 ElementScanner6<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 } | 108 * elements are {@linkplain #getSpecifiedTypeElements specified 109 * types} and any types nested within them. 110 * 111 * @param a annotation type being requested 112 * @return the elements annotated with the given annotation type, 113 * or an empty set if there are none 114 */ 115 public Set<? extends Element> getElementsAnnotatedWith(TypeElement a) { 116 Set<Element> result = Collections.emptySet(); 117 Types typeUtil = processingEnv.getTypeUtils(); 118 if (a.getKind() != ElementKind.ANNOTATION_TYPE) 119 throw new IllegalArgumentException(NOT_AN_ANNOTATION_TYPE + a); 120 121 DeclaredType annotationTypeElement; 122 TypeMirror tm = a.asType(); 123 if ( tm instanceof DeclaredType ) 124 annotationTypeElement = (DeclaredType) a.asType(); 125 else 126 throw new AssertionError("Bad implementation type for " + tm); 127 128 ElementScanner7<Set<Element>, DeclaredType> scanner = 129 new AnnotationSetScanner(result, typeUtil); 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 ElementScanner7<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 } |