1616 */
1717package io .microsphere .annotation .processor .util ;
1818
19+ import io .microsphere .util .Utils ;
20+
1921import javax .lang .model .element .Element ;
2022import javax .lang .model .element .Modifier ;
2123import javax .lang .model .element .VariableElement ;
2224import javax .lang .model .type .TypeMirror ;
23- import java .util .Collection ;
2425import java .util .List ;
2526import java .util .function .Predicate ;
26- import java .util .stream .Collectors ;
2727
28+ import static io .microsphere .annotation .processor .util .MemberUtils .getAllDeclaredMembers ;
2829import static io .microsphere .annotation .processor .util .MemberUtils .getDeclaredMembers ;
2930import static io .microsphere .annotation .processor .util .MemberUtils .hasModifiers ;
30- import static io .microsphere .annotation .processor .util .MemberUtils .matches ;
31- import static io .microsphere .annotation .processor .util .TypeUtils .getHierarchicalTypes ;
31+ import static io .microsphere .annotation .processor .util .MemberUtils .matchesElementKind ;
3232import static io .microsphere .annotation .processor .util .TypeUtils .isEnumType ;
33+ import static io .microsphere .collection .CollectionUtils .isEmpty ;
3334import static io .microsphere .lang .function .Predicates .EMPTY_PREDICATE_ARRAY ;
3435import static io .microsphere .lang .function .Streams .filterAll ;
3536import static io .microsphere .lang .function .Streams .filterFirst ;
37+ import static io .microsphere .util .ArrayUtils .isNotEmpty ;
3638import static java .util .Collections .emptyList ;
3739import static javax .lang .model .element .ElementKind .ENUM_CONSTANT ;
3840import static javax .lang .model .element .ElementKind .FIELD ;
4244/**
4345 * The utilities class for the field in the package "javax.lang.model."
4446 *
47+ * @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
4548 * @since 1.0.0
4649 */
47- public interface FieldUtils {
50+ public interface FieldUtils extends Utils {
4851
49- static List < VariableElement > getDeclaredFields (Element element , Predicate < VariableElement >... fieldFilters ) {
50- return element == null ? emptyList () : getDeclaredFields (element .asType (), fieldFilters );
52+ static VariableElement getDeclaredField (Element element , String fieldName ) {
53+ return element == null ? null : getDeclaredField (element .asType (), fieldName );
5154 }
5255
53- static List < VariableElement > getDeclaredFields ( Element element ) {
54- return getDeclaredFields ( element , EMPTY_PREDICATE_ARRAY );
56+ static VariableElement getDeclaredField ( TypeMirror type , String fieldName ) {
57+ return filterFirst ( findDeclaredFields ( type , field -> fieldName . equals ( field . getSimpleName (). toString ())) );
5558 }
5659
57- static List <VariableElement > getDeclaredFields (TypeMirror type , Predicate < VariableElement >... fieldFilters ) {
58- return filterAll ( fieldsIn ( getDeclaredMembers ( type )), fieldFilters );
60+ static List <VariableElement > getDeclaredFields (Element element ) {
61+ return findDeclaredFields ( element , EMPTY_PREDICATE_ARRAY );
5962 }
6063
6164 static List <VariableElement > getDeclaredFields (TypeMirror type ) {
62- return getDeclaredFields (type , EMPTY_PREDICATE_ARRAY );
65+ return findDeclaredFields (type , EMPTY_PREDICATE_ARRAY );
6366 }
6467
65- static List <VariableElement > getAllDeclaredFields (Element element , Predicate < VariableElement >... fieldFilters ) {
66- return element == null ? emptyList () : getAllDeclaredFields ( element . asType (), fieldFilters );
68+ static List <VariableElement > getAllDeclaredFields (Element element ) {
69+ return findAllDeclaredFields ( element , EMPTY_PREDICATE_ARRAY );
6770 }
6871
69- static List <VariableElement > getAllDeclaredFields (Element element ) {
70- return getAllDeclaredFields ( element , EMPTY_PREDICATE_ARRAY );
72+ static List <VariableElement > getAllDeclaredFields (TypeMirror type ) {
73+ return findAllDeclaredFields ( type , EMPTY_PREDICATE_ARRAY );
7174 }
7275
73- static List <VariableElement > getAllDeclaredFields (TypeMirror type , Predicate <VariableElement >... fieldFilters ) {
74- return getHierarchicalTypes (type )
75- .stream ()
76- .map (t -> getDeclaredFields (t , fieldFilters ))
77- .flatMap (Collection ::stream )
78- .collect (Collectors .toList ());
76+ static VariableElement findField (Element element , String fieldName ) {
77+ return element == null ? null : findField (element .asType (), fieldName );
7978 }
8079
81- static List < VariableElement > getAllDeclaredFields (TypeMirror type ) {
82- return getAllDeclaredFields ( type , EMPTY_PREDICATE_ARRAY );
80+ static VariableElement findField (TypeMirror type , String fieldName ) {
81+ return filterFirst ( findAllDeclaredFields ( type , field -> equalsFieldName ( field , fieldName )) );
8382 }
8483
85- static VariableElement getDeclaredField (Element element , String fieldName ) {
86- return element == null ? null : getDeclaredField (element .asType (), fieldName );
84+ static List < VariableElement > findDeclaredFields (Element element , Predicate <? super VariableElement >... fieldFilters ) {
85+ return element == null ? emptyList () : findDeclaredFields (element .asType (), fieldFilters );
8786 }
8887
89- static VariableElement getDeclaredField (TypeMirror type , String fieldName ) {
90- return filterFirst ( getDeclaredFields ( type , field -> fieldName . equals ( field . getSimpleName (). toString ())) );
88+ static List < VariableElement > findDeclaredFields (TypeMirror type , Predicate <? super VariableElement >... fieldFilters ) {
89+ return filterDeclaredFields ( type , false , fieldFilters );
9190 }
9291
93- static VariableElement findField (Element element , String fieldName ) {
94- return element == null ? null : findField (element .asType (), fieldName );
92+ static List < VariableElement > findAllDeclaredFields (Element element , Predicate <? super VariableElement >... fieldFilters ) {
93+ return element == null ? emptyList () : findAllDeclaredFields (element .asType (), fieldFilters );
9594 }
9695
97- static VariableElement findField (TypeMirror type , String fieldName ) {
98- return filterFirst (getAllDeclaredFields (type , field -> equals (field , fieldName )));
96+ static List <VariableElement > findAllDeclaredFields (TypeMirror type , Predicate <? super VariableElement >... fieldFilters ) {
97+ return filterDeclaredFields (type , true , fieldFilters );
98+ }
99+
100+ static List <VariableElement > filterDeclaredFields (TypeMirror type , boolean all , Predicate <? super VariableElement >... fieldFilters ) {
101+ if (type == null ) {
102+ return emptyList ();
103+ }
104+
105+ List <? extends Element > declaredMembers = all ? getAllDeclaredMembers (type ) : getDeclaredMembers (type );
106+ if (isEmpty (declaredMembers )) {
107+ return emptyList ();
108+ }
109+
110+ List <VariableElement > fields = fieldsIn (declaredMembers );
111+ if (isEmpty (fields )) {
112+ return emptyList ();
113+ }
114+
115+ if (isNotEmpty (fieldFilters )) {
116+ fields = filterAll (fields , fieldFilters );
117+ }
118+
119+ return isEmpty (fields ) ? emptyList () : fields ;
99120 }
100121
101122 /**
@@ -116,31 +137,30 @@ static boolean isNonStaticField(VariableElement field) {
116137 }
117138
118139 static boolean isField (VariableElement field ) {
119- return matches (field , FIELD ) || isEnumMemberField (field );
140+ return matchesElementKind (field , FIELD ) || isEnumMemberField (field );
120141 }
121142
122143 static boolean isField (VariableElement field , Modifier ... modifiers ) {
123144 return isField (field ) && hasModifiers (field , modifiers );
124145 }
125146
126147 static List <VariableElement > getNonStaticFields (TypeMirror type ) {
127- return getDeclaredFields (type , FieldUtils ::isNonStaticField );
148+ return findDeclaredFields (type , FieldUtils ::isNonStaticField );
128149 }
129150
130151 static List <VariableElement > getNonStaticFields (Element element ) {
131152 return element == null ? emptyList () : getNonStaticFields (element .asType ());
132153 }
133154
134155 static List <VariableElement > getAllNonStaticFields (TypeMirror type ) {
135- return getAllDeclaredFields (type , FieldUtils ::isNonStaticField );
156+ return findAllDeclaredFields (type , FieldUtils ::isNonStaticField );
136157 }
137158
138159 static List <VariableElement > getAllNonStaticFields (Element element ) {
139160 return element == null ? emptyList () : getAllNonStaticFields (element .asType ());
140161 }
141162
142- static boolean equals (VariableElement field , CharSequence fieldName ) {
163+ static boolean equalsFieldName (VariableElement field , CharSequence fieldName ) {
143164 return field != null && fieldName != null && field .getSimpleName ().toString ().equals (fieldName .toString ());
144165 }
145-
146166}
0 commit comments