3232import java .util .Map ;
3333import java .util .Objects ;
3434import java .util .function .Predicate ;
35- import java .util .stream .Collectors ;
3635
36+ import static io .microsphere .annotation .processor .util .TypeUtils .isSameType ;
37+ import static io .microsphere .annotation .processor .util .TypeUtils .isTypeElement ;
38+ import static io .microsphere .annotation .processor .util .TypeUtils .ofTypeElement ;
3739import static io .microsphere .lang .function .Predicates .EMPTY_PREDICATE_ARRAY ;
3840import static io .microsphere .lang .function .Streams .filterAll ;
3941import static io .microsphere .lang .function .Streams .filterFirst ;
4042import static java .lang .Enum .valueOf ;
4143import static java .util .Collections .emptyList ;
44+ import static java .util .stream .Collectors .toList ;
4245
4346/**
4447 * The utilities class for annotation in the package "javax.lang.model.*"
4548 *
49+ * @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
4650 * @since 1.0.0
4751 */
4852public abstract class AnnotationUtils {
@@ -61,85 +65,90 @@ public static List<AnnotationMirror> getAnnotations(AnnotatedConstruct annotated
6165 }
6266
6367 public static List <AnnotationMirror > getAnnotations (AnnotatedConstruct annotatedConstruct , CharSequence annotationClassName ) {
64- return getAnnotations (annotatedConstruct , annotation -> TypeUtils . isSameType (annotation .getAnnotationType (), annotationClassName ));
68+ return findAnnotations (annotatedConstruct , annotation -> isSameType (annotation .getAnnotationType (), annotationClassName ));
6569 }
6670
6771 public static List <AnnotationMirror > getAnnotations (AnnotatedConstruct annotatedConstruct ) {
68- return getAnnotations (annotatedConstruct , EMPTY_PREDICATE_ARRAY );
69- }
70-
71- public static List <AnnotationMirror > getAnnotations (AnnotatedConstruct annotatedConstruct , Predicate <AnnotationMirror >... annotationFilters ) {
72-
73- AnnotatedConstruct actualAnnotatedConstruct = annotatedConstruct ;
74-
75- if (annotatedConstruct instanceof TypeMirror ) {
76- actualAnnotatedConstruct = TypeUtils .ofTypeElement ((TypeMirror ) actualAnnotatedConstruct );
77- }
78-
79- return actualAnnotatedConstruct == null ? emptyList () : filterAll ((List <AnnotationMirror >) actualAnnotatedConstruct .getAnnotationMirrors (), annotationFilters );
72+ return findAnnotations (annotatedConstruct , EMPTY_PREDICATE_ARRAY );
8073 }
8174
8275 public static List <AnnotationMirror > getAllAnnotations (TypeMirror type ) {
83- return getAllAnnotations (TypeUtils . ofTypeElement (type ));
76+ return getAllAnnotations (ofTypeElement (type ));
8477 }
8578
8679 public static List <AnnotationMirror > getAllAnnotations (Element element ) {
87- return getAllAnnotations (element , EMPTY_PREDICATE_ARRAY );
80+ return findAllAnnotations (element , EMPTY_PREDICATE_ARRAY );
8881 }
8982
9083 public static List <AnnotationMirror > getAllAnnotations (TypeMirror type , Class <? extends Annotation > annotationClass ) {
91- return getAllAnnotations (TypeUtils . ofTypeElement (type ), annotationClass );
84+ return getAllAnnotations (ofTypeElement (type ), annotationClass );
9285 }
9386
9487 public static List <AnnotationMirror > getAllAnnotations (Element element , Class <? extends Annotation > annotationClass ) {
9588 return element == null || annotationClass == null ? emptyList () : getAllAnnotations (element , annotationClass .getTypeName ());
9689 }
9790
9891 public static List <AnnotationMirror > getAllAnnotations (TypeMirror type , CharSequence annotationClassName ) {
99- return getAllAnnotations (TypeUtils . ofTypeElement (type ), annotationClassName );
92+ return getAllAnnotations (ofTypeElement (type ), annotationClassName );
10093 }
10194
10295 public static List <AnnotationMirror > getAllAnnotations (Element element , CharSequence annotationClassName ) {
103- return getAllAnnotations (element , annotation -> TypeUtils . isSameType (annotation .getAnnotationType (), annotationClassName ));
96+ return findAllAnnotations (element , annotation -> isSameType (annotation .getAnnotationType (), annotationClassName ));
10497 }
10598
106- public static List <AnnotationMirror > getAllAnnotations (TypeMirror type , Predicate < AnnotationMirror >... annotationFilters ) {
107- return getAllAnnotations ( TypeUtils . ofTypeElement ( type ), annotationFilters );
99+ public static List <AnnotationMirror > getAllAnnotations (ProcessingEnvironment processingEnv , Type annotatedType ) {
100+ return findAllAnnotations ( processingEnv , annotatedType , EMPTY_PREDICATE_ARRAY );
108101 }
109102
110- public static List <AnnotationMirror > getAllAnnotations (Element element , Predicate <AnnotationMirror >... annotationFilters ) {
103+ public static List <AnnotationMirror > findAnnotations (AnnotatedConstruct annotatedConstruct , Predicate <? super AnnotationMirror >... annotationFilters ) {
104+
105+ AnnotatedConstruct actualAnnotatedConstruct = annotatedConstruct ;
111106
112- List <AnnotationMirror > allAnnotations = TypeUtils .isTypeElement (element ) ? TypeUtils .getHierarchicalTypes (TypeUtils .ofTypeElement (element )).stream ().map (AnnotationUtils ::getAnnotations ).flatMap (Collection ::stream ).collect (Collectors .toList ()) : element == null ? emptyList () : (List <AnnotationMirror >) element .getAnnotationMirrors ();
107+ if (annotatedConstruct instanceof TypeMirror ) {
108+ actualAnnotatedConstruct = ofTypeElement ((TypeMirror ) actualAnnotatedConstruct );
109+ }
113110
114- return filterAll (allAnnotations , annotationFilters );
111+ return actualAnnotatedConstruct == null ? emptyList () : filterAll (( List < AnnotationMirror >) actualAnnotatedConstruct . getAnnotationMirrors () , annotationFilters );
115112 }
116113
117- public static List <AnnotationMirror > getAllAnnotations ( ProcessingEnvironment processingEnv , Type annotatedType ) {
118- return getAllAnnotations ( processingEnv , annotatedType , EMPTY_PREDICATE_ARRAY );
114+ public static List <AnnotationMirror > findAllAnnotations ( TypeMirror type , Predicate <? super AnnotationMirror >... annotationFilters ) {
115+ return findAllAnnotations ( ofTypeElement ( type ), annotationFilters );
119116 }
120117
121- public static List <AnnotationMirror > getAllAnnotations (ProcessingEnvironment processingEnv , Type annotatedType , Predicate <AnnotationMirror >... annotationFilters ) {
122- return annotatedType == null ? emptyList () : getAllAnnotations (processingEnv , annotatedType .getTypeName (), annotationFilters );
118+ public static List <AnnotationMirror > findAllAnnotations (ProcessingEnvironment processingEnv , Type annotatedType , Predicate <? super AnnotationMirror >... annotationFilters ) {
119+ return annotatedType == null ? emptyList () : findAllAnnotations (processingEnv , annotatedType .getTypeName (), annotationFilters );
123120 }
124121
125- public static List <AnnotationMirror > getAllAnnotations (ProcessingEnvironment processingEnv , CharSequence annotatedTypeName , Predicate <AnnotationMirror >... annotationFilters ) {
126- return getAllAnnotations (TypeUtils .getType (processingEnv , annotatedTypeName ), annotationFilters );
122+ public static List <AnnotationMirror > findAllAnnotations (ProcessingEnvironment processingEnv , CharSequence annotatedTypeName , Predicate <? super AnnotationMirror >... annotationFilters ) {
123+ return findAllAnnotations (TypeUtils .getTypeElement (processingEnv , annotatedTypeName ), annotationFilters );
124+ }
125+
126+ public static List <AnnotationMirror > findAllAnnotations (Element element , Predicate <? super AnnotationMirror >... annotationFilters ) {
127+
128+ List <AnnotationMirror > allAnnotations = isTypeElement (element ) ?
129+ TypeUtils .getAllTypeElements (ofTypeElement (element ))
130+ .stream ()
131+ .map (AnnotationUtils ::getAnnotations )
132+ .flatMap (Collection ::stream )
133+ .collect (toList ()) : element == null ? emptyList () : (List <AnnotationMirror >) element .getAnnotationMirrors ();
134+
135+ return filterAll (allAnnotations , annotationFilters );
127136 }
128137
129138 public static AnnotationMirror findAnnotation (TypeMirror type , Class <? extends Annotation > annotationClass ) {
130139 return annotationClass == null ? null : findAnnotation (type , annotationClass .getTypeName ());
131140 }
132141
133142 public static AnnotationMirror findAnnotation (TypeMirror type , CharSequence annotationClassName ) {
134- return findAnnotation (TypeUtils . ofTypeElement (type ), annotationClassName );
143+ return findAnnotation (ofTypeElement (type ), annotationClassName );
135144 }
136145
137146 public static AnnotationMirror findAnnotation (Element element , Class <? extends Annotation > annotationClass ) {
138147 return annotationClass == null ? null : findAnnotation (element , annotationClass .getTypeName ());
139148 }
140149
141150 public static AnnotationMirror findAnnotation (Element element , CharSequence annotationClassName ) {
142- return filterFirst (getAllAnnotations (element , annotation -> TypeUtils . isSameType (annotation .getAnnotationType (), annotationClassName )));
151+ return filterFirst (findAllAnnotations (element , annotation -> isSameType (annotation .getAnnotationType (), annotationClassName )));
143152 }
144153
145154 public static AnnotationMirror findMetaAnnotation (Element annotatedConstruct , CharSequence metaAnnotationClassName ) {
0 commit comments