如何让Java反映到超类中的字段?不仅仅是实际的类
2022-09-01 16:42:10
我最近更改了我的模式,所以我的类继承自一个超类,问题是我的比较方法生成审计日志,使用Java反射,现在只循环遍历子类的字段,而不是超类,有没有办法获得所有的FIELDS?还是我需要把它扔进超级类.....?
这是我下面的方法:
public static <T> String GenerateChangeLogForEntity(T old, T updated) {
String text = "";
try {
Field[] fields = old.getClass().getDeclaredFields();
if(fields != null) {
BaseController.getLogger().info("Z1 num fields:"+fields.length);
for (Field field : fields) {
if(field.isAnnotationPresent(Column.class)) {
String fieldName = field.getName();
BaseController.getLogger().info(field.getName());
if(field.isAnnotationPresent(Variation.class)) {
Variation v = field.getAnnotation(Variation.class);
fieldName = v.friendlyName();
}
field.setAccessible(true);
if(field.get(old) != null && field.get(updated) != null) {
if(!(field.get(old)).equals(field.get(updated))) {
text += "<p><span class=\"field-name\">"+fieldName+"</span> changed from: <strong>"+GetFriendlyFieldValueForChangeLog(field.get(old))+"</strong> to: <strong>"+GetFriendlyFieldValueForChangeLog(field.get(updated)) + "</strong></p>";
}
}
if(field.get(old) == null && field.get(updated) != null) {
text += "<p><span class=\"field-name\">"+fieldName+"</span> changed from: <strong>empty</strong> to: <strong>"+GetFriendlyFieldValueForChangeLog(field.get(updated)) + "</strong></p>";
}
if(field.get(old) != null && field.get(updated) == null) {
text += "<p><span class=\"field-name\">"+fieldName+"</span> changed from: <strong>"+GetFriendlyFieldValueForChangeLog(field.get(updated))+"</strong> to <strong>empty</strong>" + "</p>";
}
field.setAccessible(false);
}
}
}
} catch(IllegalAccessException e) {}
return text;
}