通过PropertyDescriptor反射进行字段名值的获取及设置
2017-08-30 15:58
351 查看
/**
* 根据属性名获取对应的value
* @param fieldName
* @param obj
* @return
* @throws Exception
*/
private static String getValueByFiled(String fieldName,Object obj)throws Exception{
//属性扫描器
PropertyDescriptor pd = new PropertyDescriptor(fieldName, obj.getClass());
//从属性描述器中获取 get 方法
Method method = pd.getReadMethod();
//结果
Object value = method.invoke(obj);
//执行方法并返回结果
return value==null?"":String.valueOf(value);
}
下面是比较完整的
参考:http://blog.csdn.net/qq_34120041/article/details/53665526
* 根据属性名获取对应的value
* @param fieldName
* @param obj
* @return
* @throws Exception
*/
private static String getValueByFiled(String fieldName,Object obj)throws Exception{
//属性扫描器
PropertyDescriptor pd = new PropertyDescriptor(fieldName, obj.getClass());
//从属性描述器中获取 get 方法
Method method = pd.getReadMethod();
//结果
Object value = method.invoke(obj);
//执行方法并返回结果
return value==null?"":String.valueOf(value);
}
下面是比较完整的
public class PropertyUtil { @SuppressWarnings("unchecked") 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方法 //构建get 方法 getMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ }); //构建一个属性描述器 把对应属性 propertyName 的 get 和 set 方法保存到属性描述器中 pd = new PropertyDescriptor(propertyName, getMethod, setMethod); } } catch (Exception ex) { ex.printStackTrace(); } return pd; } //对obj对象的指定字段进行值设置 @SuppressWarnings("unchecked") 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(); } } //根据属性字段及对象获取对应的属性值 @SuppressWarnings("unchecked") 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;//返回值 } } //调用 import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class ReflectTest { 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); } } }
参考:http://blog.csdn.net/qq_34120041/article/details/53665526
相关文章推荐
- 通过SiteMapDataSource动态获取SiteMap文件进行权限设置
- Java - 如何通过反射获取和设置对象私有字段的值?
- 反射第五步 : 通过反射获取某一个类的字段值,并设置字段值 (很少用)
- 如何通过反射获取和设置对象私有字段的值?
- 演示了通过存储过程来获取某个字段的值,并对该字段值进行了判断
- 通过反射获取和设置对象私有字段的值?
- 获取通过sql查询数据库的表,字段,主键,自增,字段类型等信息。
- (转)css 相对定位 绝对定位 浮动 CSS 相对定位 相对定位是一个非常容易掌握的概念。如果对一个元素进行相对定位,它将出现在它所在的位置上。然后,可以通过设置垂直或水平位置,让这个元素“相对于”
- Git通过设置代理进行访问
- spring 通过@Value 获取properties文件中设置了属性 ,与@Value # 和$的区别
- android view的宽高通过代码进行设置---dp和sp之间互转
- 通过java反射获取任意对象的字段名及字段值
- 等分View,对角线排列。通过OnMeasure,设置控件宽高,通过OnLayout进行排列
- ruby进行http请求头设置及获取
- 通过设置chrome浏览器解决跨域问题,在本地进行开发工作
- 通过java反射获取任意对象的字段名及字段值
- 通过反射获取类属性字段的个数
- 通过 Visual Studio 对 SQL Server 中的存储过程设置断点并进入存储过程对其进行调试
- 移动项目开发笔记(服务器端获取客户端只读文本框通过JavaScript设置的值)
- 通过Javascript 对 CSS属性的设置 与 获取