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


相关推荐

  • 十六、状态模式—用类表示状态 #和设计模式一起旅行#

    人有悲欢离合,月有阴晴圆缺!故事背景白天、黑夜是不同的状态,水蒸气、冰是水不同的状态,大千的世界,不同的各种东西会有很多种状态,本篇要介绍的就是 “用类来表示状态”,用类表示状态后,我们就可以切换类来方便地改变对象的状态。故事主角状态模式:允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类。在状态模式结构中包括几个角色: – Conte…

    2022年2月27日
    36
  • 云计算、大数据和物联网三者之间有哪些区别和联系?_云计算物联网大数据的区别

    云计算、大数据和物联网三者之间有哪些区别和联系?_云计算物联网大数据的区别云计算、大数据和物联网代表了IT领域最新的技术发展趋势,三者既有区别又有联系。云计算最初主要包括了两类含义:一类是以谷歌的GFS和MapReduce为代表的大规模分布式并行计算技术;另一类是以亚马逊的虚拟机和对象存储为代表的“按需租用”的商业模式。随着大数据概念的提出,云计算中的分布式计算技术开始更多地被列入大数据技术,而人们提到云计算时,更多指的是底层基础IT资源的整合优化以及以服务的方式提供…

    2022年10月7日
    4
  • 浮动 IP

    浮动 IP在做双机的时候 设定一个 IP 通过访问这个 IP 具体到后台哪个机器 由系统指定 浮动 IP 是随资源一起走的 就是由软件根据情况把该 IP 设置在某台机器上 对外提供服务 为了避免因为一台机器 down 而导致不能对外提供服务 使得业务中断

    2025年8月26日
    5
  • Eclipse(MyEclipse)使用技巧——改动凝视字体大小

    Eclipse(MyEclipse)使用技巧——改动凝视字体大小

    2021年11月30日
    47
  • TLSF算法分析

    TLSF算法分析注:本文的大部分内容摘录自论文《TLSF:aNewDynamicMemoryAllocatorforReal-TimeSystems》,可以通过“科学上网”访问如下链接阅读原文:http://www.gii.upv.es/tlsf/files/ecrts04_tlsf.pdf。什么是TLSFTLSF是TwoLevelSegregatedFitmemoryal

    2022年6月30日
    29
  • NVIC中断管理

    NVIC中断管理使用中断之前,第一步要了解的是其优先级管理,下面总结一下STM32NVIC的中断优先级管理。(正点原子系列)以smt32f103系列为例,其具有16个内核中断和60个可屏蔽中断。下面介绍其库函数的开发:MDK内与NVIC相关的寄存器包含在结构体中,通过创建结构体,配置其内部组成员也就是寄存器,来配置NVIC各个配置。先介绍其结构体的内部内容:在结构体内有介绍!中断配置寄存器[]内的…

    2022年5月27日
    70

发表回复

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

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