com.neko233.config233.utils.ClassUtilsForConfig233.kt Maven / Gradle / Ivy
package com.neko233.config233.utils
import com.neko233.config233.utils.api.FieldFilter
object ClassUtilsForConfig233 {
/**
* 递归获取 class 的 FieldName to Type
*
* @param clazz 类
* @return , Type>
*/
@JvmStatic
fun getFieldNameToTypeMap(
targetClass: Class<*>?,
filter: FieldFilter,
): Map> {
var clazz = targetClass
val fieldMap: MutableMap> = HashMap()
// 获取所有字段,包括继承的字段
while (clazz != null) {
if (clazz == Any::class.java) {
break
}
val fields = clazz.getDeclaredFields()
for (field in fields) {
if (filter.isNotNeed(field)) {
continue
}
if (field.isSynthetic) {
continue
}
fieldMap[field.name] = field.type
}
clazz = clazz.superclass
}
return fieldMap
}
}