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


相关推荐

  • idea激活激活码【在线破解激活】

    idea激活激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    51
  • 分享SilverLight网站「建议收藏」

    分享SilverLight网站「建议收藏」一、可参考的应用网站1、网址:http://www.preiskombirechner.ch/Preis_Kombi_Rechner.ebs?ActiveID=11912、网址:http://www.

    2022年7月2日
    29
  • pycharm不能输入中文怎么办_电脑无法转换中文输入法

    pycharm不能输入中文怎么办_电脑无法转换中文输入法Pycharm无法输入中文或显示编码错误怎么办?那当然是调整一下啊!!!调整Pycharm中文编码总共三个步骤第一步,打开Pycharm;第二步,调整编码方式;第三步,没有第三步,调整好了不就可以用了嘛!基于第一步太简单,实在没有啥难度,我就不再啰嗦了。我们直接上手第二步,干就完了,奥利给!!!基本上按照上面两幅图的操作就能搞定了,如果还不行的话,请看下面继续…

    2022年8月26日
    9
  • VS2008序列号_苹果序列号过期什么意思

    VS2008序列号_苹果序列号过期什么意思转自:http://lanmaodream.blog.163.com/blog/static/212569007201291874945494/今天打开VS2008突然弹出图如下:为什么会出现上图这种情况呢?嘿嘿,咱使用的是试用版本哦!!!我的是Win7系统,其实各个系统都一样!不过在要输入序列号时,你会发现没有地方可以输入,这时你需要给VS2008打个补丁!!!打

    2022年8月10日
    6
  • AD域的搭建

    AD域的搭建AD域1——肯定是先添加AD域服务器开始—》所有程序—》管理你的服务器—》添加删除程序—》选择添加AD域然后一系列设置,记得搭建了好几次,每次搭建的时候还是要看文档问别人,这次索性就记录每一个步骤,到了检测DNS服务器那里了。2——打开DNS服务管理器,修改一下SOA的属性3——AD的作用是把用户添加到一个组中进行统一管理…

    2022年5月13日
    168
  • pip安装失败如何卸载_ie11安装程序无法继续

    pip安装失败如何卸载_ie11安装程序无法继续今天在安装pandas时提示Youareusingpipversion20.0.0,howeverversion21.0.1isavailable.但是在升级pip时出现了问题出现了黄字警告,提示已经安装pip20.0.0,并没有搜索到新版本但是安装需要新版本支持在一顿没用的查找后发现目录下有两个pip,包括这两个版本,删除后并没有得到改善,再次输入升级命令后,目录下新版本出现,但命令提示符窗口仍旧是相同报错。最后在尝试卸载重装时发现,我的电脑真的安装了两个pip,在输入命

    2022年10月18日
    3

发表回复

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

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