获取一个 Object 对象的所有属性

Field[] fields = xxxx.class.getDeclaredFields();  // 获取实体类的所有属性,返回Field数组

fields[0].setAccessible(true );

field.getName();  // 获取字段名称

field.get("fieldName");  // 获取字段值

注:

1、setAccessible:

Set the accessible flag for this object to the indicated boolean value. A value of true indicates that the reflected object should suppress Java language access checking when it is used. A value of false indicates that the reflected object should enforce Java language access checks.

First, if there is a security manager, its checkPermission method is called with a ReflectPermission("suppressAccessChecks") permission.

A SecurityException is raised if flag is true but accessibility of this object may not be changed (for example, if this element object is a Constructor object for the class Class).

A SecurityException is raised if this object is a Constructor object for the class java.lang.Class, and flag is true.

将此对象的可访问标志设置为指示布尔值。true的值表示反射对象在使用Java语言访问检查时应该抑制它。false表示反射的对象应该强制执行Java语言访问检查。

首先,如果有一个安全管理器,那么它的checkPermission方法就会被调用,并获得一个ReflectPermission(“抑制处理”)权限。

如果标记为true,则会抛出SecurityException,但是这个对象的可访问性可能不会被更改(例如,如果这个元素对象是类类的构造函数对象)。 

如果该对象是类java.lang的构造函数对象,则会引发SecurityException。Class和flag是真的。

posted @ 2018-05-10 23:19  kino_熊  阅读(4391)  评论(0编辑  收藏  举报