Java反射获得类的属性和父类的属性

原文地址:https://blog.csdn.net/want_water_fish/article/details/65628052

不多说,直接上代码

try {
    Class<?> clazz = param.getClass();
    for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                continue;
            }
            field.setAccessible(true);
            Object val = field.get(param);
            if (val instanceof String) {
                String value = (String) val;
                if (StringUtils.isNotEmpty(value)) {
                    value = new String(value.getBytes("ISO8859-1"), "utf-8");
                }
                field.set(param, value);
            }
        }
    }
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
阅读更多
换一批

没有更多推荐了,返回首页