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

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


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

          當在spring環境中時,使用spring的bind類處理的時候,發現,我的code.primaryKey的類型居然為Serializable,而不是我想要的Long
          當時以為其他地方搞錯了,寫了個簡單的測試代碼
          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);

          才成功。
          仔細跟蹤,發現原來,泛型的時候,產生的編譯類中,有兩個同名的方法
           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規范對于這種情況是沒有辦法分清楚,所以也就導致了結果和預期的不同。
          如果是在程序中,這點算不了問題??墒俏倚枰趙eb的頁面上進行值的綁定,而輸入的東西,只能為字符串。所以不可能出來Long類型。
          目前,采用了一種比較傻的辦法,在TestBean中增加了一個臨時變量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 布衣郎 閱讀(1575) 評論(1)  編輯  收藏 所屬分類: jdk相關

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

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

          常用鏈接

          留言簿(12)

          隨筆分類(59)

          隨筆檔案(57)

          blog

          java

          uml

          搜索

          •  

          積分與排名

          • 積分 - 357342
          • 排名 - 155

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 通州市| 西乌珠穆沁旗| 合山市| 独山县| 华池县| 封丘县| 成安县| 平邑县| 邛崃市| 呈贡县| 乐业县| 临沭县| 沁源县| 区。| 突泉县| 台中县| 宜君县| 横山县| 嘉兴市| 奇台县| 许昌县| 临安市| 改则县| 象山县| 仙桃市| 香港| 深圳市| 进贤县| 永寿县| 贵德县| 阿拉善盟| 蚌埠市| 永顺县| 五华县| 祁东县| 象山县| 怀柔区| 阳原县| 大城县| 昌吉市| 商洛市|