Java通過PropertyDescriptor反射調(diào)用set和get方法
更新時間:2017年03月01日 14:41:54 作者:周XXXX
這篇文章主要為大家詳細(xì)介紹了Java通過PropertyDescriptor反射調(diào)用set和get方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了PropertyDescriptor反射調(diào)用set和get方法,供大家參考,具體內(nèi)容如下
第一段:
package com.zhoushun; import java.lang.reflect.Method; import java.lang.reflect.Field; import java.beans.PropertyDescriptor; public class PropertyUtil { @SuppressWarnings("unchecked") public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) { StringBuffer sb = new StringBuffer();//構(gòu)建一個可變字符串用來構(gòu)建方法名稱 Method setMethod = null; Method getMethod = null; PropertyDescriptor pd = null; try { Field f = clazz.getDeclaredField(propertyName);//根據(jù)字段名來獲取字段 if (f!= null) { //構(gòu)建方法的后綴 String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); sb.append("set" + methodEnd);//構(gòu)建set方法 setMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ f.getType() }); sb.delete(0, sb.length());//清空整個可變字符串 sb.append("get" + methodEnd);//構(gòu)建get方法 //構(gòu)建get 方法 getMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ }); //構(gòu)建一個屬性描述器 把對應(yīng)屬性 propertyName 的 get 和 set 方法保存到屬性描述器中 pd = new PropertyDescriptor(propertyName, getMethod, setMethod); } } catch (Exception ex) { ex.printStackTrace(); } return pd; } @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});//調(diào)用 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[]{});//調(diào)用方法獲取方法的返回值 } catch (Exception e) { e.printStackTrace(); } return value;//返回值 } }
第二段:
public boolean setValue(Object objSet, Object objGet) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IntrospectionException { boolean flag = true; Field fields[] = objSet.getClass().getDeclaredFields(); String value = ""; String fieldNameGet = ""; List list = new ArrayList(); Field afield[]; int j = (afield = fields).length; for(int i = 0; i < j; i++) { Field field = afield[i]; String fieldName = field.getName(); fieldNameGet = xmlToModel(fieldName); if(!"error".equals(fieldNameGet)) { PropertyDescriptor pd = new PropertyDescriptor(fieldNameGet, objGet.getClass()); Method getMethod = pd.getReadMethod(); value = String.valueOf(getMethod.invoke(objGet, new Object[0])); boolean checkResult = returnMessage.checkValue(value, fieldNameGet); if(checkResult) { PropertyDescriptor pd1 = new PropertyDescriptor(fieldName, objSet.getClass()); Method setMethod = pd1.getWriteMethod(); setMethod.invoke(objSet, new Object[] { field.getType().getConstructor(new Class[] { java/lang/String }).newInstance(new Object[] { value }) }); } else { flag = checkResult; } } } return flag; }
第三段:
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(); //寫數(shù)據(jù) for(Field f : fields) { PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz); Method wM = pd.getWriteMethod();//獲得寫方法 wM.invoke(obj, 2);//因為知道是int類型的屬性,所以傳個int過去就是了。。實際情況中需要判斷下他的參數(shù)類型 } //讀數(shù)據(jù) for(Field f : fields) { PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz); Method rM = pd.getReadMethod();//獲得讀方法 Integer num = (Integer) rM.invoke(obj);//因為知道是int類型的屬性,所以轉(zhuǎn)換成integer就是了。。也可以不轉(zhuǎn)換直接打印 System.out.println(num); } } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解析MapStruct轉(zhuǎn)換javaBean時出現(xiàn)的詭異事件
在項目中用到了MapStruct,對其可以轉(zhuǎn)換JavaBean特別好奇,今天小編給大家分享一個demo給大家講解MapStruct轉(zhuǎn)換javaBean時出現(xiàn)的詭異事件,感興趣的朋友一起看看吧2021-09-09Spring中@DependsOn注解的作用及實現(xiàn)原理解析
這篇文章主要介紹了Spring中@DependsOn注解的作用及實現(xiàn)原理解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03