Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1372950
  • 博文数量: 205
  • 博客积分: 6732
  • 博客等级: 准将
  • 技术积分: 2835
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-04 17:59
文章分类

全部博文(205)

文章存档

2016年(1)

2015年(10)

2014年(1)

2013年(39)

2012年(23)

2011年(27)

2010年(21)

2009年(55)

2008年(28)

我的朋友

分类: Java

2013-11-18 14:59:53

接口调用中经常会打印一些参数的值,但是一个对象的属性太多,一个一个的打印,太麻烦了。
于是就产生了下面的代码。


点击(此处)折叠或打开

  1. public String outObjPropertyString(Object obj)
  2.     {
  3.         StringBuffer sb = new StringBuffer();
  4.         if(null != obj)
  5.         {
  6.             try {
  7.                 this.getPropertyString(obj, sb);
  8.             }
  9.             catch (Exception e) {
  10.                 logger.log("outObjPropertyString is error " + e.getMessage());
  11.                 e.printStackTrace();
  12.             }
  13.         }
  14.         return sb.toString();
  15.     }
  16.     
  17.     public String getPropertyString(Object entityName, StringBuffer sb) throws Exception {
  18.         Class c = entityName.getClass();
  19.         Field field [] = c.getDeclaredFields();
  20.         Object obj = null;
  21.         String classname = "";
  22.         Object tempObj = null;
  23.         sb.append("------ " + " begin ------\n");
  24.         for(Field f : field){

  25.             sb.append(f.getName());
  26.             sb.append(" : ");
  27.             obj = invokeMethod(entityName,f.getName(),f.getType(),null);
  28.             if(null != obj)
  29.             {
  30.                 if(obj.getClass().isArray())
  31.                 {
  32.                     for (int i=0;i<Array.getLength(obj);i++)
  33.                     {
  34.                         tempObj = Array.get(obj, i);
  35.                         if(tempObj.getClass().isPrimitive())
  36.                         {
  37.                             sb.append(tempObj.toString());
  38.                         }
  39.                         else if(tempObj instanceof String)
  40.                         {
  41.                             sb.append(tempObj.toString());
  42.                         }
  43.                         else if(tempObj instanceof Date)
  44.                         {
  45.                             sb.append(tempObj.toString());
  46.                         }
  47.                         else if(tempObj instanceof Number)
  48.                         {
  49.                             sb.append(tempObj.toString());
  50.                         }
  51.                         else
  52.                         {
  53.                             this.getPropertyString(tempObj , sb);
  54.                         }
  55.                     }
  56.                 }
  57.                 
  58.                 classname = obj.getClass().getName();
  59.                 if(classname.indexOf("com.cignacmb.core.model.") > -1)
  60.                 {
  61.                     this.getPropertyString(obj , sb);
  62.                 }

  63.             }
  64.             
  65.             /*if (f.getType() == Address.class)
  66.             {
  67.                 this.getPropertyString(obj , sb);
  68.             }*/
  69.             
  70.             sb.append(obj);
  71.             sb.append("\n");
  72.         }
  73.         sb.append("------ " + " end ------\n");
  74.         return sb.toString();
  75.     }
  76.     
  77.     
  78.     public Object invokeMethod(Object owner, String methodName, Class fieldType, Object[] args) throws Exception{
  79.         Class ownerClass = owner.getClass();

  80.         methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
  81.         Method method = null;
  82.         try {
  83.             if(fieldType == boolean.class)
  84.             {
  85.                 method = ownerClass.getMethod("is" + methodName);
  86.             }
  87.             else
  88.             {
  89.                 method = ownerClass.getMethod("get" + methodName);
  90.             }
  91.         } catch (SecurityException e) {
  92.             e.printStackTrace();
  93.         } catch (NoSuchMethodException e) {
  94.             //e.printStackTrace();

  95.             return " can't find 'get" + methodName + "' method";
  96.         }

  97.         return method.invoke(owner);
  98.         
  99.     }

 


阅读(7455) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~