首頁 > 軟體

Java BeanMap實現Bean與Map的相互轉換

2022-11-21 14:00:55

net.sf.cglib.beans.BeanMap用法

bean轉Map

@Data
public class Student {
    private int id;
    private String name;
    private Integer age;
}
    Student student = new Student();
    BeanMap beanMap = BeanMap.create(student);

此時的beanMap就是一個map型別

但是對於直接生成的beanMap無法新增key,也無法刪除key(會報錯),並且修改值會直接影響到student這個物件。

如果有這個需求可以再進一步轉換為HashMap(如果沒這個需要,就不要再轉換,避免不必要的效能浪費)

HashMap map = new HashMap();
map.putAll(beanMap);

map轉Bean

普通Map轉轉換成bean

        HashMap map = new HashMap();
        map.put("name","hello world");   
        Student student = new Student();
        BeanMap beanMap = BeanMap.create(student);
        beanMap.putAll(map);

利用了修改beanMap會影響bean的特性,將map put到beanMap完成轉換.

beanMap轉成對應的bean

public static <T> T beanMapToBean(BeanMap beanMap) {
        if (beanMap == null) {
            throw new DataStreamException("bean.can.not.be.null");
        }
        return (T) beanMap.getBean();
    }

直接呼叫getBean方法就可以獲取beanMap對應的bean

beanMap實現以及高效能的原因

先看beanMap

public abstract class BeanMap implements Map 

繼承自Map介面,實現了contains,remove,put,get等操作

    public Object remove(Object key) {
        throw new UnsupportedOperationException();
    }

但是remove會丟擲UnsupportedOperationException

BeanMap.create()方法通過asm動態生成位元組碼建立一個beanMap

那麼我們來看看建立出來的beanMap的位元組碼檔案

import java.math.BigDecimal;
import java.util.Date;
import java.util.Set;
import net.sf.cglib.beans.BeanMap;
import net.sf.cglib.beans.FixedKeySet;
public class Student$$BeanMapByCGLIB$$54bf0fe9 extends BeanMap {
    private static FixedKeySet keys;
    private static final Class CGLIB$load_class$java$2Eutil$2ESet;
    private static final Class CGLIB$load_class$java$2Elang$2EInteger;
    private static final Class CGLIB$load_class$java$2Elang$2EString;
    private static final Class CGLIB$load_class$java$2Emath$2EBigDecimal;
    private static final Class CGLIB$load_class$java$2Eutil$2EDate;
    public Student$$BeanMapByCGLIB$$54bf0fe9() {
    }
    public BeanMap newInstance(Object var1) {
        return new Student$$BeanMapByCGLIB$$54bf0fe9(var1);
    }
    public Student$$BeanMapByCGLIB$$54bf0fe9(Object var1) {
        super(var1);
    }
    public Object get(Object var1, Object var2) {
        Student var10000 = (Student)var1;
        String var10001 = (String)var2;
        switch(((String)var2).hashCode()) {
        case -600094315:
            if (var10001.equals("friends")) {
                return var10000.getFriends();
            }
            break;
        case 3355:
            if (var10001.equals("id")) {
                return new Integer(var10000.getId());
            }
            break;
        case 96511:
            if (var10001.equals("age")) {
                return var10000.getAge();
            }
            break;
        case 3373707:
            if (var10001.equals("name")) {
                return var10000.getName();
            }
            break;
        case 104079552:
            if (var10001.equals("money")) {
                return var10000.getMoney();
            }
            break;
        case 1069376125:
            if (var10001.equals("birthday")) {
                return var10000.getBirthday();
            }
        }
        return null;
    }
    public Object put(Object var1, Object var2, Object var3) {
        Student var10000 = (Student)var1;
        String var10001 = (String)var2;
        switch(((String)var2).hashCode()) {
        case -600094315:
            if (var10001.equals("friends")) {
                Set var7 = var10000.getFriends();
                var10000.setFriends((Set)var3);
                return var7;
            }
            break;
        case 3355:
            if (var10001.equals("id")) {
                Integer var10003 = new Integer(var10000.getId());
                var10000.setId(((Number)var3).intValue());
                return var10003;
            }
            break;
        case 96511:
            if (var10001.equals("age")) {
                Integer var6 = var10000.getAge();
                var10000.setAge((Integer)var3);
                return var6;
            }
            break;
        case 3373707:
            if (var10001.equals("name")) {
                String var5 = var10000.getName();
                var10000.setName((String)var3);
                return var5;
            }
            break;
        case 104079552:
            if (var10001.equals("money")) {
                BigDecimal var4 = var10000.getMoney();
                var10000.setMoney((BigDecimal)var3);
                return var4;
            }
            break;
        case 1069376125:
            if (var10001.equals("birthday")) {
                Date var10002 = var10000.getBirthday();
                var10000.setBirthday((Date)var3);
                return var10002;
            }
        }
        return null;
    }
    static {
        CGLIB$STATICHOOK1();
        keys = new FixedKeySet(new String[]{"birthday", "money", "name", "id", "age", "friends"});
    }
    static void CGLIB$STATICHOOK1() {
        CGLIB$load_class$java$2Eutil$2ESet = Class.forName("java.util.Set");
        CGLIB$load_class$java$2Elang$2EInteger = Class.forName("java.lang.Integer");
        CGLIB$load_class$java$2Elang$2EString = Class.forName("java.lang.String");
        CGLIB$load_class$java$2Emath$2EBigDecimal = Class.forName("java.math.BigDecimal");
        CGLIB$load_class$java$2Eutil$2EDate = Class.forName("java.util.Date");
    }
    public Set keySet() {
        return keys;
    }
    public Class getPropertyType(String var1) {
        switch(var1.hashCode()) {
        case -600094315:
            if (var1.equals("friends")) {
                return CGLIB$load_class$java$2Eutil$2ESet;
            }
            break;
        case 3355:
            if (var1.equals("id")) {
                return Integer.TYPE;
            }
            break;
        case 96511:
            if (var1.equals("age")) {
                return CGLIB$load_class$java$2Elang$2EInteger;
            }
            break;
        case 3373707:
            if (var1.equals("name")) {
                return CGLIB$load_class$java$2Elang$2EString;
            }
            break;
        case 104079552:
            if (var1.equals("money")) {
                return CGLIB$load_class$java$2Emath$2EBigDecimal;
            }
            break;
        case 1069376125:
            if (var1.equals("birthday")) {
                return CGLIB$load_class$java$2Eutil$2EDate;
            }
        }
        return null;
    }
}

這個生成的類繼承自BeanMap,在構造時傳進來bean範例,並且根據bean的屬性建立對應的get/put方法

可以看出來在執行get和put時都是根據屬性名稱找到對應bean範例的set/get方法進行處理。也因此beanMap和對應範例會相互影響

還具備getPropertyType獲取欄位型別的功能

在bean轉map過程中除了生成這個動態class之外(生成之後會快取,對於同一型別的bean,不會重複建立),整個過程沒有真正複製物件變成map(避免的記憶體的浪費),也沒有通過效率較慢的反射去操作bean範例。因此BeanMap效能是比較不錯的

到此這篇關於Java BeanMap實現Bean與Map的相互轉換的文章就介紹到這了,更多相關Bean與Map的相互轉換內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com