com.tukeof.common.util.ReflectUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-core Show documentation
Show all versions of common-core Show documentation
a common and useful pure java library
The newest version!
package com.tukeof.common.util;
import com.tukeof.common.annotation.NotNull;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ReflectUtil {
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
private static final String[] ANNOTATION_ALIAS_METHODS = new String[]{
"value", "name"
};
public static void retrieveSuperClasses(Class> clazz, List classList) {
Class superClazz = clazz.getSuperclass();
classList.add(superClazz);
if (!superClazz.equals(Object.class)) {
retrieveSuperClasses(superClazz, classList);
}
}
public static void retrieveMethods(Class> clazz, List methodList) {
Method[] methods = clazz.getDeclaredMethods();
methodList.addAll(Arrays.asList(methods));
Class superClazz = clazz.getSuperclass();
if (!superClazz.equals(Object.class)) {
retrieveMethods(superClazz, methodList);
}
}
public static void retrieveFields(Class> clazz, List fieldList) {
Field[] fields = clazz.getDeclaredFields();
fieldList.addAll(Arrays.asList(fields));
Class superClazz = clazz.getSuperclass();
if (!superClazz.equals(Object.class)) {
retrieveFields(superClazz, fieldList);
}
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
public static void retrieveFields(Class> clazz, Map fieldMap, Class... annotationClasses) {
Field[] fields = clazz.getDeclaredFields();
Arrays.stream(fields)
.forEach((field) -> {
String name = retrieveFieldName(field, annotationClasses);
fieldMap.put(name, field);
});
Class superClazz = clazz.getSuperclass();
if (!superClazz.equals(Object.class)) {
retrieveFields(superClazz, fieldMap, annotationClasses);
}
}
// exclude static, transient, volatile
public static void retrieveFieldNames(Class> clazz, List fieldNameList, Class... annotationClasses) {
Field[] fields = clazz.getDeclaredFields();
fieldNameList.addAll(Arrays.stream(fields)
.filter(field -> {
int modifiers = field.getModifiers();
return !Modifier.isStatic(modifiers)
&& !Modifier.isTransient(modifiers)
&& !Modifier.isVolatile(modifiers);
})
.map(field -> retrieveFieldName(field, annotationClasses))
.collect(Collectors.toList()));
Class superClazz = clazz.getSuperclass();
if (!superClazz.equals(Object.class)) {
retrieveFieldNames(superClazz, fieldNameList, annotationClasses);
}
}
// not excluding strategy
public static String retrieveFieldName(Field field, Class... annotationClasses) {
String fieldName = field.getName();
Annotation[] annotations = field.getAnnotations();
if (annotations == null || annotations.length == 0
|| annotationClasses == null || annotationClasses.length == 0) {
return fieldName;
}
List aliasAnnotationList = Arrays.asList(annotationClasses);
List filteredAnnotationList = Arrays.stream(annotations)
.filter(it -> aliasAnnotationList.contains(it.getClass()))
.collect(Collectors.toList());
if (ObjectUtil.isNotEmpty(filteredAnnotationList)) {
for (Annotation annotation : filteredAnnotationList) {
String alias = retrieveAnnotationAlias(annotation);
if (alias != null) {
return alias;
}
}
}
return fieldName;
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
public static String retrieveAnnotationAlias(Annotation annotation) {
for (String name : ANNOTATION_ALIAS_METHODS) {
try {
return annotation.getClass().getDeclaredMethod(name).invoke(annotation).toString();
} catch (Exception ignored) {
}
}
return null;
}
public static boolean exclude(
Field field, int excludeModifiers, Class... excludeAnnotation) {
boolean exclude = hasAnnotation(field, excludeAnnotation);
int modifiers = field.getModifiers();
return exclude || (modifiers & excludeModifiers) > 0;
}
public static boolean excludeStaticAndExtra(
Field field, int extraExcludeModifiers, Class... excludeAnnotation) {
int excludeModifiers = extraExcludeModifiers | Modifier.STATIC;
return exclude(field, excludeModifiers, excludeAnnotation);
}
@SuppressWarnings("unchecked")
public static boolean hasAnnotation(Field field, Class... annotationClasses) {
if (ObjectUtil.isEmpty(annotationClasses)) return false;
Annotation[] annotations = field.getAnnotations();
if (ObjectUtil.isEmpty(annotations)) return false;
for (Class extends Annotation> annotationClass : annotationClasses) {
Annotation annotation = field.getAnnotation(annotationClass);
if (annotation != null) return true;
}
return false;
}
public static boolean anyAssignable(Class> clazz, Class... classes) {
if (ObjectUtil.isEmpty(classes)) return false;
for (Class> i : classes) {
// i is same as clazz or superclass
if (i.isAssignableFrom(clazz)) {
return true;
}
}
return false;
}
public static boolean anyModifiers(int modifiers, int... modifierses) {
if (modifierses == null || modifierses.length == 0) return false;
for (int m : modifierses) {
boolean hasModifiers = (m & modifiers) != 0;
if (hasModifiers) return true;
}
return false;
}
public static boolean isFinalOrStaticField(Field field) {
int modifiers = field.getModifiers();
return Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers);
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
public static boolean isBoxedClass(@NotNull Class> clazz) {
return clazz.equals(Integer.class) ||
clazz.equals(Long.class) ||
clazz.equals(Double.class) ||
clazz.equals(Boolean.class) ||
clazz.equals(Character.class) ||
clazz.equals(Float.class) ||
clazz.equals(Byte.class) ||
clazz.equals(Short.class);
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
public static String[] getParameterNames(Method method) {
Parameter[] parameters = method.getParameters();
int len = parameters.length;
String[] parameterNames = new String[len];
for (int i = 0; i < len; i++) {
Parameter parameter = parameters[i];
if (parameter.isNamePresent()) {
parameterNames[i] = parameter.getName();
}
}
return parameterNames;
}
// ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
public static Object getFirstFieldValue(T bean) {
Class> clazz = bean.getClass();
Field[] fields = clazz.getFields();
if (ObjectUtil.isEmpty(fields)) return null;
Field field = fields[0];
field.setAccessible(true);
try {
return field.get(bean);
} catch (IllegalAccessException e) {
return null;
}
}
}