最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中PropertyDescriptor的用法及說(shuō)明

 更新時(shí)間:2023年07月07日 08:40:10   作者:bboyzqh  
這篇文章主要介紹了Java中PropertyDescriptor的用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java中PropertyDescriptor用法

PropertyDescriptor 類表示 JavaBean 類通過(guò)存儲(chǔ)器導(dǎo)出一個(gè)屬性。

構(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類型對(duì)象
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è)可變字符串用來(lái)構(gòu)建方法名稱
? ? ? ? Method setMethod = null;
? ? ? ? Method getMethod = null;
? ? ? ? PropertyDescriptor pd = null;
? ? ? ? try {
? ? ? ? ? ? Field f = clazz.getDeclaredField(propertyName);//根據(jù)字段名來(lái)獲取字段
? ? ? ? ? ? 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)建一個(gè)屬性描述器 把對(duì)應(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();//獲取對(duì)象的類型
? ? ? ? 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();//獲取對(duì)象的類型
? ? ? ? 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);//因?yàn)橹朗莍nt類型的屬性,所以傳個(gè)int過(guò)去就是了。。實(shí)際情況中需要判斷下他的參數(shù)類型
? ? ? ? }
? ? ? ? //讀數(shù)據(jù)
? ? ? ? for (Field f : fields) {
? ? ? ? ? ? PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz);
? ? ? ? ? ? Method rM = pd.getReadMethod();//獲得讀方法
? ? ? ? ? ? Integer num = (Integer) rM.invoke(obj);//因?yàn)橹朗莍nt類型的屬性,所以轉(zhuǎn)換成integer就是了。。也可以不轉(zhuǎn)換直接打印
? ? ? ? ? ? System.out.println(num);
? ? ? ? }
? ? }
}

Java PropertyDescriptor分析

簡(jiǎn)單說(shuō)明

正如其名稱,用于描述屬性相關(guān)的信息,如對(duì)于讀寫方法的設(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對(duì)象對(duì)當(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;
? ? }
}

測(cè)試代碼

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è)置屬性編輯器,用來(lái)修改屬性
? ? ? ? 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());
? ? }
}

運(yùn)行:

讀方法是:
public int yudaosourcecode.propertydescriptortest.BeanForPropertyDescriptor.getAge()
寫方法是:
public void yudaosourcecode.propertydescriptortest.BeanForPropertyDescriptor.setAge(int)
class java.lang.Integer

Process finished with exit code 0

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java異常處理與throws關(guān)鍵字用法分析

    Java異常處理與throws關(guān)鍵字用法分析

    這篇文章主要介紹了Java異常處理與throws關(guān)鍵字用法,結(jié)合實(shí)例形式分析了java常見(jiàn)的異常、錯(cuò)誤處理及throws關(guān)鍵字相關(guān)使用技巧、注意事項(xiàng),需要的朋友可以參考下
    2019-01-01
  • Java讀取properties配置文件的實(shí)現(xiàn)過(guò)程

    Java讀取properties配置文件的實(shí)現(xiàn)過(guò)程

    這篇文章主要介紹了Java讀取properties配置文件的實(shí)現(xiàn)過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2026-03-03
  • SpringBoot項(xiàng)目打成war和jar的區(qū)別說(shuō)明

    SpringBoot項(xiàng)目打成war和jar的區(qū)別說(shuō)明

    這篇文章主要介紹了SpringBoot項(xiàng)目打成war和jar的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 一個(gè)簡(jiǎn)單的SpringBoot項(xiàng)目快速搭建詳細(xì)步驟

    一個(gè)簡(jiǎn)單的SpringBoot項(xiàng)目快速搭建詳細(xì)步驟

    Spring Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化新Spring應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程,下面這篇文章主要給大家介紹了一個(gè)簡(jiǎn)單的SpringBoot項(xiàng)目快速搭建詳細(xì)步驟,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-08-08
  • Eclipse、MyEclipse 導(dǎo)入svn項(xiàng)目具體步驟

    Eclipse、MyEclipse 導(dǎo)入svn項(xiàng)目具體步驟

    這篇文章主要介紹了Eclipse、MyEclipse 導(dǎo)入svn項(xiàng)目具體步驟的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • java web實(shí)現(xiàn)用戶權(quán)限管理

    java web實(shí)現(xiàn)用戶權(quán)限管理

    這篇文章主要介紹了java web實(shí)現(xiàn)用戶權(quán)限管理,設(shè)計(jì)并實(shí)現(xiàn)一套簡(jiǎn)單的權(quán)限管理功能,感興趣的小伙伴們可以參考一下
    2015-11-11
  • SpringBoot2.0集成WebSocket實(shí)現(xiàn)后臺(tái)向前端推送信息

    SpringBoot2.0集成WebSocket實(shí)現(xiàn)后臺(tái)向前端推送信息

    這篇文章主要介紹了SpringBoot2.0集成WebSocket實(shí)現(xiàn)后臺(tái)向前端推送信息,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • java 使用JDBC構(gòu)建簡(jiǎn)單的數(shù)據(jù)訪問(wèn)層實(shí)例詳解

    java 使用JDBC構(gòu)建簡(jiǎn)單的數(shù)據(jù)訪問(wèn)層實(shí)例詳解

    以下是如何使用JDBC構(gòu)建一個(gè)數(shù)據(jù)訪問(wèn)層,包括數(shù)據(jù)轉(zhuǎn)換(將從數(shù)據(jù)庫(kù)中查詢的數(shù)據(jù)封裝到對(duì)應(yīng)的對(duì)象中……),數(shù)據(jù)庫(kù)的建立,以及如何連接到數(shù)據(jù)庫(kù),需要的朋友可以參考下
    2016-11-11
  • 詳解Java單元測(cè)試Junit框架實(shí)例

    詳解Java單元測(cè)試Junit框架實(shí)例

    這篇文章主要介紹了Java的異常測(cè)試框架JUnit使用上手指南,JUnit是Java代碼進(jìn)行單元測(cè)試中的常用工具,需要的朋友可以參考下
    2017-04-04
  • Spring Boot編寫攔截器教程實(shí)例解析

    Spring Boot編寫攔截器教程實(shí)例解析

    這篇文章主要介紹了Spring Boot編寫攔截器教程實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06

最新評(píng)論

客服| 阜新市| 阜南县| 会泽县| 绥化市| 榕江县| 灵璧县| 武威市| 灵山县| 肥乡县| 青河县| 南漳县| 龙州县| 宿州市| 洞口县| 兰坪| 瑞昌市| 礼泉县| 枞阳县| 荔波县| 射洪县| 梓潼县| 崇州市| 屯留县| 通许县| 丹凤县| 成安县| 肥乡县| 佛坪县| 亚东县| 平武县| 永福县| 宣城市| 平利县| 罗田县| 潞城市| 正蓝旗| 合江县| 江陵县| 游戏| 甘洛县|