propertydescriptor是用来干什么的_constructor java

propertydescriptor是用来干什么的_constructor java1、PropertyDescriptor简述PropertyDescriptor对象是位于java.beans包下的工具类,顾名思义为属性描述器,通常我们用于通过反射获取对象方法的时候,下面来看一下常用的用法吧!2、PropertyDescriptor用法(1)、给你一个java对象,你如何生成PropertyDescriptor对象呢?通常,我们会用到…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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对象的信息

{
	"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、实战练习,将一个对象属性值复制到另一个对象中。

// 学生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);
			}

		}

	}

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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

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