利用awk自动生成DTO

      大多用于ejb开发的工具都支持dto的自动生成,前提是必须已经存在对应的实体bean。在没有实体bean时,就得我们自己敲那些繁琐无聊毫无意义的代码。这种情况下,这个awk script也许可以帮点忙。 如果你不了解awk,可以去
     http://www-900.ibm.com/developerWorks/cn/linux/shell/awk/awk-1/index.shtml
     http://www-900.ibm.com/developerWorks/cn/linux/shell/awk/awk-2/index.shtml
     http://www-900.ibm.com/developerWorks/cn/linux/shell/awk/awk-3/index.shtml
看看.  如果用的是windows,要想运行这个script,需要去down一个名为awk.exe的解释程序。 

对文本要求的格式:
    第一行 是 dto 的名字,
    其余行 是"类型-变量名"对, 注意别加分号
    如下例:
    User_DTO
    int          id
    String       name
    Collection   friends
以下斜体部分是script正文:

BEGIN{
    count =0 ;
}
{
    if(NF==2)
    {
       arr[count,0] = $1;
       arr[count,1] = $2;
   
       #print arr[count,0], arr[count,1], count;
       count++;
    }

    if(NF==1)
      dto = $1;
}

END{
    printf("/* DTO created by awk*//n/n");
    printf("
/*@todo Complete package & import here*//n/n");
    printf("public class %s implements Serializable/n{/n", dto);

    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       arrx = arr[i,0];
      
       printf("/tprivate %s %s;/n", arrx, x);
    }

    printf("/n/n");

    printf("/tpublic %s()/n/t{/n/t}/n/n", dto);

    printf("/tpublic %s(", dto);
    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       arrx = arr[i,0];      
       printf("%s %s", arrx, x);

       if(i!=count-1)
         printf(", ");
    }
    printf(")/n/t{/n");

    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       arrx = arr[i,0];      
       printf("/t/tthis.%s = %s;/n", x, x);
    }

    printf("/t}/n/n");


    # setters & getters
    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       arrx = arr[i,0];
      
       xHead = toupper(substr(x,1,1));
       xBody= substr(x,2,length(x)-1);
       printf("/tpublic void set%s%s(%s %s)/n/t{/n", xHead, xBody, arrx, x);
       printf("/t/tthis.%s = %s;/n", x, x);
       printf("/t}/n/n");

       printf("/tpublic %s get%s%s()/n/t{/n", arrx, xHead, xBody, arrx, x);
       printf("/t/treturn %s;/n", x);
       printf("/t}/n/n");
    }
   
    #  equals
    printf("/n/n");
    printf("/tpublic boolean equals(Object obj)/n/t{/n");
    printf("/t/tif (obj != null)/n/t/t{/n");
    printf("/t/t/tif (this.getClass().equals(obj.getClass()))/n/t/t/t{/n");
    printf("/t/t/t/t%s that = (%s) obj;/n", dto, dto);
    printf("/t/t/t/treturn /n");
    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       arrx = arr[i,0];      
       xHead = toupper(substr(x,1,1));
       xBody= substr(x,2,length(x)-1);
       if(arrx=="byte" || arrx=="char" || arrx=="int" || arrx=="long" || arrx=="float" || arrx=="double" || arrx=="boolean" )
       {
          printf("/t/t/t/t/tthis.get%s%s() == that.get%s%s() ", xHead, xBody, xHead, xBody);
       }else
       {
          printf("/t/t/t/t/t(((this.get%s%s() == null) && (that.get%s%s() == null)) ||/n",  xHead, xBody, xHead, xBody);
          printf("/t/t/t/t/t/t(this.get%s%s() != null && this.get%s%s().equals(that.get%s%s())))",  xHead, xBody, xHead, xBody, xHead, xBody);
       }
       if(i!=count-1)
         printf(" && /n");
       else
         printf(";/n");
    }
    printf("/t/t/t}/n/t/t}/n/t/treturn false;/n/t}/n/n");


    #  hashCode()
    printf("/tpublic int hashCode()/n/t{/n");
    printf("/t/treturn (");
    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       if(i!=0)
         printf(" + ");

       printf("/"/" + %s", x);
    }
    printf(").hashCode();");
    printf("/n/t}/n/n");

   
    # toString()
    printf("/tpublic String toString()/n/t{/n");
    printf("/t/treturn ");
    for ( i=0; i< count; i++ ) {
       x = arr[i,1];
       if(i==0)
         printf("/"/" + ");
       else
         printf(" + /",/" + ");

       printf("%s", x);
    }
    printf(";");
    printf("/n/t}/n/n");  

    printf("}");
}

#########################################################
#   如果有bug,请告诉我,thanks! :)
#   YanJian, msn/email:
yanjians@msn.com
#   2004.03.30
#########################################################

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值