propertydescriptor类_Java中break的用法

propertydescriptor类_Java中break的用法PropertyDescriptor类表示JavaBean类通过存储器导出一个属性。构造方法有:PropertyDescriptor(StringpropertyName,ClassbeanClass)PropertyDescriptor(StringpropertyName,ClassbeanClass,StringreadMethodName,Stringw

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

PropertyDescriptor 类表示 JavaBean 类通过存储器导出一个属性。

构造方法有:

PropertyDescriptor(String propertyName, Class<?> beanClass)

PropertyDescriptor(String propertyName, Class<?> beanClass, String readMethodName, String writeMethodName)

PropertyDescriptor(String propertyName, Method readMethod, Method writeMethod)

常用方法有:

Class<?> getPropertyType() // 获取属性的java类型对象
Method getReadMethod() // 获得用于读取属性值的方法
Method getWriteMethod() // 获得用于写入属性值的方法
void setReadMethod(Method readMethod) // Sets the method that should be used to read the property value.
void setWriteMethod(Method writeMethod) //Sets the method that should be used to write the property value.

用法:

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/** * Created by zhuqiuhui on 2017/11/15. */
public class PropertyUtil { 
   

    public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) {
        StringBuffer sb = new StringBuffer();//构建一个可变字符串用来构建方法名称
        Method setMethod = null;
        Method getMethod = null;
        PropertyDescriptor pd = null;
        try {
            Field f = clazz.getDeclaredField(propertyName);//根据字段名来获取字段
            if (f != null) {
                //构建方法的后缀
                String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
                sb.append("set" + methodEnd);
                //构建set方法
                setMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{f.getType()});
                sb.delete(0, sb.length());
                sb.append("get" + methodEnd);
                //构建get 方法
                getMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{});
                //构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中
                pd = new PropertyDescriptor(propertyName, getMethod, setMethod);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return pd;
    }

    public static void setProperty(Object obj, String propertyName, Object value) {
        Class clazz = obj.getClass();//获取对象的类型
        PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);//获取 clazz 类型中的 propertyName 的属性描述器
        Method setMethod = pd.getWriteMethod();//从属性描述器中获取 set 方法
        try {
            setMethod.invoke(obj, new Object[]{value});//调用 set 方法将传入的value值保存属性中去
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object getProperty(Object obj, String propertyName) {
        Class clazz = obj.getClass();//获取对象的类型
        PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);//获取 clazz 类型中的 propertyName 的属性描述器
        Method getMethod = pd.getReadMethod();//从属性描述器中获取 get 方法
        Object value = null;
        try {
            value = getMethod.invoke(clazz, new Object[]{});//调用方法获取方法的返回值
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }


    public static void main(String[] args) throws Exception {
        Class clazz = Class.forName("TaskProvidePropsList");//这里的类名是全名。。有包的话要加上包名
        Object obj = clazz.newInstance();
        Field[] fields = clazz.getDeclaredFields();
        //写数据
        for (Field f : fields) {
            PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz);
            Method wM = pd.getWriteMethod();//获得写方法
            wM.invoke(obj, 2);//因为知道是int类型的属性,所以传个int过去就是了。。实际情况中需要判断下他的参数类型
        }
        //读数据
        for (Field f : fields) {
            PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz);
            Method rM = pd.getReadMethod();//获得读方法
            Integer num = (Integer) rM.invoke(obj);//因为知道是int类型的属性,所以转换成integer就是了。。也可以不转换直接打印
            System.out.println(num);
        }
    }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/187304.html原文链接:https://javaforall.net

(0)
上一篇 2022年9月30日 下午10:46
下一篇 2022年9月30日 下午10:46


相关推荐

  • 5大优秀黑客必逛技术网站

    5大优秀黑客必逛技术网站5大优秀黑客必逛技术网站HackForums最理想的黑客技术学习技术根据地,也适用于开发人员游戏开发者,程序员,图形设计师以及网络营销人士HackThisSite提供合法而安全的网络安全资源,可以通过·各类挑战题目测试自己的黑客技能EnilZone一个专门面向黑科群体的论坛,其中也涉及科学,编程以及艺术等领域的内容Exploit-DB提供一整套庞大的归档体…

    2022年7月15日
    26
  • 爆火的OpenClaw到底是个啥?一文看透这只“AI龙虾”的真面目与暗坑

    爆火的OpenClaw到底是个啥?一文看透这只“AI龙虾”的真面目与暗坑

    2026年3月15日
    3
  • Oracle 904_oracle01017

    Oracle 904_oracle01017SQL>desceq_admin.its_earthquake_recover;名前NULL?型—————————————————————————–ORG_INF…

    2026年2月2日
    6
  • 什么是MVC软件架构模式_mvc架构的设计思路

    什么是MVC软件架构模式_mvc架构的设计思路缘起:作为程序员,很容易天天被业务追逐着,抽不开时间修炼。有一天突然停了一下,忽地就会有一种怅然的感觉,过去的那些日子我学到了什么?有人很认真地说自己有10年经验,有人笑说你不过是一年经验用了10年而已。

    2022年10月10日
    5
  • 数据库读写分离的理解

    数据库读写分离的理解读写分离 基本的原理是让主数据库处理事务性增 改 删操作 INSERT UPDATE DELETE 而从数据库处理 SELECT 查询操作 数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库 nbsp nbsp nbsp nbsp nbsp nbsp 为什么要分库 分表 读写分 nbsp nbsp nbsp nbsp nbsp nbsp 单表的数据量限制 当单表数据量到一定条数之后数据库性能会显著下降 数据多了之后 对数据库的读 写就会很多 分库减少单台数据库的压力 接触过几个

    2026年3月17日
    1
  • 讯飞星火如何删除生成的作品

    讯飞星火如何删除生成的作品

    2026年3月14日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号