Java通过PropertyDescriptor反射调用set和get方法
本文实例为大家分享了PropertyDescriptor反射调用set和get方法,供大家参考,具体内容如下
第一段:
packagecom.zhoushun; importjava.lang.reflect.Method; importjava.lang.reflect.Field; importjava.beans.PropertyDescriptor; publicclassPropertyUtil{ @SuppressWarnings("unchecked") publicstaticPropertyDescriptorgetPropertyDescriptor(Classclazz,StringpropertyName){ StringBuffersb=newStringBuffer();//构建一个可变字符串用来构建方法名称 MethodsetMethod=null; MethodgetMethod=null; PropertyDescriptorpd=null; try{ Fieldf=clazz.getDeclaredField(propertyName);//根据字段名来获取字段 if(f!=null){ //构建方法的后缀 StringmethodEnd=propertyName.substring(0,1).toUpperCase()+propertyName.substring(1); sb.append("set"+methodEnd);//构建set方法 setMethod=clazz.getDeclaredMethod(sb.toString(),newClass[]{f.getType()}); sb.delete(0,sb.length());//清空整个可变字符串 sb.append("get"+methodEnd);//构建get方法 //构建get方法 getMethod=clazz.getDeclaredMethod(sb.toString(),newClass[]{}); //构建一个属性描述器把对应属性propertyName的get和set方法保存到属性描述器中 pd=newPropertyDescriptor(propertyName,getMethod,setMethod); } }catch(Exceptionex){ ex.printStackTrace(); } returnpd; } @SuppressWarnings("unchecked") publicstaticvoidsetProperty(Objectobj,StringpropertyName,Objectvalue){ Classclazz=obj.getClass();//获取对象的类型 PropertyDescriptorpd=getPropertyDescriptor(clazz,propertyName);//获取clazz类型中的propertyName的属性描述器 MethodsetMethod=pd.getWriteMethod();//从属性描述器中获取set方法 try{ setMethod.invoke(obj,newObject[]{value});//调用set方法将传入的value值保存属性中去 }catch(Exceptione){ e.printStackTrace(); } } @SuppressWarnings("unchecked") publicstaticObjectgetProperty(Objectobj,StringpropertyName){ Classclazz=obj.getClass();//获取对象的类型 PropertyDescriptorpd=getPropertyDescriptor(clazz,propertyName);//获取clazz类型中的propertyName的属性描述器 MethodgetMethod=pd.getReadMethod();//从属性描述器中获取get方法 Objectvalue=null; try{ value=getMethod.invoke(clazz,newObject[]{});//调用方法获取方法的返回值 }catch(Exceptione){ e.printStackTrace(); } returnvalue;//返回值 } }
第二段:
publicbooleansetValue(ObjectobjSet,ObjectobjGet) throwsIllegalArgumentException,SecurityException,InstantiationException,IllegalAccessException,InvocationTargetException,NoSuchMethodException,IntrospectionException { booleanflag=true; Fieldfields[]=objSet.getClass().getDeclaredFields(); Stringvalue=""; StringfieldNameGet=""; Listlist=newArrayList(); Fieldafield[]; intj=(afield=fields).length; for(inti=0;i<j;i++) { Fieldfield=afield[i]; StringfieldName=field.getName(); fieldNameGet=xmlToModel(fieldName); if(!"error".equals(fieldNameGet)) { PropertyDescriptorpd=newPropertyDescriptor(fieldNameGet,objGet.getClass()); MethodgetMethod=pd.getReadMethod(); value=String.valueOf(getMethod.invoke(objGet,newObject[0])); booleancheckResult=returnMessage.checkValue(value,fieldNameGet); if(checkResult) { PropertyDescriptorpd1=newPropertyDescriptor(fieldName,objSet.getClass()); MethodsetMethod=pd1.getWriteMethod(); setMethod.invoke(objSet,newObject[]{ field.getType().getConstructor(newClass[]{ java/lang/String }).newInstance(newObject[]{ value }) }); }else { flag=checkResult; } } } returnflag; }
第三段:
importjava.beans.PropertyDescriptor; importjava.lang.reflect.Field; importjava.lang.reflect.Method; publicclassReflectTest{ publicstaticvoidmain(String[]args)throwsException{ Classclazz=Class.forName("TaskProvidePropsList");//这里的类名是全名。。有包的话要加上包名 Objectobj=clazz.newInstance(); Field[]fields=clazz.getDeclaredFields(); //写数据 for(Fieldf:fields){ PropertyDescriptorpd=newPropertyDescriptor(f.getName(),clazz); MethodwM=pd.getWriteMethod();//获得写方法 wM.invoke(obj,2);//因为知道是int类型的属性,所以传个int过去就是了。。实际情况中需要判断下他的参数类型 } //读数据 for(Fieldf:fields){ PropertyDescriptorpd=newPropertyDescriptor(f.getName(),clazz); MethodrM=pd.getReadMethod();//获得读方法 Integernum=(Integer)rM.invoke(obj);//因为知道是int类型的属性,所以转换成integer就是了。。也可以不转换直接打印 System.out.println(num); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。