<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    俊星的BLOG

    Commons BeanUtil之我的GetPropUtil

    Commons BeanUtil的介紹(抄錄手冊的說明):

    The Bean Introspection Utilities component of the Apache Commons subproject offers low-level utility classes that assist in getting and setting property values on Java classes that follow the naming design patterns outlined in the JavaBeans Specification, as well as mechanisms for dynamically defining and accessing bean properties.
    下面是我對Get 方法的一個簡單實現:
    1、屬性工具類:
    package test;

    import java.beans.BeanInfo;
    import java.beans.IntrospectionException;
    import java.beans.Introspector;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.Array;
    import java.util.List;
    import java.util.Map;

    public class MyPropUtil {
        
    private static final char MAPPED_START = '(';
        
    private static final char MAPPED_END = ')';
        
    private static final char INDEXED_START = '[';
        
    private static final char INDEXED_END = ']';
        
    private static MyPropUtil instance;

        
    private MyPropUtil() {

        }


        
    public static MyPropUtil getInstance() {
            
    if (instance == null{
                instance 
    = new MyPropUtil();
            }

            
    return instance;
        }


        
    /** 獲取簡單屬性 */
        
    public Object getSimpleProperty(Object bean, String name) throws Exception {
            String prop 
    = this.getProperty(name);
            PropertyDescriptor desc 
    = this.getPropDesc(bean.getClass(), prop);
            
    return desc.getReadMethod().invoke(bean, new Object[0]);
        }


        
    /** 設置簡單屬性 */
        
    public void setSimpleProperty(Object bean, String name, Object value) throws Exception {
            String prop 
    = this.getProperty(name);
            PropertyDescriptor desc 
    = this.getPropDesc(bean.getClass(), prop);
            Object[] args 
    = new Object[1];
            args[
    0= value;
            desc.getWriteMethod().invoke(bean, args);
        }


        
    /** 獲取索引屬性 */
        
    public Object getIndexedProperty(Object bean, String name) throws Exception {
            
    int index = this.getIndex(name);
            String prop 
    = this.getProperty(name);
            PropertyDescriptor desc 
    = this.getPropDesc(bean.getClass(), prop);
            Object value 
    = desc.getReadMethod().invoke(bean, new Object[0]);
            
    if (value.getClass().isArray()) {
                value 
    = Array.get(value, index);
            }
     else {
                
    if (value instanceof List<?>{
                    value 
    = ((List<?>) value).get(index);
                }

            }

            
    return value;
        }


        
    /** 設置Map屬性 */
        
    public void setIndexedProperty(Object bean, String name, Object value) throws Exception {
            
    int index = this.getIndex(name);
            String prop 
    = this.getProperty(name);
            PropertyDescriptor desc 
    = this.getPropDesc(bean.getClass(), prop);
            Object array 
    = desc.getReadMethod().invoke(bean, new Object[0]);
            
    if (array.getClass().isArray()) {
                Array.set(array, index, value);
            }
     else {
                
    if (array instanceof List) {
                    ((List) array).set(index, value);
                }

            }

        }


        
    /** 獲取Map屬性 */
        
    public Object getMappedProperty(Object bean, String name) throws Exception {
            String key 
    = this.getKey(name);
            String prop 
    = this.getProperty(name);
            PropertyDescriptor desc 
    = this.getPropDesc(bean.getClass(), prop);
            Object value 
    = desc.getReadMethod().invoke(bean, new Object[0]);
            
    if (value instanceof Map<??>{
                value 
    = ((Map<??>) value).get(key);
            }

            
    return value;
        }


        
    public void setMappedProperty(Object bean, String name, Object value) throws Exception {
            String key 
    = this.getKey(name);
            String prop 
    = this.getProperty(name);
            PropertyDescriptor desc 
    = this.getPropDesc(bean.getClass(), prop);
            Object map 
    = desc.getReadMethod().invoke(bean, new Object[0]);
            
    if (map instanceof Map) {
                ((Map) map).put(key, value);
            }

        }


        
    private String getProperty(String s) {
            
    for (int i = 0, size = s.length(); i < size; i++{
                
    char c = s.charAt(i);
                
    if (c == MAPPED_START || c == INDEXED_START) {
                    
    return s.substring(0, i);
                }

            }

            
    return s;
        }


        
    private PropertyDescriptor getPropDesc(Class<?> cls, String prop) throws IntrospectionException {
            BeanInfo info 
    = Introspector.getBeanInfo(cls);
            PropertyDescriptor[] descs 
    = info.getPropertyDescriptors();
            PropertyDescriptor desc 
    = null;
            
    for (PropertyDescriptor pd : descs) {
                
    if (prop.equals(pd.getName())) {
                    desc 
    = pd;
                    
    break;
                }

            }

            
    return desc;
        }


        
    private int getIndex(String s) {
            
    for (int i = 0, size = s.length(); i < size; i++{
                
    char c = s.charAt(i);
                
    if (c == INDEXED_START) {
                    
    int end = s.indexOf(INDEXED_END, i);
                    
    return Integer.parseInt(s.substring(i + 1, end));
                }

            }

            
    return -1;
        }


        
    private String getKey(String s) {
            
    for (int i = 0, size = s.length(); i < size; i++{
                
    char c = s.charAt(i);
                
    if (c == MAPPED_START) {
                    
    int end = s.indexOf(MAPPED_END, i);
                    
    return s.substring(i + 1, end);
                }

            }

            
    return null;
        }


    }


    2、測試:
    2-1、業務類:
    package test;

    import java.util.List;
    import java.util.Map;

    public class MyBean {
        
    private String name;

        
    private List<String> list;

        
    private Map<String, String> map;

        
    private int[] intArray;

        
    public int[] getIntArray() {
            
    return intArray;
        }


        
    public void setIntArray(int[] intArray) {
            
    this.intArray = intArray;
        }


        
    public Map<String, String> getMap() {
            
    return map;
        }


        
    public void setMap(Map<String, String> map) {
            
    this.map = map;
        }


        
    public List<String> getList() {
            
    return list;
        }


        
    public void setList(List<String> list) {
            
    this.list = list;
        }


        
    public MyBean(String name) {
            
    super();
            
    this.name = name;
        }


        
    public String getName() {
            
    return name;
        }


        
    public void setName(String name) {
            
    this.name = name;
        }


        
    public String toString() {
            StringBuffer sb 
    = new StringBuffer();
            sb.append(
    "name:").append(name);
            sb.append(
    " list:[");
            
    if (list != null && list.size() > 0{
                
    for (String temp : list) {
                    sb.append(temp).append(
    ",");
                }

            }

            sb.append(
    "] map:(");
            
    if (map != null && map.size() > 0{
                
    for (String temp : map.keySet()) {
                    sb.append(temp).append(
    "-").append(map.get(temp)).append(",");
                }

            }

            sb.append(
    ") array:{");
            
    if (intArray != null && intArray.length > 0{
                
    for (int i : intArray) {
                    sb.append(i).append(
    ",");
                }

            }

            sb.append(
    "}");
            
    return sb.toString();
        }


    }


    2-2、測試類:
    package test;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;

    public class BeanTest {
        
    public static void main(String[] args) {
            
    try {
                testGet();
                testSet();
            }
     catch (Exception e) {
                
    // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


        
    public static void testGet() throws Exception {
            MyPropUtil util 
    = MyPropUtil.getInstance();
            MyBean bean 
    = new MyBean("good");
            System.out.println(util.getSimpleProperty(bean, 
    "name"));

            List
    <String> list = new ArrayList<String>();
            list.add(
    "list0");
            bean.setList(list);
            System.out.println(util.getIndexedProperty(bean, 
    "list[0]"));

            
    int[] intArray = 123 };
            bean.setIntArray(intArray);
            System.out.println(util.getIndexedProperty(bean, 
    "intArray[2]"));

            Map
    <String, String> map = new HashMap<String, String>();
            map.put(
    "key""value");
            bean.setMap(map);
            System.out.println(util.getMappedProperty(bean, 
    "map(key)"));

        }


        
    public static void testSet() throws Exception {
            MyPropUtil util 
    = MyPropUtil.getInstance();
            MyBean bean 
    = new MyBean("good");
            List
    <String> list = new ArrayList<String>();
            list.add(
    "list0");
            bean.setList(list);
            bean.setMap(
    new HashMap<String, String>());
            bean.setIntArray(
    new int[3]);
            util.setSimpleProperty(bean, 
    "name""namegood");
            util.setIndexedProperty(bean, 
    "list[0]""listgoodvalue");
            util.setIndexedProperty(bean, 
    "intArray[0]"3);
            util.setMappedProperty(bean, 
    "map(key)""value");
            System.out.println(bean);
        }


    }


    輸出如下:
    good
    list0
    3
    value
    name:namegood list:
    [listgoodvalue,] map:(key-value,) array:{3,0,0,}

    posted on 2009-05-02 22:19 俊星 閱讀(320) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 222www免费视频| 成年女人免费视频播放体验区| 亚洲成a人片77777老司机| 91成人在线免费观看| 亚洲人成7777| 在线亚洲精品自拍| 18勿入网站免费永久| 日本一区二区三区免费高清在线| 亚洲人成中文字幕在线观看| 三年片在线观看免费大全| 亚洲精品视频免费| 亚洲五月综合缴情婷婷| 久久久久久A亚洲欧洲AV冫| 日韩免费一区二区三区在线播放 | 春意影院午夜爽爽爽免费| 亚洲视频在线观看地址| 国产一区二区免费在线| 3344免费播放观看视频| 一级毛片aa高清免费观看| 精品亚洲AV无码一区二区三区| 亚洲国产精品日韩专区AV| 免费视频专区一国产盗摄| 成人免费ā片在线观看| 亚洲国产精品无码久久九九大片| 国产成人A人亚洲精品无码| 黄a大片av永久免费| 91免费国产自产地址入| 两个人看的www高清免费视频| 亚洲熟妇av午夜无码不卡| 久久亚洲AV成人出白浆无码国产| 亚洲成a人片在线播放| 成年女人男人免费视频播放 | 国产乱弄免费视频| 久久久久久精品成人免费图片| 久久WWW免费人成—看片| 亚洲成av人片在www鸭子| 亚洲国产中文在线二区三区免| 国产亚洲人成无码网在线观看| 免费女人18毛片a级毛片视频| 成人影片麻豆国产影片免费观看| 午夜视频免费在线观看|