Java中PropertyDescriptor的用法及說明
Java中PropertyDescriptor用法
PropertyDescriptor 類表示 JavaBean 類通過存儲器導(dǎo)出一個屬性。
構(gòu)造方法有
PropertyDescriptor(String propertyName, Class<?> beanClass) PropertyDescriptor(String propertyName, Class<?> beanClass, String readMethodName, String writeMethodName) PropertyDescriptor(String propertyName, Method readMethod, Method writeMethod)
常用方法有
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.
用法:
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();//構(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 方法 ? ? ? ? ? ? ? ? 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; ? ? } ? ? 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(); ? ? ? ? } ? ? } ? ? 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 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); ? ? ? ? } ? ? } }
Java PropertyDescriptor分析
簡單說明
正如其名稱,用于描述屬性相關(guān)的信息,如對于讀寫方法的設(shè)置和讀取,獲取屬性的類型等操作。
源碼
public構(gòu)造方法
public PropertyDescriptor(String propertyName, // 屬性的名稱 ?? ??? ??? ??? ??? ??? ?Class<?> beanClass // bean的class類型 ?? ??? ??? ??? ??? ??? ??? ?) public PropertyDescriptor(String propertyName, // 屬性名稱 ?? ??? ??? ??? ??? ??? ?Class<?> beanClass, // bean的class類型? ? ? ? ? ? ? ? ? ?? ??? ?String readMethodName, // 讀方法名稱 ? ? ? ? ? ? ? ? ?? ??? ?String writeMethodName // 寫方法名稱 ? ? ? ? ? ? ? ? ?? ??? ?) public PropertyDescriptor(String propertyName, // 屬性名稱 ?? ??? ??? ??? ??? ??? ?Method readMethod, // 讀方法 ?? ??? ??? ??? ??? ??? ?Method writeMethod // 寫方法 ?? ??? ??? ??? ??? ??? ?)
主要方法
// 獲取屬性的類型 public synchronized Class<?> getPropertyType() {} // 獲取讀方法 public synchronized Method getReadMethod() {} // 設(shè)置讀方法 public synchronized void setReadMethod(Method readMethod) {} // 獲取讀方法 public synchronized Method getWriteMethod() {} // 設(shè)置讀方法 public synchronized void setWriteMethod(Method writeMethod){} // 設(shè)置屬性可以使用的屬性編輯器類型 public void setPropertyEditorClass(Class<?> propertyEditorClass) {} // 獲取當(dāng)前設(shè)置的屬性編輯器類型 public Class<?> getPropertyEditorClass() {} // 創(chuàng)建bean對象對當(dāng)前屬性的屬性編輯器 public PropertyEditor createPropertyEditor(Object bean) {}
例子
自定義屬性編輯器
/** ?* 自定義的屬性編輯器,轉(zhuǎn)換數(shù)據(jù)類型 ?*/ public class MyAgePropertyEditor extends PropertyEditorSupport { ? ? private String sourceText; ? ? /** ? ? ?* 設(shè)置源數(shù)據(jù)值 ? ? ?* @param text ? ? ?* @throws IllegalArgumentException ? ? ?*/ ? ? @Override ? ? public void setAsText(String text) throws IllegalArgumentException { ? ? ? ? this.sourceText = text; ? ? } ? ? /** ? ? ?* 轉(zhuǎn)換為滿足age屬性要求的int類型 ? ? ?* @return ? ? ?*/ ? ? @Override ? ? public Object getValue() { ? ? ? ? return Integer.valueOf(sourceText); ? ? } }
定義bean
public class BeanForPropertyDescriptor { ? ? private String name; ? ? private int age; ? ? public String getName() { ? ? ? ? return name; ? ? } ? ? public void setName(String name) { ? ? ? ? this.name = name; ? ? } ? ? public int getAge() { ? ? ? ? return age; ? ? } ? ? public void setAge(int age) { ? ? ? ? this.age = age; ? ? } }
測試代碼
public class MainForPropertyDescriptor { ? ? public static void main(String[] args) throws Exception { ? ? ? ? Class<BeanForPropertyDescriptor> beanForPropertyDescriptorClass ? ? ? ? ? ? ? ? = BeanForPropertyDescriptor.class; ? ? ? ? PropertyDescriptor agePropDescriptor ? ? ? ? ? ? ? ? = new PropertyDescriptor("age", beanForPropertyDescriptorClass); ? ? ? ? Method readMethod = agePropDescriptor.getReadMethod(); ? ? ? ? System.out.println("讀方法是:"); ? ? ? ? System.out.println(readMethod); ? ? ? ? Method writeMethod = agePropDescriptor.getWriteMethod(); ? ? ? ? System.out.println("寫方法是:"); ? ? ? ? System.out.println(writeMethod); ? ? ? ? // 設(shè)置屬性編輯器,用來修改屬性 ? ? ? ? agePropDescriptor.setPropertyEditorClass(MyAgePropertyEditor.class); ? ? ? ? BeanForPropertyDescriptor beanForPropertyDescriptor = new BeanForPropertyDescriptor(); ? ? ? ? PropertyEditor agePropertyEditor = agePropDescriptor.createPropertyEditor(beanForPropertyDescriptor); ? ? ? ? agePropertyEditor.setAsText("90"); ? ? ? ? Object value = agePropertyEditor.getValue(); ? ? ? ? System.out.println(value.getClass()); ? ? } }
運行:
讀方法是:
public int yudaosourcecode.propertydescriptortest.BeanForPropertyDescriptor.getAge()
寫方法是:
public void yudaosourcecode.propertydescriptortest.BeanForPropertyDescriptor.setAge(int)
class java.lang.IntegerProcess finished with exit code 0
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Idea 2020.2 創(chuàng)建web、Spring項目的教程圖解
這篇文章主要介紹了Idea 2020.2 創(chuàng)建web、Spring項目的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08IntelliJ IDEA創(chuàng)建maven web項目的圖文步驟(IDEA新手適用)
這篇文章主要介紹了IntelliJ IDEA創(chuàng)建maven web項目的圖文步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文)
這篇文章主要介紹了使用maven整合Spring+SpringMVC+Mybatis框架詳細(xì)步驟(圖文),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05在CentOS系統(tǒng)中檢測Java安裝及運行jar應(yīng)用的方法
這篇文章主要介紹了在CentOS系統(tǒng)中檢測Java安裝及運行jar應(yīng)用的方法,同樣適用于Fedora等其他RedHat系的Linux系統(tǒng),需要的朋友可以參考下2015-06-06