我最近改变了我的模式,所以我的类从一个超类继承,问题是我的比较方法生成审计日志,使用 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;
}