3939import static io .microsphere .lang .function .Predicates .EMPTY_PREDICATE_ARRAY ;
4040import static io .microsphere .lang .function .Streams .filterAll ;
4141import static io .microsphere .lang .function .Streams .filterFirst ;
42+ import static io .microsphere .util .ClassLoaderUtils .resolveClass ;
4243import static java .lang .Enum .valueOf ;
4344import static java .util .Collections .emptyList ;
4445import static java .util .stream .Collectors .toList ;
4950 * @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
5051 * @since 1.0.0
5152 */
52- public abstract class AnnotationUtils {
53+ public interface AnnotationUtils {
5354
54- public static AnnotationMirror getAnnotation (AnnotatedConstruct annotatedConstruct , Class <? extends Annotation > annotationClass ) {
55+ static AnnotationMirror getAnnotation (AnnotatedConstruct annotatedConstruct , Class <? extends Annotation > annotationClass ) {
5556 return annotationClass == null ? null : getAnnotation (annotatedConstruct , annotationClass .getTypeName ());
5657 }
5758
58- public static AnnotationMirror getAnnotation (AnnotatedConstruct annotatedConstruct , CharSequence annotationClassName ) {
59+ static AnnotationMirror getAnnotation (AnnotatedConstruct annotatedConstruct , CharSequence annotationClassName ) {
5960 List <AnnotationMirror > annotations = getAnnotations (annotatedConstruct , annotationClassName );
6061 return annotations .isEmpty () ? null : annotations .get (0 );
6162 }
6263
63- public static List <AnnotationMirror > getAnnotations (AnnotatedConstruct annotatedConstruct , Class <? extends Annotation > annotationClass ) {
64+ static List <AnnotationMirror > getAnnotations (AnnotatedConstruct annotatedConstruct , Class <? extends Annotation > annotationClass ) {
6465 return annotationClass == null ? emptyList () : getAnnotations (annotatedConstruct , annotationClass .getTypeName ());
6566 }
6667
67- public static List <AnnotationMirror > getAnnotations (AnnotatedConstruct annotatedConstruct , CharSequence annotationClassName ) {
68+ static List <AnnotationMirror > getAnnotations (AnnotatedConstruct annotatedConstruct , CharSequence annotationClassName ) {
6869 return findAnnotations (annotatedConstruct , annotation -> isSameType (annotation .getAnnotationType (), annotationClassName ));
6970 }
7071
71- public static List <AnnotationMirror > getAnnotations (AnnotatedConstruct annotatedConstruct ) {
72+ static List <AnnotationMirror > getAnnotations (AnnotatedConstruct annotatedConstruct ) {
7273 return findAnnotations (annotatedConstruct , EMPTY_PREDICATE_ARRAY );
7374 }
7475
75- public static List <AnnotationMirror > getAllAnnotations (TypeMirror type ) {
76+ static List <AnnotationMirror > getAllAnnotations (TypeMirror type ) {
7677 return getAllAnnotations (ofTypeElement (type ));
7778 }
7879
79- public static List <AnnotationMirror > getAllAnnotations (Element element ) {
80+ static List <AnnotationMirror > getAllAnnotations (Element element ) {
8081 return findAllAnnotations (element , EMPTY_PREDICATE_ARRAY );
8182 }
8283
83- public static List <AnnotationMirror > getAllAnnotations (TypeMirror type , Class <? extends Annotation > annotationClass ) {
84+ static List <AnnotationMirror > getAllAnnotations (TypeMirror type , Class <? extends Annotation > annotationClass ) {
8485 return getAllAnnotations (ofTypeElement (type ), annotationClass );
8586 }
8687
87- public static List <AnnotationMirror > getAllAnnotations (Element element , Class <? extends Annotation > annotationClass ) {
88+ static List <AnnotationMirror > getAllAnnotations (Element element , Class <? extends Annotation > annotationClass ) {
8889 return element == null || annotationClass == null ? emptyList () : getAllAnnotations (element , annotationClass .getTypeName ());
8990 }
9091
91- public static List <AnnotationMirror > getAllAnnotations (TypeMirror type , CharSequence annotationClassName ) {
92+ static List <AnnotationMirror > getAllAnnotations (TypeMirror type , CharSequence annotationClassName ) {
9293 return getAllAnnotations (ofTypeElement (type ), annotationClassName );
9394 }
9495
95- public static List <AnnotationMirror > getAllAnnotations (Element element , CharSequence annotationClassName ) {
96+ static List <AnnotationMirror > getAllAnnotations (Element element , CharSequence annotationClassName ) {
9697 return findAllAnnotations (element , annotation -> isSameType (annotation .getAnnotationType (), annotationClassName ));
9798 }
9899
99- public static List <AnnotationMirror > getAllAnnotations (ProcessingEnvironment processingEnv , Type annotatedType ) {
100+ static List <AnnotationMirror > getAllAnnotations (ProcessingEnvironment processingEnv , Type annotatedType ) {
100101 return findAllAnnotations (processingEnv , annotatedType , EMPTY_PREDICATE_ARRAY );
101102 }
102103
103- public static List <AnnotationMirror > findAnnotations (AnnotatedConstruct annotatedConstruct , Predicate <? super AnnotationMirror >... annotationFilters ) {
104+ static List <AnnotationMirror > findAnnotations (AnnotatedConstruct annotatedConstruct , Predicate <? super AnnotationMirror >... annotationFilters ) {
104105
105106 AnnotatedConstruct actualAnnotatedConstruct = annotatedConstruct ;
106107
@@ -111,19 +112,19 @@ public static List<AnnotationMirror> findAnnotations(AnnotatedConstruct annotate
111112 return actualAnnotatedConstruct == null ? emptyList () : filterAll ((List <AnnotationMirror >) actualAnnotatedConstruct .getAnnotationMirrors (), annotationFilters );
112113 }
113114
114- public static List <AnnotationMirror > findAllAnnotations (TypeMirror type , Predicate <? super AnnotationMirror >... annotationFilters ) {
115+ static List <AnnotationMirror > findAllAnnotations (TypeMirror type , Predicate <? super AnnotationMirror >... annotationFilters ) {
115116 return findAllAnnotations (ofTypeElement (type ), annotationFilters );
116117 }
117118
118- public static List <AnnotationMirror > findAllAnnotations (ProcessingEnvironment processingEnv , Type annotatedType , Predicate <? super AnnotationMirror >... annotationFilters ) {
119+ static List <AnnotationMirror > findAllAnnotations (ProcessingEnvironment processingEnv , Type annotatedType , Predicate <? super AnnotationMirror >... annotationFilters ) {
119120 return annotatedType == null ? emptyList () : findAllAnnotations (processingEnv , annotatedType .getTypeName (), annotationFilters );
120121 }
121122
122- public static List <AnnotationMirror > findAllAnnotations (ProcessingEnvironment processingEnv , CharSequence annotatedTypeName , Predicate <? super AnnotationMirror >... annotationFilters ) {
123+ static List <AnnotationMirror > findAllAnnotations (ProcessingEnvironment processingEnv , CharSequence annotatedTypeName , Predicate <? super AnnotationMirror >... annotationFilters ) {
123124 return findAllAnnotations (TypeUtils .getTypeElement (processingEnv , annotatedTypeName ), annotationFilters );
124125 }
125126
126- public static List <AnnotationMirror > findAllAnnotations (Element element , Predicate <? super AnnotationMirror >... annotationFilters ) {
127+ static List <AnnotationMirror > findAllAnnotations (Element element , Predicate <? super AnnotationMirror >... annotationFilters ) {
127128
128129 List <AnnotationMirror > allAnnotations = isTypeElement (element ) ?
129130 TypeUtils .getAllTypeElements (ofTypeElement (element ))
@@ -135,35 +136,35 @@ public static List<AnnotationMirror> findAllAnnotations(Element element, Predica
135136 return filterAll (allAnnotations , annotationFilters );
136137 }
137138
138- public static AnnotationMirror findAnnotation (TypeMirror type , Class <? extends Annotation > annotationClass ) {
139+ static AnnotationMirror findAnnotation (TypeMirror type , Class <? extends Annotation > annotationClass ) {
139140 return annotationClass == null ? null : findAnnotation (type , annotationClass .getTypeName ());
140141 }
141142
142- public static AnnotationMirror findAnnotation (TypeMirror type , CharSequence annotationClassName ) {
143+ static AnnotationMirror findAnnotation (TypeMirror type , CharSequence annotationClassName ) {
143144 return findAnnotation (ofTypeElement (type ), annotationClassName );
144145 }
145146
146- public static AnnotationMirror findAnnotation (Element element , Class <? extends Annotation > annotationClass ) {
147+ static AnnotationMirror findAnnotation (Element element , Class <? extends Annotation > annotationClass ) {
147148 return annotationClass == null ? null : findAnnotation (element , annotationClass .getTypeName ());
148149 }
149150
150- public static AnnotationMirror findAnnotation (Element element , CharSequence annotationClassName ) {
151+ static AnnotationMirror findAnnotation (Element element , CharSequence annotationClassName ) {
151152 return filterFirst (findAllAnnotations (element , annotation -> isSameType (annotation .getAnnotationType (), annotationClassName )));
152153 }
153154
154- public static AnnotationMirror findMetaAnnotation (Element annotatedConstruct , CharSequence metaAnnotationClassName ) {
155+ static AnnotationMirror findMetaAnnotation (Element annotatedConstruct , CharSequence metaAnnotationClassName ) {
155156 return annotatedConstruct == null ? null : getAnnotations (annotatedConstruct ).stream ().map (annotation -> findAnnotation (annotation .getAnnotationType (), metaAnnotationClassName )).filter (Objects ::nonNull ).findFirst ().orElse (null );
156157 }
157158
158- public static boolean isAnnotationPresent (Element element , CharSequence annotationClassName ) {
159+ static boolean isAnnotationPresent (Element element , CharSequence annotationClassName ) {
159160 return findAnnotation (element , annotationClassName ) != null || findMetaAnnotation (element , annotationClassName ) != null ;
160161 }
161162
162- public static <T > T getAttribute (AnnotationMirror annotation , String attributeName ) {
163+ static <T > T getAttribute (AnnotationMirror annotation , String attributeName ) {
163164 return annotation == null ? null : getAttribute (annotation .getElementValues (), attributeName );
164165 }
165166
166- public static <T > T getAttribute (Map <? extends ExecutableElement , ? extends AnnotationValue > attributesMap , String attributeName ) {
167+ static <T > T getAttribute (Map <? extends ExecutableElement , ? extends AnnotationValue > attributesMap , String attributeName ) {
167168 T annotationValue = null ;
168169 for (Map .Entry <? extends ExecutableElement , ? extends AnnotationValue > entry : attributesMap .entrySet ()) {
169170 ExecutableElement attributeMethod = entry .getKey ();
@@ -172,25 +173,21 @@ public static <T> T getAttribute(Map<? extends ExecutableElement, ? extends Anno
172173 AnnotationValue value = entry .getValue ();
173174 if (attributeType instanceof ArrayType ) { // array-typed attribute values
174175 ArrayType arrayType = (ArrayType ) attributeType ;
175- String componentType = arrayType .getComponentType ().toString ();
176+ String componentTypeName = arrayType .getComponentType ().toString ();
176177 ClassLoader classLoader = AnnotationUtils .class .getClassLoader ();
177178 List <AnnotationValue > values = (List <AnnotationValue >) value .getValue ();
178179 int size = values .size ();
179- try {
180- Class componentClass = classLoader .loadClass (componentType );
181- boolean isEnum = componentClass .isEnum ();
182- Object array = Array .newInstance (componentClass , values .size ());
183- for (int i = 0 ; i < size ; i ++) {
184- Object element = values .get (i ).getValue ();
185- if (isEnum ) {
186- element = valueOf (componentClass , element .toString ());
187- }
188- Array .set (array , i , element );
180+ Class componentClass = resolveClass (componentTypeName , classLoader );
181+ boolean isEnum = componentClass .isEnum ();
182+ Object array = Array .newInstance (componentClass , values .size ());
183+ for (int i = 0 ; i < size ; i ++) {
184+ Object element = values .get (i ).getValue ();
185+ if (isEnum ) {
186+ element = valueOf (componentClass , element .toString ());
189187 }
190- annotationValue = (T ) array ;
191- } catch (ClassNotFoundException e ) {
192- throw new RuntimeException (e );
188+ Array .set (array , i , element );
193189 }
190+ annotationValue = (T ) array ;
194191 } else {
195192 annotationValue = (T ) value .getValue ();
196193 }
@@ -200,7 +197,7 @@ public static <T> T getAttribute(Map<? extends ExecutableElement, ? extends Anno
200197 return annotationValue ;
201198 }
202199
203- public static <T > T getValue (AnnotationMirror annotation ) {
204- return ( T ) getAttribute (annotation , "value" );
200+ static <T > T getValue (AnnotationMirror annotation ) {
201+ return getAttribute (annotation , "value" );
205202 }
206203}
0 commit comments