<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Objenesis不使用構造方法創(chuàng)建對象

          聯(lián)合創(chuàng)作 · 2023-09-28 21:14

          如果一個類沒有參數(shù)為空的構造方法時候,那么你直接調(diào)用newInstance方法試圖得到一個實例對象的時候是會拋出異常的。能不能有 辦法繞過構造方法來實例化一個對象呢?

          Objenesis 為其提供了在四個不同的jvm上的解決方案。

          •  Sun Hotspot VM, versions 1.3, 1.4, 1.5 and 1.6

          •  GCJ version 3.4.4 (tested on Windows/Cygwin)

          •  BEA JRockit versions 7.0 (1.3.1), 1.4.2 and 1.5

          •  Aonix PERC (no serialization support), tested on version  5.0.0667

          從運行平臺上得到幾個關鍵的參數(shù),如下:

          1. /** JVM version */

          2. protected static final String VM_VERSION = System.getProperty("java.runtime.version");   


          3. /** JVM version */

          4. protected static final String VM_INFO = System.getProperty("java.vm.info");   


          5. /** Vendor version */

          6. protected static final String VENDOR_VERSION = System.getProperty("java.vm.version");   


          7. /** Vendor name */

          8. protected static final String VENDOR = System.getProperty("java.vm.vendor");   


          9. /** JVM name */

          10. protected static final String JVM_NAME = System.getProperty("java.vm.name");  

          然后根據(jù)得到的參數(shù)進行判斷:

          根據(jù)得到平臺提供的jvm版本和供應商來選擇不同的實例化策略。
          說實話,這幾個平臺里面我還 是對sun公司提供的相對熟悉一些,所以除了sun公司提供的jvm對于的實例策略我在這里就不介紹了,
          大家有興趣的話可以去項目主頁下載下來細 細研究。

          現(xiàn)在我們僅僅關注sun公司的,并且版本大于1.3的。
          版本為1.3的jvm具體實例化策略這里不做討論了,有興趣的可 以去看objenesis的實現(xiàn)。

          代碼如下:

          1. import sun.reflect.ReflectionFactory;    

          2. public class SunReflectionFactoryInstantiator implements ObjectInstantiator {   


          3. private final Constructor mungedConstructor;   


          4. public SunReflectionFactoryInstantiator(Class type) {    


          5. ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory();    

          6. Constructor javaLangObjectConstructor;   


          7. try {    

          8. javaLangObjectConstructor = Object.class.getConstructor((Class[]) null);   

          9. }   

          10. catch(NoSuchMethodException e) {    

          11. throw new Error("Cannot find constructor for java.lang.Object!");    

          12. }   

          13. mungedConstructor = reflectionFactory.newConstructorForSerialization(type,    

          14. javaLangObjectConstructor);   

          15. mungedConstructor.setAccessible(true);   

          16. }   


          17. public Object newInstance() {    

          18. try {    

          19. return mungedConstructor.newInstance((Object[]) null);   

          20. }   

          21. catch(Exception e) {    

          22. throw new ObjenesisException(e);   

          23. }   

          24. }   

          25. }   


          通過sun.reflect.ReflectionFactory這 個類來實例化一個class那么就繞過了其類的構造方法,我們可以暫且稱之為繞道方式實例一個對象。
          希望上面的代碼能給大家起到一定的幫助,另外easymock的 最新版本已經(jīng)使用了Objenesis來實例化一個Class獲取對象。

          介紹來自 blogjava— @和風賽跑

          瀏覽 14
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美逼首页 | 国产极品艳情生活视频在线播放 | 欧美在线视频播放 | 在线免费观看小黄片 | 西西4444www大胆艺术 |