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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • idea2021.2.3激活码 3月最新注册码

    idea2021.2.3激活码 3月最新注册码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月15日
    165
  • WORD域代码详解[通俗易懂]

    WORD域代码详解[通俗易懂]来自:百度文库WORD域代码详解我们在word中编辑文档时,有时会经常遇到要求输入数学公式的情况。虽然简单的加、减、乘、除等运算都可在键盘上直接输入,但遇到复杂的数学公式:如积分、开方、求和等符号时,就只能利微软的office套件中所带的工具”公式编辑器”来完成,但是在office套件的典型安装的过程中,并没有选择这个工具,那么有没有办法可以在word中直接输入这些符号呢?答案是肯定的,下

    2022年6月14日
    38
  • android 测试用例模板下载,app测试用例模板.doc

    android 测试用例模板下载,app测试用例模板.docapp测试用例模板APP基本测试用例个人首页1.我的页面2.个人信息页面3.个性标签页面4.TA的页面消息页面消息页面发布商品和图片发布商品分享图片买买买页面买买买页面一级分类页面买手热荐品类二级分类页面侧边栏页面购物车页面我的钱包页面一、编号条件步骤预期结果实际结果1打开我的页面?出现我的信息(头像、昵称、签名、关注数、粉丝数、入手、出手)、中部出现切换我发表的与我喜欢的tab、下部列表出现内容…

    2022年7月17日
    12
  • Powerpoint中VBA编程技巧

    Powerpoint中VBA编程技巧转自:http://www.pinlue.com/article/2019/03/3115/108521098826.html

    2022年5月1日
    44
  • 金山词霸2009sp3 (解决字典消失、屏幕不能取词问题)

    金山词霸2009sp3 (解决字典消失、屏幕不能取词问题)姓名:杨正祎(阿一)互联网工作者,专注于互联网项目开发与开发,擅长于符合Web标准的页面设计制作,互联网项目管理。现就职于上海沪江网http://www.cnblogs.com/JustinYoung/archive/2009/05/10/jinshanciba2009sp3.html   金山词霸2009sp3激活成功教程版下载(解决字典消失、屏幕不能取词问题)

    2022年7月15日
    14

发表回复

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

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