前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >propertydescriptor类_Java中break的用法

propertydescriptor类_Java中break的用法

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

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

PropertyDescriptor 类表示 JavaBean 类通过存储器导出一个属性。

构造方法有:

代码语言:javascript
复制
PropertyDescriptor(String propertyName, Class<?> beanClass)

PropertyDescriptor(String propertyName, Class<?> beanClass, String readMethodName, String writeMethodName)

PropertyDescriptor(String propertyName, Method readMethod, Method writeMethod)

常用方法有:

代码语言:javascript
复制
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.

用法:

代码语言:javascript
复制
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.cn/187304.html原文链接:https://javaforall.cn

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

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

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

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

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