packagexxx.xxx.utils.external.clazz;
importcn.hutool.core.collection.CollUtil;
importlombok.AllArgsConstructor;
importlombok.Getter;
importjava.lang.annotation.Annotation;
importjava.util.ArrayList;
importjava.util.Arrays;
importjava.util.Collections;
importjava.util.List;
importjava.util.stream.Collectors;
importjava.util.stream.Stream;
publicclassClassUtil {
;
privatefinalstaticintLIMIT_FOR_COUNT=100;
privatefinalstaticClass<Object>OBJECT_CLASS=Object.class;
publicstaticList<Class<?>>getSuperClassList(Class<?>clazz) {
try {
if (clazz==null||clazz.isInterface()) {
returnCollections.emptyList();
}
List<Class<?>>resultList=newArrayList<>();
resultList.add(clazz);
Class<?>lastTargetClass=ClassUtil.getSuperClass(clazz);
if (lastTargetClass==null) {
returnresultList;
}
resultList.add(lastTargetClass);
intcurrentCount=0;
while (currentCount<LIMIT_FOR_COUNT) {
++currentCount;
Class<?>targetClass=ClassUtil.getSuperClass(lastTargetClass);
lastTargetClass=targetClass;
if (targetClass==null) {
break;
}
resultList.add(targetClass);
}
returnresultList;
} catch (Exceptionignore) {
}
returnCollections.emptyList();
}
publicstaticList<Class<?>>getSuperInterfaceList(Class<?>clazz) {
if (clazz==null) {
returnCollections.emptyList();
}
List<Class<?>>resultList=newArrayList<>();
if (clazz.isInterface()) {
resultList.add(clazz);
}
if (clazz.getInterfaces().length==0) {
returnresultList;
}
CollUtil.addAll(resultList, clazz.getInterfaces());
Stream.of(clazz.getInterfaces()).forEach(item->CollUtil.addAll(resultList, getSuperInterfaceList(item)));
returnresultList.stream().distinct().collect(Collectors.toList());
}
publicstaticClass<?>getSuperClass(Class<?>clazz) {
if (clazz==null||clazz==OBJECT_CLASS||clazz.isInterface()) {
returnnull;
}
returnclazz.getSuperclass();
}
publicstaticbooleanisInterface(Class<?>clazz) {
if (null==clazz) {
returnfalse;
}
returnclazz.isInterface();
}
publicstaticbooleanequals(Class<?>compareClass, Class<?>beComparedClass) {
if (null==compareClass||null==beComparedClass) {
returnfalse;
}
List<ClassTypeEnum>classTypeEnumList=ClassUtil.getClassType(beComparedClass);
if (classTypeEnumList.isEmpty()) {
returnfalse;
}
if (classTypeEnumList.contains(ClassTypeEnum.ANNOTATION)) {
returnClassUtil.equalsAnnotation(Arrays.asList(compareClass.getAnnotations()), beComparedClass);
}
if (classTypeEnumList.contains(ClassTypeEnum.ENUM)) {
returncompareClass==beComparedClass;
}
if (classTypeEnumList.contains(ClassTypeEnum.INTERFACE)) {
returnClassUtil.equalsInterface(Arrays.asList(compareClass.getInterfaces()), beComparedClass);
}
returnClassUtil.equalsClass(compareClass, beComparedClass);
}
publicstaticbooleanequalsAnnotation(List<Annotation>annotationList, Class<?>beComparedClass) {
if (null==annotationList||annotationList.isEmpty()) {
returnfalse;
}
List<Annotation>subAnnotationList=newArrayList<>();
booleanflag=false;
for (Annotationannotation : annotationList) {
if (annotation.getClass() ==beComparedClass) {
flag=true;
break;
} else {
subAnnotationList.add(annotation);
}
}
if (!flag) {
flag=ClassUtil.equalsAnnotation(subAnnotationList, beComparedClass);
}
returnflag;
}
publicstaticbooleanequalsClass(Class<?>clazz, Class<?>beComparedClass) {
returnnull!=clazz&& (clazz==beComparedClass||ClassUtil.equalsClass(clazz.getSuperclass(), beComparedClass));
}
publicstaticbooleanequalsInterface(List<Class<?>>interfaceList, Class<?>beComparedClass) {
if (null==interfaceList||interfaceList.isEmpty()) {
returnfalse;
}
List<Class<?>>subInterfaceList=newArrayList<>();
booleanflag=false;
for (Class<?>clazz : interfaceList) {
if (clazz==beComparedClass) {
flag=true;
break;
} else {
CollUtil.addAll(subInterfaceList, clazz.getInterfaces());
}
}
if (!flag) {
flag=ClassUtil.equalsInterface(subInterfaceList, beComparedClass);
}
returnflag;
}
publicstaticList<ClassTypeEnum>getClassType(Class<?>clazz) {
List<ClassTypeEnum>classTypeEnumList=newArrayList<>();
if (null==clazz) {
classTypeEnumList.add(ClassTypeEnum.NULL);
returnclassTypeEnumList;
}
if (clazz.isInterface()) {
classTypeEnumList.add(ClassTypeEnum.INTERFACE);
}
if (clazz.isEnum()) {
classTypeEnumList.add(ClassTypeEnum.ENUM);
}
if (clazz.isAnnotation()) {
classTypeEnumList.add(ClassTypeEnum.ANNOTATION);
}
if (clazz.isArray()) {
classTypeEnumList.add(ClassTypeEnum.ARRAY);
}
if (clazz.isSynthetic()) {
classTypeEnumList.add(ClassTypeEnum.SYNTHETIC);
}
if (clazz.isAnonymousClass()) {
classTypeEnumList.add(ClassTypeEnum.SYNTHETIC);
}
returnclassTypeEnumList;
}
@Getter@AllArgsConstructorpublicenumClassTypeEnum {
CLASS,
INTERFACE,
ENUM,
ANNOTATION,
OTHER,
NULL,
ARRAY,
SYNTHETIC,
ANONYMOUS,
;
}
}