Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

Java反射setAccessible(true)安全检查不通过

Bean转Map

public static Map<String, Object> beanToMap(Object object) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();

    Class cls = object.getClass();
    Field[] fields = cls.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        map.put(field.getName(), field.get(object));
    }
    return map;
}

上述代码使用了setAccessible(true)公司安全检查不通过

public static Map<String, Object> beanToMap(Object obj) {
    if (obj == null) {
        return null;
    }
    Map<String, Object> map = new HashMap<String, Object>();
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            // 过滤class属性
            if (!key.equals("class")) {
                // 得到property对应的getter方法
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj);
                map.put(key, value);
            }
        }
    } catch (Exception e) {
        logger.error("transBean2Map Error ", e);
    }
    return map;
}

使用这种方式,安全可靠

上一篇 Jackson实体为NULL或者为空不显示
下一篇 使用Java暴力搜索文件夹下所有包含指定字符串的文本文件