org.zodiac.fastorm.rdb.utils.AnnotationUtils Maven / Gradle / Ivy
The newest version!
package org.zodiac.fastorm.rdb.utils;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import org.zodiac.sdk.toolkit.util.PropertiesToolUtil;
@Deprecated
public abstract class AnnotationUtils {
private AnnotationUtils() {
super();
}
public static Optional getFiledByDescriptor(Class> type, PropertyDescriptor descriptor) {
String name = descriptor.getName();
/*Retrieve properties.*/
while (true) {
try {
try {
return Optional
.of(
type.getDeclaredField(name)
);
} catch (NoSuchFieldException e1) {
char[] arr = name.toCharArray();
if (Character.isUpperCase(arr[0])) {
arr[0] = Character.toLowerCase(arr[0]);
name = new String(arr);
continue;
}
throw e1;
}
} catch (NoSuchFieldException e) {
type = type.getSuperclass();
if (type == null || type == Object.class) {
break;
}
}
}
return Optional.empty();
}
public static Set getAnnotations(Class> entityClass, PropertyDescriptor descriptor) {
Set annotations = new LinkedHashSet<>();
Set> types = new LinkedHashSet<>();
Consumer annoConsumer = (ann) -> {
for (Annotation annotation : ann) {
if (!types.contains(annotation.getClass())) {
annotations.add(annotation);
}
types.add(annotation.annotationType());
}
};
/*First, obtain the method.*/
Method read = descriptor.getReadMethod(),
write = descriptor.getWriteMethod();
if (read != null) {
annoConsumer.accept(read.getAnnotations());
}
if (write != null) {
annoConsumer.accept(write.getAnnotations());
}
PropertiesToolUtil.getFiledByDescriptor(entityClass, descriptor).map(Field::getAnnotations)
.ifPresent(annoConsumer);
return annotations;
}
public static T getAnnotation(Class> entityClass, PropertyDescriptor descriptor, Class type) {
T ann = null;
if (descriptor == null) {
return null;
}
/*First, obtain the method.*/
Method read = descriptor.getReadMethod(),
write = descriptor.getWriteMethod();
if (read != null) {
ann = getAnnotation(read, type);
}
if (null == ann && write != null) {
ann = getAnnotation(write, type);
}
/*Retrieve properties.*/
if (ann == null) {
ann = PropertiesToolUtil.getFiledByDescriptor(entityClass, descriptor).map(f -> f.getAnnotation(type))
.orElse(null);
}
return ann;
}
public static T getAnnotation(Class> clazz, Class annotation) {
T ann = clazz.getAnnotation(annotation);
while (ann == null) {
clazz = clazz.getSuperclass();
if (clazz == null || clazz == Object.class) {
break;
}
ann = clazz.getAnnotation(annotation);
}
return ann;
}
public static T getAnnotation(Method method, Class annotation) {
T ann = method.getAnnotation(annotation);
Class> clazz = method.getDeclaringClass();
while (ann == null) {
clazz = clazz.getSuperclass();
if (clazz == null || clazz == Object.class) {
break;
}
try {
/*The method of the parent class.*/
Method suMethod = clazz.getMethod(method.getName(), method.getParameterTypes());
ann = suMethod.getAnnotation(annotation);
} catch (NoSuchMethodException e) {
return null;
}
}
return ann;
}
}