public static Map<String, String> getFieldNameAndDescriptionMap(Object entity) throws IllegalAccessException {
Class<?> clazz = entity.getClass();
Field[] fields = clazz.getDeclaredFields(); // 获取所有字段
Map<String, String> fieldMap = new HashMap<>();
for (Field field : fields) {
if (!field.isSynthetic()) { // 不处理合成字段(如编译器自动生成的)
String name = field.getName(); // 获取字段名
Annotation annotation = field.getAnnotation(Description.class); // 获取字段上的 Description注解
if (annotation != null && annotation instanceof Description) {
Descriptiondescription = (Description) annotation;
String value = description.value(); // 获取注解的值
fieldMap.put(name, value); // 将字段名和描述存入 map
}
}
}
return fieldMap;
}
标签:段值,java,Description,获取,fieldMap,field,注解,类字,annotation
From: https://www.cnblogs.com/liucong/p/17994869