Skip to content

Commit 2b57675

Browse files
authored
Merge pull request #127 from mercyblitz/dev
Test Coverage
2 parents d39ccfd + 12fdeef commit 2b57675

14 files changed

Lines changed: 948 additions & 271 deletions

File tree

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/AnnotationUtils.java

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import static io.microsphere.lang.function.Predicates.EMPTY_PREDICATE_ARRAY;
4040
import static io.microsphere.lang.function.Streams.filterAll;
4141
import static io.microsphere.lang.function.Streams.filterFirst;
42+
import static io.microsphere.util.ClassLoaderUtils.resolveClass;
4243
import static java.lang.Enum.valueOf;
4344
import static java.util.Collections.emptyList;
4445
import static java.util.stream.Collectors.toList;
@@ -49,58 +50,58 @@
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
}

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/ExecutableElementComparator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
*/
3838
public class ExecutableElementComparator implements Comparator<ExecutableElement> {
3939

40+
/**
41+
* The singleton instance
42+
*/
4043
public static final ExecutableElementComparator INSTANCE = new ExecutableElementComparator();
4144

4245
private ExecutableElementComparator() {
@@ -61,7 +64,7 @@ public int compare(ExecutableElement e1, ExecutableElement e2) {
6164

6265
if (value == 0) { // Step 3
6366
for (int i = 0; i < ps1.size(); i++) {
64-
value = CharSequenceComparator.INSTANCE.compare(ps1.get(i).getSimpleName(), ps2.get(i).getSimpleName());
67+
value = CharSequenceComparator.INSTANCE.compare(ps1.get(i).asType().toString(), ps2.get(i).asType().toString());
6568
if (value != 0) {
6669
break;
6770
}

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/FieldUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import static io.microsphere.annotation.processor.util.MemberUtils.getDeclaredMembers;
2929
import static io.microsphere.annotation.processor.util.MemberUtils.hasModifiers;
30-
import static io.microsphere.annotation.processor.util.MemberUtils.matches;
30+
import static io.microsphere.annotation.processor.util.MemberUtils.matchesElementKind;
3131
import static io.microsphere.annotation.processor.util.TypeUtils.getAllDeclaredTypes;
3232
import static io.microsphere.annotation.processor.util.TypeUtils.isEnumType;
3333
import static io.microsphere.lang.function.Predicates.EMPTY_PREDICATE_ARRAY;
@@ -117,7 +117,7 @@ static boolean isNonStaticField(VariableElement field) {
117117
}
118118

119119
static boolean isField(VariableElement field) {
120-
return matches(field, FIELD) || isEnumMemberField(field);
120+
return matchesElementKind(field, FIELD) || isEnumMemberField(field);
121121
}
122122

123123
static boolean isField(VariableElement field, Modifier... modifiers) {

microsphere-annotation-processor/src/main/java/io/microsphere/annotation/processor/util/MemberUtils.java

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@
2222
import javax.lang.model.element.TypeElement;
2323
import javax.lang.model.element.VariableElement;
2424
import javax.lang.model.type.TypeMirror;
25+
import java.lang.reflect.Type;
2526
import java.util.Collection;
2627
import java.util.List;
27-
import java.util.Objects;
2828
import java.util.Set;
29+
import java.util.function.Predicate;
2930

3031
import static io.microsphere.annotation.processor.util.TypeUtils.getAllDeclaredTypes;
32+
import static io.microsphere.annotation.processor.util.TypeUtils.isSameType;
3133
import static io.microsphere.annotation.processor.util.TypeUtils.ofTypeElement;
34+
import static io.microsphere.collection.CollectionUtils.isEmpty;
35+
import static io.microsphere.lang.function.Predicates.EMPTY_PREDICATE_ARRAY;
36+
import static io.microsphere.lang.function.Predicates.and;
37+
import static io.microsphere.reflect.TypeUtils.getTypeNames;
38+
import static io.microsphere.util.ArrayUtils.isNotEmpty;
39+
import static io.microsphere.util.ArrayUtils.length;
3240
import static java.util.Collections.emptyList;
3341
import static java.util.stream.Collectors.toList;
3442
import static javax.lang.model.element.Modifier.PUBLIC;
@@ -42,7 +50,7 @@
4250
*/
4351
public interface MemberUtils {
4452

45-
static boolean matches(Element member, ElementKind kind) {
53+
static boolean matchesElementKind(Element member, ElementKind kind) {
4654
return member == null || kind == null ? false : kind.equals(member.getKind());
4755
}
4856

@@ -64,29 +72,78 @@ static boolean hasModifiers(Element member, Modifier... modifiers) {
6472
}
6573

6674
static List<? extends Element> getDeclaredMembers(TypeMirror type) {
67-
TypeElement element = ofTypeElement(type);
68-
return element == null ? emptyList() : element.getEnclosedElements();
75+
return type == null ? emptyList() : getDeclaredMembers(ofTypeElement(type));
76+
}
77+
78+
static List<? extends Element> getDeclaredMembers(TypeElement type) {
79+
return type == null ? emptyList() : findDeclaredMembers(type, EMPTY_PREDICATE_ARRAY);
6980
}
7081

7182
static List<? extends Element> getAllDeclaredMembers(TypeMirror type) {
72-
return getAllDeclaredTypes(type)
83+
return type == null ? emptyList() : findAllDeclaredMembers(ofTypeElement(type), EMPTY_PREDICATE_ARRAY);
84+
}
85+
86+
static List<? extends Element> getAllDeclaredMembers(TypeElement type) {
87+
return type == null ? emptyList() : findAllDeclaredMembers(type, EMPTY_PREDICATE_ARRAY);
88+
}
89+
90+
static List<? extends Element> findDeclaredMembers(TypeMirror type, Predicate<? super Element>... memberFilters) {
91+
return type == null ? emptyList() : findDeclaredMembers(ofTypeElement(type), memberFilters);
92+
}
93+
94+
static List<? extends Element> findDeclaredMembers(TypeElement type, Predicate<? super Element>... memberFilters) {
95+
if (type == null) {
96+
return emptyList();
97+
}
98+
return filterMembers(type.getEnclosedElements(), memberFilters);
99+
}
100+
101+
static List<? extends Element> findAllDeclaredMembers(TypeMirror type, Predicate<? super Element>... memberFilters) {
102+
return type == null ? emptyList() : findAllDeclaredMembers(ofTypeElement(type), memberFilters);
103+
}
104+
105+
static List<? extends Element> findAllDeclaredMembers(TypeElement type, Predicate<? super Element>... memberFilters) {
106+
if (type == null) {
107+
return emptyList();
108+
}
109+
List<? extends Element> declaredMembers = getAllDeclaredTypes(type)
73110
.stream()
74111
.map(MemberUtils::getDeclaredMembers)
75112
.flatMap(Collection::stream)
76113
.collect(toList());
114+
return filterMembers(declaredMembers, memberFilters);
77115
}
78116

79-
static boolean matchParameterTypes(List<? extends VariableElement> parameters, CharSequence... parameterTypes) {
117+
static List<? extends Element> filterMembers(List<? extends Element> members, Predicate<? super Element>... memberFilters) {
118+
if (isEmpty(members)) {
119+
return emptyList();
120+
}
121+
if (isNotEmpty(memberFilters)) {
122+
Predicate predicate = and(memberFilters);
123+
members = (List) members.stream().filter(predicate).collect(toList());
124+
}
125+
return members.isEmpty() ? emptyList() : members;
126+
}
127+
128+
static boolean matchParameterTypes(List<? extends VariableElement> parameters, Type... parameterTypes) {
129+
return parameters == null || parameterTypes == null ? false : matchParameterTypeNames(parameters, getTypeNames(parameterTypes));
130+
}
131+
132+
static boolean matchParameterTypeNames(List<? extends VariableElement> parameters, CharSequence... parameterTypeNames) {
133+
if (parameters == null || parameterTypeNames == null) {
134+
return false;
135+
}
80136

137+
int length = length(parameterTypeNames);
81138
int size = parameters.size();
82139

83-
if (size != parameterTypes.length) {
140+
if (size != length) {
84141
return false;
85142
}
86143

87144
for (int i = 0; i < size; i++) {
88145
VariableElement parameter = parameters.get(i);
89-
if (!Objects.equals(parameter.asType().toString(), parameterTypes[i])) {
146+
if (!isSameType(parameter, parameterTypeNames[i])) {
90147
return false;
91148
}
92149
}

0 commit comments

Comments
 (0)