Java 反射 set get
发布日期:2021-06-30 17:38:30
浏览次数:2
分类:技术文章
本文共 3475 字,大约阅读时间需要 11 分钟。
直接上代码,可以用。
package com.xxx.xxx.util;import java.beans.IntrospectionException;import java.beans.PropertyDescriptor;import java.lang.reflect.Field;import java.lang.reflect.Method;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class PropertyUtil { private static Logger logger = LoggerFactory.getLogger("PropertyUtil"); private static final String SET_PREFIX = "set"; private static final String IS_PREFIX = "is"; private static final String GET_PREFIX = "get"; public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) {//根据需求,定制 自己的get和set方法 Method setMethod = null; Method getMethod = null; PropertyDescriptor pd = null; try { Field field = clazz.getDeclaredField(propertyName);// 根据字段名来获取字段 if (field != null) { // 构建方法的后缀 String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); setMethod = clazz.getDeclaredMethod(SET_PREFIX + methodEnd, new Class[] { field.getType() }); // 构建get 方法 getMethod = clazz.getDeclaredMethod(GET_PREFIX + methodEnd, new Class[] {}); // 构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中 pd = new PropertyDescriptor(propertyName, getMethod, setMethod); } } catch (Exception ex) { logger.error("getPropertyDescriptor error,msg:"+ex.getMessage()); } return pd; } public static PropertyDescriptor getPropertyDescriptor2(Class clazz, String propertyName) {//使用 PropertyDescriptor 提供的 get和set方法 try { return new PropertyDescriptor(propertyName, clazz); } catch (IntrospectionException e) { logger.error(" getPropertyDescriptor2 error,msg:"+e.getMessage()); } return null; } public static void setProperty(Object obj, String propertyName, Object value) { Class clazz = obj.getClass();// 获取对象的类型 propertyName = formatProperty(propertyName);//字段中的下划线处理 PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);// 获取 clazz类型中的propertyName的属性描述器 Method setMethod = pd.getWriteMethod();// 从属性描述器中获取 set 方法 try { setMethod.invoke(obj, new Object[] { value });// 调用 set 方法将传入的value值保存属性中去 } catch (Exception e) { logger.error(" setProperty error,msg:"+e.getMessage()); } } public static Object getProperty(Object obj, String propertyName) { Class clazz = obj.getClass();// 获取对象的类型 propertyName = formatProperty(propertyName);//字段中的下划线处理 PropertyDescriptor pd = getPropertyDescriptor(clazz, propertyName);// 获取clazz类型中的propertyName的属性描述器 Method getMethod = pd.getReadMethod();// 从属性描述器中获取 get 方法 Object value = null; try { value = getMethod.invoke(obj, new Object[] {});// 调用方法获取方法的返回值 } catch (Exception e) { logger.error(" getProperty error,msg:"+e.getMessage()); } return value;// 返回值 } public static String formatProperty(String propertyName) { if (propertyName.contains("_")) { String[] tmp = propertyName.split("_"); StringBuilder sb = new StringBuilder(); sb.append(tmp[0]); for (int i = 1; i < tmp.length; i++) { sb.append(tmp[i].substring(0, 1).toUpperCase()).append(tmp[i].substring(1)); } return sb.toString(); }else { return propertyName; } } }
转载地址:https://lemonstone.blog.csdn.net/article/details/83311437 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月30日 02时06分27秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
ACM 2016 沈阳区域赛 E - Counting Cliques (dfs)
2019-04-30
ACM 2017 北京区域赛 J-Pangu and Stones(区间dp)
2019-04-30
HDU - 5643 King's Game (约瑟夫环变式)
2019-04-30
UVA - 1452 Jump (约瑟夫环变式)
2019-04-30
POJ - 3517 And Then There Was One (约瑟夫环变式)
2019-04-30
HDU - 2068 RPG的错排 (错排+组合数)
2019-04-30
CodeForces 591C Median Smoothing(思维 模拟)
2019-04-30
升级yosemite后java出错的解决
2019-04-30
开发人员与测试人员, 道不同也相谋
2019-04-30
Thirdparty之CrashRptProbe, 查询程序奔溃的利器
2019-04-30
基于VTK实现面绘制(SR)和体绘制(VR)的基本代码
2019-04-30
Visual Studio之RelWithDebInfo模式,“被忽视”的编译模式
2019-04-30
提示框的综合使用--UIAlertViewController
2019-04-30
最全的UIAlertController的使用
2019-04-30
CBPeripheralManager学习笔记
2019-04-30
CLLocationManager定位服务
2019-04-30
iOS 开发如何获取当前的应用版本号
2019-04-30