前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >propertydescriptor是用来干什么的_constructor java

propertydescriptor是用来干什么的_constructor java

作者头像
全栈程序员站长
发布2022-11-15 11:22:55
7770
发布2022-11-15 11:22:55
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

1、PropertyDescriptor简述

PropertyDescriptor对象是位于java.beans包下的工具类,顾名思义为属性描述器,通常我们用于通过反射获取对象方法的时候,下面来看一下常用的用法吧!

2、PropertyDescriptor用法

(1)、给你一个java对象,你如何生成PropertyDescriptor对象呢?

通常,我们会用到PropertyUtilsBean对象(位于java.beans包下),代码如下:student为我们已经获取到的对象,此时获取到studentDescriptors数组,打印下可以看到获取到的对象的相关信息。

PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean(); PropertyDescriptor[] studentDescriptors = propertyUtilsBean.getPropertyDescriptors(student); System.out.println(“studentDescriptors:”+JSON.toJSONString(studentDescriptors));

这里我们看一下获取到的一个PropertyDescriptor对象的信息

代码语言:javascript
复制
{
	"bound": false,
	"constrained": false,
	"displayName": "id",
	"expert": false,
	"hidden": false,
	"name": "id",
	"preferred": false,
	"propertyType": "java.lang.String",
	"readMethod": {
		"accessible": false,
		"annotatedExceptionTypes": [],
		"annotatedParameterTypes": [],
		"annotatedReceiverType": {
			"annotations": [],
			"declaredAnnotations": [],
			"type": "com.iflytek.zbg.zwfw.bog.situation.common.entity.BaseEntity"
		},
		"annotatedReturnType": {
			"annotations": [],
			"declaredAnnotations": [],
			"type": "java.lang.String"
		},
		"annotations": [],
		"bridge": false,
		"declaringClass": "com.iflytek.zbg.zwfw.bog.situation.common.entity.BaseEntity",
		"default": false,
		"exceptionTypes": [],
		"genericExceptionTypes": [],
		"genericParameterTypes": [],
		"genericReturnType": "java.lang.String",
		"modifiers": 1,
		"name": "getId",
		"parameterAnnotations": [],
		"parameterCount": 0,
		"parameterTypes": [],
		"returnType": "java.lang.String",
		"synthetic": false,
		"typeParameters": [],
		"varArgs": false
	},
	"shortDescription": "id",
	"writeMethod": {
		"accessible": false,
		"annotatedExceptionTypes": [],
		"annotatedParameterTypes": [{
			"annotations": [],
			"declaredAnnotations": [],
			"type": "java.lang.String"
		}],
		"annotatedReceiverType": {
			"annotations": [],
			"declaredAnnotations": [],
			"type": "com.iflytek.zbg.zwfw.bog.situation.common.entity.BaseEntity"
		},
		"annotatedReturnType": {
			"annotations": [],
			"declaredAnnotations": [],
			"type": "void"
		},
		"annotations": [],
		"bridge": false,
		"declaringClass": "com.iflytek.zbg.zwfw.bog.situation.common.entity.BaseEntity",
		"default": false,
		"exceptionTypes": [],
		"genericExceptionTypes": [],
		"genericParameterTypes": ["java.lang.String"],
		"genericReturnType": "void",
		"modifiers": 1,
		"name": "setId",
		"parameterAnnotations": [
			[]
		],
		"parameterCount": 1,
		"parameterTypes": ["java.lang.String"],
		"returnType": "void",
		"synthetic": false,
		"typeParameters": [],
		"varArgs": false
	}
}

以上可以看到我们已经获取到了Student对象的id属性(包含set和get方法)相关的全部信息。

(2)、几个常用的方法

prop.getName() 获取属性编码

prop.getReadMethod() 获取get方法

prop.getWriteMethod() 获取set方法

prop.getPropertyType() 获取属性的定义类型

(3)、当我们获取到方法了以后,如何获取之前对象这个属性的值呢?

通过get方法进行对象反射获取到值,代码如下

Method method = prop.getReadMethod(); //获取到get方法 Object val = method.invoke(student); //调用student的get方法获取内容 System.out.println(“val:”+JSON.toJSONString(val));

(4)、如果需要对student的属性赋值其他的内容,怎么处理?

获取set方法,首先我们需要确定set方法存在,且赋值的内容存在,代码如下:

prop.getWriteMethod().invoke(student, val1); //student调用set方法将val1值设置到prop固定的属性中

3、实战练习,将一个对象属性值复制到另一个对象中。

代码语言:javascript
复制
// 学生1为获取到有数据的对象,学生2为new出来的无值对象
// 这里student1和student2并非要求为同一个对象,属性字段相同即可
public static void copyCommonProperties(Object student1, Object student2) {
		if (student1 == null || student2 == null) {
			return;
		}
		// 获取student1的所有属性及方法
		PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
		PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(student1);

		for (int i = 0; i < descriptors.length; i++) {
		    // 获取某一个属性的全部信息
			PropertyDescriptor propItem = descriptors[i];
			// 过滤setclass/getclass属性
			if ("class".equals(propItem.getName())) {
				continue;
			}
			try {
			    // 通过get方法获取对应属性的值
				Method method = propItem.getReadMethod();
				Object val = method.invoke(student1);
				// 如果是空,不做处理
				if (null == val) {
					continue;
				}
				// 值复制。调用写方法,设置值
				// 获取student2的当前属性propItem.getName()的信息
				PropertyDescriptor prop = propertyUtilsBean.getPropertyDescriptor(student2, propItem.getName());
				if (null != prop && prop.getWriteMethod() != null) {
					prop.getWriteMethod().invoke(student2, val);
				}
			} catch (Exception e) {
				logger.error("复制出错 ,student1 prop : " + propItem.getDisplayName() + " student1 class: " + student1.getClass()
						+ ";student2 type :" + student2.getClass(), e);
			}

		}

	}

学海无涯苦作舟,遇到一个,总结一个吧!以上仅为自己探讨学习的理解,如有问题,欢迎大家的指导!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月1日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档