閑人野居
          好好學(xué)習(xí),天天向上
          posts - 57,  comments - 137,  trackbacks - 0
              幾天前遇到這么一個(gè)問題,讓我郁悶了半天。當(dāng)初為了方便定義了以下的一個(gè)接口:
          public interface IBusinessObject<PK extends Serializable> extends Serializable {
              PK getPrimaryKey();
              
          void setPrimaryKey(PK id);
          }

          在我的子類中是這么實(shí)現(xiàn)的
          public class Code implements IBusinessObject<Long>{
           
          private Long primaryKey;
           
          public void setPrimaryKey(Long id){
              
          this.primaryKey=id;
           }
           
          public Long getPrimaryKey(){
              
          return primaryKey
           }
          }


          在通常的實(shí)例化過程中,是不會(huì)存在問題的。當(dāng)時(shí)的問題是,我定義了另外一個(gè)類,用于引用Code
          public class TestBean {
              
          private Code code;
              
          public void setCode(){}
              
          public Code getCode(){}
          }

          當(dāng)在spring環(huán)境中時(shí),使用spring的bind類處理的時(shí)候,發(fā)現(xiàn),我的code.primaryKey的類型居然為Serializable,而不是我想要的Long
          當(dāng)時(shí)以為其他地方搞錯(cuò)了,寫了個(gè)簡(jiǎn)單的測(cè)試代碼
          TestBean b=new TextBean();
          BeanWrapperImpl wrapper
          =new BeanWrapperImpl(b);
          b.setPropertyValue(
          "code.primaryKey","1");
          assertTrue(b.getCode().getPrimaryKey() 
          instanceof Long);

          居然是失敗的。
          只有在
          b.setPropertyValue("code.primaryKey",new Long(1));
          assertTrue(b.getCode().getPrimaryKey() 
          instanceof Long);

          才成功。
          仔細(xì)跟蹤,發(fā)現(xiàn)原來,泛型的時(shí)候,產(chǎn)生的編譯類中,有兩個(gè)同名的方法
           public void setPrimaryKey(Long id){
              
          this.primaryKey=id;
           }
           
          public Long getPrimaryKey(){
              
          return primaryKey
           }

           
          public void setPrimaryKey(Serializable id){
              
          this.primaryKey=id;
           }
           
          public Serializable getPrimaryKey(){
              
          return primaryKey
           } 

          而java.beans規(guī)范對(duì)于這種情況是沒有辦法分清楚,所以也就導(dǎo)致了結(jié)果和預(yù)期的不同。
          如果是在程序中,這點(diǎn)算不了問題。可是我需要在web的頁面上進(jìn)行值的綁定,而輸入的東西,只能為字符串。所以不可能出來Long類型。
          目前,采用了一種比較傻的辦法,在TestBean中增加了一個(gè)臨時(shí)變量x,通過x向code傳值
          如下:
          public void setWsCode(String wsCode) {
                  
          this.wsCode = wsCode;
                  
          if (StringUtils.isNumeric(wsCode))
                      
          this.code.setPrimaryKey(NumberUtils.toLong(wsCode));
          }

          不知道還有沒有其他好的解決方案。


          posted on 2007-04-17 08:22 布衣郎 閱讀(1587) 評(píng)論(1)  編輯  收藏 所屬分類: jdk相關(guān)

          FeedBack:
          # re: 用泛型時(shí)遇到的尷尬問題
          2007-04-17 17:57 | aaa
          確實(shí)是個(gè)問題,hibernate映射的時(shí)候必須指定類型為L(zhǎng)ong,不然就被認(rèn)成Serialiazable,xwork2的ValueStack是可以認(rèn)出來精確的類型,但是在jdk6.0下面還是不行.
          應(yīng)該用java.lang.reflection.Method.getGenericReturnType()而不是用getReturnType()  回復(fù)  更多評(píng)論
            

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


          網(wǎng)站導(dǎo)航:
           

          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(12)

          隨筆分類(59)

          隨筆檔案(57)

          blog

          java

          uml

          搜索

          •  

          積分與排名

          • 積分 - 358034
          • 排名 - 156

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 长乐市| 利辛县| 石家庄市| 大足县| 凭祥市| 怀安县| 贡嘎县| 新巴尔虎右旗| 土默特右旗| 定边县| 日照市| 馆陶县| 苍山县| 长泰县| 华蓥市| 商洛市| 乌兰察布市| 临猗县| 丰镇市| 尼木县| 淮北市| 永丰县| 微山县| 邢台县| 宜良县| 沁阳市| 宁安市| 隆尧县| 靖边县| 项城市| 洮南市| 临漳县| 嘉鱼县| 安国市| 日土县| 韩城市| 江阴市| 西畴县| 长治市| 昌黎县| 崇文区|