Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
37dda0a
Update TypeUtils.java
mercyblitz Mar 23, 2025
19086bd
Update TypeUtils.java
mercyblitz Mar 23, 2025
c91ff2e
Update TypeUtilsTest.java
mercyblitz Mar 23, 2025
f041576
Merge branch 'dev' of https://github.com/mercyblitz/microsphere-java …
mercyblitz Mar 23, 2025
9228807
Update ExecutableElementComparator.java
mercyblitz Mar 23, 2025
8bfe8ff
Create ExecutableElementComparatorTest.java
mercyblitz Mar 23, 2025
1cdb618
Update AbstractAnnotationProcessingTest.java
mercyblitz Mar 25, 2025
a1b3587
Update MethodUtils.java
mercyblitz Mar 25, 2025
4831ab3
Update TypeUtilsTest.java
mercyblitz Mar 25, 2025
8c10a0d
Update AbstractAnnotationProcessingTest.java
mercyblitz Mar 25, 2025
8185741
Update AnnotationUtilsTest.java
mercyblitz Mar 25, 2025
af76dea
Update FieldUtilsTest.java
mercyblitz Mar 25, 2025
9825487
Update MemberUtilsTest.java
mercyblitz Mar 25, 2025
d0b4019
Update MethodUtilsTest.java
mercyblitz Mar 25, 2025
9a64cc6
Update TypeUtilsTest.java
mercyblitz Mar 25, 2025
0021be7
Update MemberUtilsTest.java
mercyblitz Mar 25, 2025
6b0a017
Update MethodUtilsTest.java
mercyblitz Mar 25, 2025
d9f4f06
Update MemberUtils.java
mercyblitz Mar 25, 2025
ecab9de
Update MethodUtils.java
mercyblitz Mar 25, 2025
f21bef9
Update MemberUtilsTest.java
mercyblitz Mar 25, 2025
b819f83
Update MethodUtilsTest.java
mercyblitz Mar 25, 2025
71f059c
Update MemberUtils.java
mercyblitz Mar 25, 2025
4a0a65d
Update FieldUtils.java
mercyblitz Mar 25, 2025
c9f3690
Update MethodUtils.java
mercyblitz Mar 25, 2025
4ea579a
Update TypeUtils.java
mercyblitz Mar 25, 2025
9fea307
Update MemberUtilsTest.java
mercyblitz Mar 25, 2025
54855d6
Update MethodUtilsTest.java
mercyblitz Mar 25, 2025
fa09a91
Update AbstractAnnotationProcessingTest.java
mercyblitz Mar 25, 2025
8d774f6
Update MethodUtils.java
mercyblitz Mar 25, 2025
f098029
Update MethodUtilsTest.java
mercyblitz Mar 25, 2025
fb82ca4
Update TestServiceImpl.java
mercyblitz Mar 25, 2025
1a3a7f0
Update AnnotationUtils.java
mercyblitz Mar 25, 2025
12fdeef
Update AnnotationUtilsTest.java
mercyblitz Mar 25, 2025
2b57675
Merge pull request #127 from mercyblitz/dev
mercyblitz Mar 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static io.microsphere.lang.function.Predicates.EMPTY_PREDICATE_ARRAY;
import static io.microsphere.lang.function.Streams.filterAll;
import static io.microsphere.lang.function.Streams.filterFirst;
import static io.microsphere.util.ClassLoaderUtils.resolveClass;
import static java.lang.Enum.valueOf;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
Expand All @@ -49,58 +50,58 @@
* @author <a href="mailto:mercyblitz@gmail.com">Mercy<a/>
* @since 1.0.0
*/
public abstract class AnnotationUtils {
public interface AnnotationUtils {

public static AnnotationMirror getAnnotation(AnnotatedConstruct annotatedConstruct, Class<? extends Annotation> annotationClass) {
static AnnotationMirror getAnnotation(AnnotatedConstruct annotatedConstruct, Class<? extends Annotation> annotationClass) {
return annotationClass == null ? null : getAnnotation(annotatedConstruct, annotationClass.getTypeName());
}

public static AnnotationMirror getAnnotation(AnnotatedConstruct annotatedConstruct, CharSequence annotationClassName) {
static AnnotationMirror getAnnotation(AnnotatedConstruct annotatedConstruct, CharSequence annotationClassName) {
List<AnnotationMirror> annotations = getAnnotations(annotatedConstruct, annotationClassName);
return annotations.isEmpty() ? null : annotations.get(0);
}

public static List<AnnotationMirror> getAnnotations(AnnotatedConstruct annotatedConstruct, Class<? extends Annotation> annotationClass) {
static List<AnnotationMirror> getAnnotations(AnnotatedConstruct annotatedConstruct, Class<? extends Annotation> annotationClass) {
return annotationClass == null ? emptyList() : getAnnotations(annotatedConstruct, annotationClass.getTypeName());
}

public static List<AnnotationMirror> getAnnotations(AnnotatedConstruct annotatedConstruct, CharSequence annotationClassName) {
static List<AnnotationMirror> getAnnotations(AnnotatedConstruct annotatedConstruct, CharSequence annotationClassName) {
return findAnnotations(annotatedConstruct, annotation -> isSameType(annotation.getAnnotationType(), annotationClassName));
}

public static List<AnnotationMirror> getAnnotations(AnnotatedConstruct annotatedConstruct) {
static List<AnnotationMirror> getAnnotations(AnnotatedConstruct annotatedConstruct) {
return findAnnotations(annotatedConstruct, EMPTY_PREDICATE_ARRAY);
}

public static List<AnnotationMirror> getAllAnnotations(TypeMirror type) {
static List<AnnotationMirror> getAllAnnotations(TypeMirror type) {
return getAllAnnotations(ofTypeElement(type));
}

public static List<AnnotationMirror> getAllAnnotations(Element element) {
static List<AnnotationMirror> getAllAnnotations(Element element) {
return findAllAnnotations(element, EMPTY_PREDICATE_ARRAY);
}

public static List<AnnotationMirror> getAllAnnotations(TypeMirror type, Class<? extends Annotation> annotationClass) {
static List<AnnotationMirror> getAllAnnotations(TypeMirror type, Class<? extends Annotation> annotationClass) {
return getAllAnnotations(ofTypeElement(type), annotationClass);
}

public static List<AnnotationMirror> getAllAnnotations(Element element, Class<? extends Annotation> annotationClass) {
static List<AnnotationMirror> getAllAnnotations(Element element, Class<? extends Annotation> annotationClass) {
return element == null || annotationClass == null ? emptyList() : getAllAnnotations(element, annotationClass.getTypeName());
}

public static List<AnnotationMirror> getAllAnnotations(TypeMirror type, CharSequence annotationClassName) {
static List<AnnotationMirror> getAllAnnotations(TypeMirror type, CharSequence annotationClassName) {
return getAllAnnotations(ofTypeElement(type), annotationClassName);
}

public static List<AnnotationMirror> getAllAnnotations(Element element, CharSequence annotationClassName) {
static List<AnnotationMirror> getAllAnnotations(Element element, CharSequence annotationClassName) {
return findAllAnnotations(element, annotation -> isSameType(annotation.getAnnotationType(), annotationClassName));
}

public static List<AnnotationMirror> getAllAnnotations(ProcessingEnvironment processingEnv, Type annotatedType) {
static List<AnnotationMirror> getAllAnnotations(ProcessingEnvironment processingEnv, Type annotatedType) {
return findAllAnnotations(processingEnv, annotatedType, EMPTY_PREDICATE_ARRAY);
}

public static List<AnnotationMirror> findAnnotations(AnnotatedConstruct annotatedConstruct, Predicate<? super AnnotationMirror>... annotationFilters) {
static List<AnnotationMirror> findAnnotations(AnnotatedConstruct annotatedConstruct, Predicate<? super AnnotationMirror>... annotationFilters) {

AnnotatedConstruct actualAnnotatedConstruct = annotatedConstruct;

Expand All @@ -111,19 +112,19 @@ public static List<AnnotationMirror> findAnnotations(AnnotatedConstruct annotate
return actualAnnotatedConstruct == null ? emptyList() : filterAll((List<AnnotationMirror>) actualAnnotatedConstruct.getAnnotationMirrors(), annotationFilters);
}

public static List<AnnotationMirror> findAllAnnotations(TypeMirror type, Predicate<? super AnnotationMirror>... annotationFilters) {
static List<AnnotationMirror> findAllAnnotations(TypeMirror type, Predicate<? super AnnotationMirror>... annotationFilters) {
return findAllAnnotations(ofTypeElement(type), annotationFilters);
}

public static List<AnnotationMirror> findAllAnnotations(ProcessingEnvironment processingEnv, Type annotatedType, Predicate<? super AnnotationMirror>... annotationFilters) {
static List<AnnotationMirror> findAllAnnotations(ProcessingEnvironment processingEnv, Type annotatedType, Predicate<? super AnnotationMirror>... annotationFilters) {
return annotatedType == null ? emptyList() : findAllAnnotations(processingEnv, annotatedType.getTypeName(), annotationFilters);
}

public static List<AnnotationMirror> findAllAnnotations(ProcessingEnvironment processingEnv, CharSequence annotatedTypeName, Predicate<? super AnnotationMirror>... annotationFilters) {
static List<AnnotationMirror> findAllAnnotations(ProcessingEnvironment processingEnv, CharSequence annotatedTypeName, Predicate<? super AnnotationMirror>... annotationFilters) {
return findAllAnnotations(TypeUtils.getTypeElement(processingEnv, annotatedTypeName), annotationFilters);
}

public static List<AnnotationMirror> findAllAnnotations(Element element, Predicate<? super AnnotationMirror>... annotationFilters) {
static List<AnnotationMirror> findAllAnnotations(Element element, Predicate<? super AnnotationMirror>... annotationFilters) {

List<AnnotationMirror> allAnnotations = isTypeElement(element) ?
TypeUtils.getAllTypeElements(ofTypeElement(element))
Expand All @@ -135,35 +136,35 @@ public static List<AnnotationMirror> findAllAnnotations(Element element, Predica
return filterAll(allAnnotations, annotationFilters);
}

public static AnnotationMirror findAnnotation(TypeMirror type, Class<? extends Annotation> annotationClass) {
static AnnotationMirror findAnnotation(TypeMirror type, Class<? extends Annotation> annotationClass) {
return annotationClass == null ? null : findAnnotation(type, annotationClass.getTypeName());
}

public static AnnotationMirror findAnnotation(TypeMirror type, CharSequence annotationClassName) {
static AnnotationMirror findAnnotation(TypeMirror type, CharSequence annotationClassName) {
return findAnnotation(ofTypeElement(type), annotationClassName);
}

public static AnnotationMirror findAnnotation(Element element, Class<? extends Annotation> annotationClass) {
static AnnotationMirror findAnnotation(Element element, Class<? extends Annotation> annotationClass) {
return annotationClass == null ? null : findAnnotation(element, annotationClass.getTypeName());
}

public static AnnotationMirror findAnnotation(Element element, CharSequence annotationClassName) {
static AnnotationMirror findAnnotation(Element element, CharSequence annotationClassName) {
return filterFirst(findAllAnnotations(element, annotation -> isSameType(annotation.getAnnotationType(), annotationClassName)));
}

public static AnnotationMirror findMetaAnnotation(Element annotatedConstruct, CharSequence metaAnnotationClassName) {
static AnnotationMirror findMetaAnnotation(Element annotatedConstruct, CharSequence metaAnnotationClassName) {
return annotatedConstruct == null ? null : getAnnotations(annotatedConstruct).stream().map(annotation -> findAnnotation(annotation.getAnnotationType(), metaAnnotationClassName)).filter(Objects::nonNull).findFirst().orElse(null);
}

public static boolean isAnnotationPresent(Element element, CharSequence annotationClassName) {
static boolean isAnnotationPresent(Element element, CharSequence annotationClassName) {
return findAnnotation(element, annotationClassName) != null || findMetaAnnotation(element, annotationClassName) != null;
}

public static <T> T getAttribute(AnnotationMirror annotation, String attributeName) {
static <T> T getAttribute(AnnotationMirror annotation, String attributeName) {
return annotation == null ? null : getAttribute(annotation.getElementValues(), attributeName);
}

public static <T> T getAttribute(Map<? extends ExecutableElement, ? extends AnnotationValue> attributesMap, String attributeName) {
static <T> T getAttribute(Map<? extends ExecutableElement, ? extends AnnotationValue> attributesMap, String attributeName) {
T annotationValue = null;
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : attributesMap.entrySet()) {
ExecutableElement attributeMethod = entry.getKey();
Expand All @@ -172,25 +173,21 @@ public static <T> T getAttribute(Map<? extends ExecutableElement, ? extends Anno
AnnotationValue value = entry.getValue();
if (attributeType instanceof ArrayType) { // array-typed attribute values
ArrayType arrayType = (ArrayType) attributeType;
String componentType = arrayType.getComponentType().toString();
String componentTypeName = arrayType.getComponentType().toString();
ClassLoader classLoader = AnnotationUtils.class.getClassLoader();
List<AnnotationValue> values = (List<AnnotationValue>) value.getValue();
int size = values.size();
try {
Class componentClass = classLoader.loadClass(componentType);
boolean isEnum = componentClass.isEnum();
Object array = Array.newInstance(componentClass, values.size());
for (int i = 0; i < size; i++) {
Object element = values.get(i).getValue();
if (isEnum) {
element = valueOf(componentClass, element.toString());
}
Array.set(array, i, element);
Class componentClass = resolveClass(componentTypeName, classLoader);
boolean isEnum = componentClass.isEnum();
Object array = Array.newInstance(componentClass, values.size());
for (int i = 0; i < size; i++) {
Object element = values.get(i).getValue();
if (isEnum) {
element = valueOf(componentClass, element.toString());
}
annotationValue = (T) array;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
Array.set(array, i, element);
}
annotationValue = (T) array;
} else {
annotationValue = (T) value.getValue();
}
Expand All @@ -200,7 +197,7 @@ public static <T> T getAttribute(Map<? extends ExecutableElement, ? extends Anno
return annotationValue;
}

public static <T> T getValue(AnnotationMirror annotation) {
return (T) getAttribute(annotation, "value");
static <T> T getValue(AnnotationMirror annotation) {
return getAttribute(annotation, "value");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
*/
public class ExecutableElementComparator implements Comparator<ExecutableElement> {

/**
* The singleton instance
*/
public static final ExecutableElementComparator INSTANCE = new ExecutableElementComparator();

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

if (value == 0) { // Step 3
for (int i = 0; i < ps1.size(); i++) {
value = CharSequenceComparator.INSTANCE.compare(ps1.get(i).getSimpleName(), ps2.get(i).getSimpleName());
value = CharSequenceComparator.INSTANCE.compare(ps1.get(i).asType().toString(), ps2.get(i).asType().toString());
if (value != 0) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import static io.microsphere.annotation.processor.util.MemberUtils.getDeclaredMembers;
import static io.microsphere.annotation.processor.util.MemberUtils.hasModifiers;
import static io.microsphere.annotation.processor.util.MemberUtils.matches;
import static io.microsphere.annotation.processor.util.MemberUtils.matchesElementKind;
import static io.microsphere.annotation.processor.util.TypeUtils.getAllDeclaredTypes;
import static io.microsphere.annotation.processor.util.TypeUtils.isEnumType;
import static io.microsphere.lang.function.Predicates.EMPTY_PREDICATE_ARRAY;
Expand Down Expand Up @@ -117,7 +117,7 @@ static boolean isNonStaticField(VariableElement field) {
}

static boolean isField(VariableElement field) {
return matches(field, FIELD) || isEnumMemberField(field);
return matchesElementKind(field, FIELD) || isEnumMemberField(field);
}

static boolean isField(VariableElement field, Modifier... modifiers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;

import static io.microsphere.annotation.processor.util.TypeUtils.getAllDeclaredTypes;
import static io.microsphere.annotation.processor.util.TypeUtils.isSameType;
import static io.microsphere.annotation.processor.util.TypeUtils.ofTypeElement;
import static io.microsphere.collection.CollectionUtils.isEmpty;
import static io.microsphere.lang.function.Predicates.EMPTY_PREDICATE_ARRAY;
import static io.microsphere.lang.function.Predicates.and;
import static io.microsphere.reflect.TypeUtils.getTypeNames;
import static io.microsphere.util.ArrayUtils.isNotEmpty;
import static io.microsphere.util.ArrayUtils.length;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static javax.lang.model.element.Modifier.PUBLIC;
Expand All @@ -42,7 +50,7 @@
*/
public interface MemberUtils {

static boolean matches(Element member, ElementKind kind) {
static boolean matchesElementKind(Element member, ElementKind kind) {
return member == null || kind == null ? false : kind.equals(member.getKind());
}

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

static List<? extends Element> getDeclaredMembers(TypeMirror type) {
TypeElement element = ofTypeElement(type);
return element == null ? emptyList() : element.getEnclosedElements();
return type == null ? emptyList() : getDeclaredMembers(ofTypeElement(type));
}

static List<? extends Element> getDeclaredMembers(TypeElement type) {
return type == null ? emptyList() : findDeclaredMembers(type, EMPTY_PREDICATE_ARRAY);
}

static List<? extends Element> getAllDeclaredMembers(TypeMirror type) {
return getAllDeclaredTypes(type)
return type == null ? emptyList() : findAllDeclaredMembers(ofTypeElement(type), EMPTY_PREDICATE_ARRAY);
}

static List<? extends Element> getAllDeclaredMembers(TypeElement type) {
return type == null ? emptyList() : findAllDeclaredMembers(type, EMPTY_PREDICATE_ARRAY);
}

static List<? extends Element> findDeclaredMembers(TypeMirror type, Predicate<? super Element>... memberFilters) {
return type == null ? emptyList() : findDeclaredMembers(ofTypeElement(type), memberFilters);
}

static List<? extends Element> findDeclaredMembers(TypeElement type, Predicate<? super Element>... memberFilters) {
if (type == null) {
return emptyList();
}
return filterMembers(type.getEnclosedElements(), memberFilters);
}

static List<? extends Element> findAllDeclaredMembers(TypeMirror type, Predicate<? super Element>... memberFilters) {
return type == null ? emptyList() : findAllDeclaredMembers(ofTypeElement(type), memberFilters);
}

static List<? extends Element> findAllDeclaredMembers(TypeElement type, Predicate<? super Element>... memberFilters) {
if (type == null) {
return emptyList();
}
List<? extends Element> declaredMembers = getAllDeclaredTypes(type)
.stream()
.map(MemberUtils::getDeclaredMembers)
.flatMap(Collection::stream)
.collect(toList());
return filterMembers(declaredMembers, memberFilters);
}

static boolean matchParameterTypes(List<? extends VariableElement> parameters, CharSequence... parameterTypes) {
static List<? extends Element> filterMembers(List<? extends Element> members, Predicate<? super Element>... memberFilters) {
if (isEmpty(members)) {
return emptyList();
}
if (isNotEmpty(memberFilters)) {
Predicate predicate = and(memberFilters);
members = (List) members.stream().filter(predicate).collect(toList());
}
return members.isEmpty() ? emptyList() : members;
}

static boolean matchParameterTypes(List<? extends VariableElement> parameters, Type... parameterTypes) {
return parameters == null || parameterTypes == null ? false : matchParameterTypeNames(parameters, getTypeNames(parameterTypes));
}

static boolean matchParameterTypeNames(List<? extends VariableElement> parameters, CharSequence... parameterTypeNames) {
if (parameters == null || parameterTypeNames == null) {
return false;
}

int length = length(parameterTypeNames);
int size = parameters.size();

if (size != parameterTypes.length) {
if (size != length) {
return false;
}

for (int i = 0; i < size; i++) {
VariableElement parameter = parameters.get(i);
if (!Objects.equals(parameter.asType().toString(), parameterTypes[i])) {
if (!isSameType(parameter, parameterTypeNames[i])) {
return false;
}
}
Expand Down
Loading
Loading