zoukankan      html  css  js  c++  java
  • 获取一个 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是真的。

  • 相关阅读:
    搞定Mac Maven
    《数学之美》读书笔记
    Scalable Web Architecture and Distributed Systems
    【程序员】修炼之书
    用平常心,做非常事(字节跳动九周年张一鸣演讲)
    动态规划题目总结
    Spring Boot 启动时,让方法自动执行的 4 种方法!
    MybatisGenerator生成mapper、dao、entity
    HTTPS的GET、POST、PUT、DELETE请求
    SpringBoot使用Slf4j进行日志配置
  • 原文地址:https://www.cnblogs.com/kino/p/9022339.html
Copyright © 2011-2022 走看看