年過三十仍在路上

          平凡而不平庸,低調(diào)而不頹廢 兩耳不聞人聲,只有硬盤在歌唱

          常用鏈接

          統(tǒng)計(jì)

          印刷報(bào)價(jià)

          印刷設(shè)備

          友情鏈接

          最新評論

          實(shí)現(xiàn)不調(diào)用構(gòu)造方法創(chuàng)建對象

          如果一個(gè)類沒有參數(shù)為空的構(gòu)造方法時(shí)候,那么你直接調(diào)用newInstance方法試圖得到一個(gè)實(shí)例對象的時(shí)候是會拋出異常的。

          能不能有辦法繞過構(gòu)造方法來實(shí)例化一個(gè)對象呢?

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

          首先我們看看四個(gè)不同的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

          從運(yùn)行平臺上得到幾個(gè)關(guān)鍵的參數(shù),如下:

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

          /** JVM version */
          protected static final String VM_INFO = System.getProperty("java.vm.info");

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

          /** Vendor name */
          protected static final String VENDOR = System.getProperty("java.vm.vendor");

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

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

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

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

          代碼如下:
          import sun.reflect.ReflectionFactory;
          public class SunReflectionFactoryInstantiator implements ObjectInstantiator {

          private final Constructor mungedConstructor;

          public SunReflectionFactoryInstantiator(Class type) {

          ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory();
          Constructor javaLangObjectConstructor;

          try {
          javaLangObjectConstructor = Object.class.getConstructor((Class[]) null);
          }
          catch(NoSuchMethodException e) {
          throw new Error("Cannot find constructor for java.lang.Object!");
          }
          mungedConstructor = reflectionFactory.newConstructorForSerialization(type,
          javaLangObjectConstructor);
          mungedConstructor.setAccessible(true);
          }

          public Object newInstance() {
          try {
          return mungedConstructor.newInstance((Object[]) null);
          }
          catch(Exception e) {
          throw new ObjenesisException(e);
          }
          }
          }

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

          項(xiàng)目主頁:http://objenesis.googlecode.com/svn/docs/index.html

          posted on 2009-08-20 08:53 和風(fēng)賽跑 閱讀(1792) 評論(5)  編輯  收藏

          評論

          # re: 實(shí)現(xiàn)不調(diào)用構(gòu)造方法創(chuàng)建對象 2009-08-20 09:11 BoBo小說網(wǎng)

          值得參考下  回復(fù)  更多評論   

          # re: 實(shí)現(xiàn)不調(diào)用構(gòu)造方法創(chuàng)建對象 2009-08-20 09:12 BoBo小說網(wǎng)

          實(shí)際應(yīng)用中還是很多的  回復(fù)  更多評論   

          # re: 實(shí)現(xiàn)不調(diào)用構(gòu)造方法創(chuàng)建對象 2009-08-20 09:53 隔葉黃鶯

          對的,在 XStream 轉(zhuǎn)換 XML 或 JSON 到 JavaBean 時(shí)用得就是 sun.reflect.ReflectionFactory,所以不依賴于 JavaBean 的構(gòu)造函數(shù)。  回復(fù)  更多評論   

          # re: 實(shí)現(xiàn)不調(diào)用構(gòu)造方法創(chuàng)建對象 2009-08-21 17:46 個(gè)性藝術(shù)簽名

          實(shí)際應(yīng)用中還是很多的  回復(fù)  更多評論   

          # re: 實(shí)現(xiàn)不調(diào)用構(gòu)造方法創(chuàng)建對象 2009-08-25 08:50 找個(gè)美女做老婆

          Java樂園學(xué)習(xí)網(wǎng)站: http://www.javaly.cn

          有大量的學(xué)習(xí)文章和視頻教程,以及一些項(xiàng)目源碼

          Java樂園學(xué)習(xí)群: 81107233  回復(fù)  更多評論   


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 积石山| 上蔡县| 洪江市| 淮北市| 巴青县| 乌什县| 松阳县| 武功县| 昔阳县| 双峰县| 清镇市| 滦南县| 松滋市| 海兴县| 江都市| 浦东新区| 炎陵县| 黄梅县| 祁阳县| 通城县| 临西县| 于田县| 团风县| 绥中县| 冀州市| 拜城县| 武宁县| 台南市| 仁化县| 牡丹江市| 新和县| 永吉县| 天全县| 泰来县| 自贡市| 博客| 龙口市| 博爱县| 普定县| 新绛县| 克山县|