如何让 Java 反射以发现超类中的字段?不仅仅是真正的课堂 [英] How to get Java reflect to spot fields in the super class? not just the actual class

查看:21
本文介绍了如何让 Java 反射以发现超类中的字段?不仅仅是真正的课堂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近稍微改变了我的模式,所以我的类继承自一个超类,问题是我的比较方法使用 Java 反射生成审计日志,现在只循环遍历子类的字段,而不是超类,有没有办法获得所有的领域?还是我需要将其转换为超类.....?

I've recently changed my schema a bit so my classes inherit from a super class, problem is my comparison method which generates an audit log, using Java reflect, is now only looping through the fields of the child class, not the superclass, is there a way to get all the FIELDS? or do I need to cast it into the super class.....?

下面是我的方法:

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;
    }

推荐答案

你可以使用 this.getClass().getSuperClass() 直到这个 getSuperClass() 方法返回null 获取父字段.

You can use this.getClass().getSuperClass() until this getSuperClass() method returns null to get the parent fields.

所以,最好的办法是分解你的代码.实现一个将 Field 列表作为参数并在其中执行逻辑部分的方法,以及一个通过 while(superClass != null) 搜索字段的主要方法循环.

So, the best would be that you factorize your code. Implement one method that takes a list of Field as parameter and do your logical part within it, and a main method that search for fields through a while(superClass != null) loop.

这篇关于如何让 Java 反射以发现超类中的字段?不仅仅是真正的课堂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆