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


相关推荐

  • 如何上传代码到github?

    如何上传代码到github?github是什么?github是Git远程仓库。github是一个基于git的代码托管平台Git是什么:Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。如何上传代码到github?参考:https://www.runoob.com/git/git-remote-repo.html在githbu上注册账号注册账号 创建一个项目 获得…

    2025年6月6日
    0
  • xml实用教程_如何学唱歌入门教程

    xml实用教程_如何学唱歌入门教程前言:“与其每天担心未来,不如努力现在。别对自己丧失信心,成长的路上,只有奋斗才能给你最大的安全感。”你好,我是梦阳辰!下面和我一起学习XML吧!01.XML概述1.什么是XML?XML(eXt

    2022年8月1日
    5
  • 两种方法在VMware中安装VMware Tools

    两种方法在VMware中安装VMware ToolsVMwareTools的主要作用是虚拟硬件的驱动,鼠标的无缝移出移入,剪贴板共享,共享主机上的文件夹等功能。 方法一1.打开文件夹-Ubuntu17.10amd64(根据自己版本来看)。2.在VMwareWorkstation主界面点击菜单“虚拟机”。会弹出提示框,点击是。3.保持网络状态,等待后台下载,下载后会出现一个压缩包。在下载过程中可能会出现DVD图标…

    2022年5月26日
    33
  • android registerreceiver() 参数,android – 如何使用registerReceiver方法?

    android registerreceiver() 参数,android – 如何使用registerReceiver方法?我想使用动态注册的BroadcastReceiver有一个Activity的引用,所以它可以修改其UI。我使用Context.registerReceiver()方法,但接收器的onReceive()方法从未调用。这里是显示问题的示例代码:packagecom.example;importandroid.app.Activity;importandroid.app.IntentService…

    2022年9月12日
    0
  • editormd_editorialize

    editormd_editorialize使用editormd图片上传无法返回路径问题内部再生成json时会自动添加一些没必要的多余字段在此处添加一行只取body中pre第一个数据

    2022年10月22日
    0
  • fileinput基本使用[通俗易懂]

    fileinput基本使用[通俗易懂]新增$(“#attachmentsFile”).fileinput({theme:”fa”,showPreview:true,//是否显示预览hideThumbnailContent:true,//是否在缩略图中隐藏预览内容(图像,pdf内容,文本内容等)。showUpload:false,//隐藏上传按钮…

    2022年6月7日
    35

发表回复

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

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