獲取一個 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是真的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。