一、軟件包 java.beans?
? ? 包含與開發(fā) beans 有關(guān)的類
二、PropertyDescriptor
? ? PropertyDescriptor 描述 Java Bean 通過存儲器方法導(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)
常用方法:
public class PropertyDescriptor extends FeatureDescriptor?
{?
? ? //構(gòu)造方法?
? ? //通過調(diào)用 getFoo 和 setFoo 存取方法,為符合標準 Java 約定的屬性構(gòu)造一個 PropertyDescriptor?
? ? public PropertyDescriptor(String propertyName,?
? ? ? ? ? ? ? ? ? ? ? ? ? Class<?> beanClass)?
? ? ? ? ? ? ? ? ? throws IntrospectionException{}?
? ? //獲得屬性的 Class 對象?
? ? public Class<?> getPropertyType(){}?
? ? //獲得應(yīng)該用于讀取屬性值的方法?
? ? public Method getReadMethod(){}?
? ? //獲得應(yīng)該用于寫入屬性值的方法?
? ? public Method getWriteMethod(){}?
? ? ...
}
使用例子:
public class Price {
? ? private String mBuyPrice;
? ? public String getMBuyPrice() {
? ? ? ? return mBuyPrice;
? ? }
? ? public void setMBuyPrice(String mBuyPrice) {
? ? ? ? this.mBuyPrice = mBuyPrice;
? ? }
? ? @Override
? ? public String toString() {
? ? ? ? return "Price{" +
? ? ? ? ? ? ? ? "mBuyPrice='" + mBuyPrice + '\'' +
? ? ? ? ? ? ? ? '}';
? ? }
}
測試類:
public class TestPropertyDescriptor {
? ? public static void main(String[] args) {
? ? ? ? try {
? ? ? ? ? ? Class clazz = Class.forName("com.ssm.model.Price");
? ? ? ? ? ? Object obj =? clazz.newInstance();
? ? ? ? ? ? Field[] fields = clazz.getDeclaredFields();
? ? ? ? ? ? //寫數(shù)據(jù),即獲得寫方法(setter方法)給屬性賦值
? ? ? ? ? ? for(Field f : fields){
? ? ? ? ? ? ? ? PropertyDescriptor pd = new PropertyDescriptor(f.getName(),clazz);
? ? ? ? ? ? ? ? Method method = pd.getWriteMethod();
? ? ? ? ? ? ? ? method.invoke(obj,"100元");
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println(obj.toString());
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}
運行結(jié)果:
Price{mBuyPrice='100元'}
注意:
Price類中的屬性為
private String mBuyPrice;
使用Idea或者Eclipse自動生成getter和setter方法時,會生成如下:
public class Price {
? ? private String mBuyPrice;
? ? public String getmBuyPrice() {
? ? ? ? return mBuyPrice;
? ? }
? ? public void setmBuyPrice(String mBuyPrice) {
? ? ? ? this.mBuyPrice = mBuyPrice;
? ? }
? ? @Override
? ? public String toString() {
? ? ? ? return "Price{" +
? ? ? ? ? ? ? ? "mBuyPrice='" + mBuyPrice + '\'' +
? ? ? ? ? ? ? ? '}';
? ? }
}
用測試類運行后會報錯:
java.beans.IntrospectionException: Method not found: isMBuyPrice
同時Price類必須含有g(shù)etter和setter方法,否則也會報同樣的錯誤。
JavaBean屬性名要求:前兩個字母要么都大寫,要么都小寫
mport java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @author hui
* @description
* @create 2018/8/30 上午11:29
*/
public class PropertyDescriptorUtil {
? ? 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;
? ? }
}