1. 程式人生 > >2-java內省機制(Introspector)

2-java內省機制(Introspector)

etc getc 屬性 cep tid beans param tor urn

來一個簡單的示例吧

package com.my.test;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

import com.my.bean.User;

public class Demo {
    /**
     * 劉詩華
     * 內省機制(Introspector)
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws
Exception { //獲取User類的字節碼,不要獲取直接父類(Object)的屬性 BeanInfo beanInfo = Introspector.getBeanInfo(User.class,Object.class); //獲取User類裏面的所有屬性描述器 返回數組 PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) {
//屬性字段名 String name = pd.getName(); //屬性字段類型 Class type = pd.getPropertyType(); System.out.println(name+"="+type); } //打印結果如下顯示 //id=int //password=class java.lang.String //userName=class java.lang.String
} }

獲取Getter和Setter方法

package com.my.test;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import com.my.bean.User;

public class Demo {
    /**
     * 劉詩華
     * 內省機制(Introspector)
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        
        //獲取User類的字節碼,不要獲取直接父類(Object)的屬性
        BeanInfo beanInfo = Introspector.getBeanInfo(User.class,Object.class);
        
        //獲取User類裏面的所有屬性描述器  返回數組
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        
        for (PropertyDescriptor pd : pds) {
            //獲取getter方法
            Method readMethod = pd.getReadMethod();
            //獲取setter方法
            Method writeMethod = pd.getWriteMethod();
    
            System.out.println(readMethod);
            System.out.println(writeMethod);
        }
        
        //打印結果如下顯示
        // public int com.my.bean.User.getId()
        // public void com.my.bean.User.setId(int)
        // public java.lang.String com.my.bean.User.getPassword()
        // public void com.my.bean.User.setPassword(java.lang.String)
        // public java.lang.String com.my.bean.User.getUserName()
        // public void com.my.bean.User.setUserName(java.lang.String)
    }
}

通過內省略機制封兩個方法

package com.my.javabean;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.Map;

import com.my.bean.User;

public class BeanUtil {
    
    
    /***
     * Bean對象轉換成Map集合
     * @param bean
     * @return
     * @throws Exception
     */
    public static Map<String, Object> bean2map(Object bean) throws Exception
    {
        //創建一個map集合對象
        Map<String, Object> map=new HashMap<String, Object>();
        
        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(),Object.class);
        //獲取Bean對象的屬性描述器
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        //叠代循環屬性描述器
        for (PropertyDescriptor pd : pds) {
            //獲取屬性名
            String propertyName=pd.getName();
            //獲取屬性值,調用 invoke方法
            Object propertyValue = pd.getReadMethod().invoke(bean);
            //將內容存放到map集合當中
            map.put(propertyName, propertyValue);
        }
        return map;
    }
    
    
    /***
     * 將Map集合數據封裝到Bean對象當中
     * T代表數據類型
     * @param beanMap  參數Map
     * @param beanType Bean對象字節碼
     * @return
     * @throws Exception
     */
    public static <T>T map2bean(Map<String, Object> beanMap,Class<T> beanType) throws Exception
    {
        //創建Bean對象,用T類型來接收 T是在Class<T> beanType這個參數就會確認實際類型
        T obj = beanType.newInstance();
        
        BeanInfo beanInfo = Introspector.getBeanInfo(beanType,Object.class);
        //獲取Bean對象的屬性描述器
        PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
        
        for (PropertyDescriptor pd : pds) {
            //獲取屬性名
            String propertyName=pd.getName();
            //從Map集合中取出數據,封裝到Bean對象當中
            pd.getWriteMethod().invoke(obj, beanMap.get(propertyName));
        }
        return obj;
    }
    
    
    public static void main(String[] args) throws Exception {
        
        User u=new User(100,"劉詩華","xxx");

        Map<String, Object> m = BeanUtil.bean2map(u);
        System.out.println(m);
        //{id=100, userName=劉詩華, password=xxx}
        
        User user = map2bean(m,User.class);
        System.out.println(user);
        //User(id=100, userName=劉詩華, password=xxx)
    }
}

2-java內省機制(Introspector)