`
jiajunde
  • 浏览: 166539 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

打印对象里的内容

J# 
阅读更多

import java.util.*;
import java.lang.reflect.*;

public class TypeUtil {

 /**
  * Returns a string holding the contents of the passed object,
  *
  * @param scope
  *            String
  * @param parentObject
  *            Object
  * @param visitedObjs
  *            List
  * @return String
  */

 private static String complexTypeToString(String scope,
   Object parentObject, List visitedObjs) {

  StringBuffer buffer = new StringBuffer("");

  try {
   //
   // Ok, now we need to reflect into the object and add its child
   // nodes...
   //

   Class cl = parentObject.getClass();
   while (cl != null) {

    processFields(cl.getDeclaredFields(), scope, parentObject,
      buffer, visitedObjs);

    cl = cl.getSuperclass();
   }
  } catch (IllegalAccessException iae) {
   buffer.append(iae.toString());
  }

  return (buffer.toString());
 }

 /**
  * Method processFields
  *
  * @param fields
  *            Field[]
  * @param scope
  *            String
  * @param parentObject
  *            Object
  * @param buffer
  *            StringBuffer
  * @param visitedObjs
  *            List
  * @throws IllegalAccessException
  */
 private static void processFields(Field[] fields, String scope,
   Object parentObject, StringBuffer buffer, List visitedObjs)
   throws IllegalAccessException {

  for (int i = 0; i < fields.length; i++) {

   //
   // Disregard certain fields for IDL structures
   //
   if (fields[i].getName().equals("__discriminator")
     || fields[i].getName().equals("__uninitialized")) {
    continue;
   }

   //
   // This allows us to see non-public fields. We might need to deal
   // with some
   // SecurityManager issues here once it is outside of VAJ...
   //
   fields[i].setAccessible(true);

   if (Modifier.isStatic(fields[i].getModifiers())) {
    //
    // Ignore all static members. The classes that this dehydrator
    // is
    // meant to handle are simple data objects, so static members
    // have no
    // bearing....
    //
   } else {
    buffer.append(typeToString(scope + "." + fields[i].getName(),
      fields[i].get(parentObject), visitedObjs));
   }
  }

 }

 /**
  * Method isCollectionType
  *
  * @param obj
  *            Object
  * @return boolean
  */
 public static boolean isCollectionType(Object obj) {

  return (obj.getClass().isArray() || (obj instanceof Collection)
    || (obj instanceof Hashtable) || (obj instanceof HashMap)
    || (obj instanceof HashSet) || (obj instanceof List) || (obj instanceof AbstractMap));
 }

 /**
  * Method isComplexType
  *
  * @param obj
  *            Object
  * @return boolean
  */
 public static boolean isComplexType(Object obj) {

  if (obj instanceof Boolean || obj instanceof Short
    || obj instanceof Byte || obj instanceof Integer
    || obj instanceof Long || obj instanceof Float
    || obj instanceof Character || obj instanceof Double
    || obj instanceof String) {

   return false;
  } else {

   Class objectClass = obj.getClass();

   if (objectClass == boolean.class || objectClass == Boolean.class
     || objectClass == short.class || objectClass == Short.class
     || objectClass == byte.class || objectClass == Byte.class
     || objectClass == int.class || objectClass == Integer.class
     || objectClass == long.class || objectClass == Long.class
     || objectClass == float.class || objectClass == Float.class
     || objectClass == char.class
     || objectClass == Character.class
     || objectClass == double.class
     || objectClass == Double.class
     || objectClass == String.class) {

    return false;

   }

   else {
    return true;
   }
  }
 }

 /**
  * Returns a string holding the contents of the passed object,
  *
  * @param scope
  *            String
  * @param obj
  *            Object
  * @param visitedObjs
  *            List
  * @return String
  */

 private static String collectionTypeToString(String scope, Object obj,
   List visitedObjs) {

  StringBuffer buffer = new StringBuffer("");

  if (obj.getClass().isArray()) {
   if (Array.getLength(obj) > 0) {

    for (int j = 0; j < Array.getLength(obj); j++) {

     Object x = Array.get(obj, j);

     buffer.append(typeToString(scope + "[" + j + "]", x,
       visitedObjs));
    }

   } else {
    buffer.append(scope + "[]: empty\n");
   }
  } else {
   boolean isCollection = (obj instanceof Collection);
   boolean isHashTable = (obj instanceof Hashtable);
   boolean isHashMap = (obj instanceof HashMap);
   boolean isHashSet = (obj instanceof HashSet);
   boolean isAbstractMap = (obj instanceof AbstractMap);
   boolean isMap = isAbstractMap || isHashMap || isHashTable;

   if (isMap) {
    Set keySet = ((Map) obj).keySet();
    Iterator iterator = keySet.iterator();
    int size = keySet.size();

    if (size > 0) {

     for (int j = 0; iterator.hasNext(); j++) {

      Object key = iterator.next();
      Object x = ((Map) obj).get(key);

      buffer.append(typeToString(scope + "[\"" + key + "\"]",
        x, visitedObjs));
     }
    } else {
     buffer.append(scope + "[]: empty\n");
    }
   } else if (/* isHashTable || */
   isCollection || isHashSet /* || isHashMap */
   ) {

    Iterator iterator = null;
    int size = 0;

    if (obj != null) {

     if (isCollection) {
      iterator = ((Collection) obj).iterator();
      size = ((Collection) obj).size();
     } else if (isHashTable) {
      iterator = ((Hashtable) obj).values().iterator();
      size = ((Hashtable) obj).size();
     } else if (isHashSet) {
      iterator = ((HashSet) obj).iterator();
      size = ((HashSet) obj).size();
     } else if (isHashMap) {
      iterator = ((HashMap) obj).values().iterator();
      size = ((HashMap) obj).size();
     }

     if (size > 0) {

      for (int j = 0; iterator.hasNext(); j++) {

       Object x = iterator.next();
       buffer.append(typeToString(scope + "[" + j + "]",
         x, visitedObjs));
      }
     } else {
      buffer.append(scope + "[]: empty\n");
     }
    } else {
     //
     // theObject is null
     buffer.append(scope + "[]: null\n");
    }
   }
  }

  return (buffer.toString());

 }

 /**
  * Method typeToString
  *
  * @param scope
  *            String
  * @param obj
  *            Object
  * @param visitedObjs
  *            List
  * @return String
  */
 private static String typeToString(String scope, Object obj,
   List visitedObjs) {

  if (obj == null) {
   return (scope + ": null\n");
  } else if (isCollectionType(obj)) {
   return collectionTypeToString(scope, obj, visitedObjs);
  } else if (isComplexType(obj)) {
   if (!visitedObjs.contains(obj)) {
    visitedObjs.add(obj);
    return complexTypeToString(scope, obj, visitedObjs);
   } else {
    return (scope + ": <already visited>\n");
   }
  } else {
   return (scope + ": " + obj.toString() + "\n");
  }
 }

 /**
  * The typeToString() method is used to dump the contents of a passed object
  * of any type (or collection) to a String. This can be very useful for
  * debugging code that manipulates complex structures.
  *
  * @param scope
  * @param obj
  *
  * @return String
  *
  */

 public static String typeToString(String scope, Object obj) {

  if (obj == null) {
   return (scope + ": null\n");
  } else if (isCollectionType(obj)) {
   return collectionTypeToString(scope, obj, new ArrayList());
  } else if (isComplexType(obj)) {
   return complexTypeToString(scope, obj, new ArrayList());
  } else {
   return (scope + ": " + obj.toString() + "\n");
  }
 }
 
 public static void main(String [] args){
  
  List list = new ArrayList();
  list.add("adf");
  list.add("adfccc");
  list.add("adfzxzc");
  
  System.out.println(TypeUtil.typeToString("list",list));
 }
}

分享到:
评论

相关推荐

    打印自定义复杂对象工具类

    一个可以打印复杂自定义对象的工具类,方便java代码查询和调试对象内容数据

    应用java反射机制打印一个对象.docx

    应用java反射机制打印一个对象.docx

    BarTender怎样批量打印EXCEL里的数据?

    很多时候,我们在BarTender条码打印软件里做标签时,涉及数据特别大,都保存在EXCLE表格里,那要怎么做,才可以使BarTender批量打印EXCEL数据呢?下面,小编就教教大家一种简单的批量打印标签的方法吧! 1. 在...

    js如何打印object对象

    js调试中经常会碰到输出的内容是对象而无法打印的时候,光靠alert只能打印出object标示,却不能打印出来里面的内容,甚是不方便,于是各方面整理总结了如下一个函数,能够将数组或者对象这类的结果一一打印出来,...

    CAD中ole类型表格打印PDF无法显示的解决方法

    CAD中OLE类型表格打印无法显示,解决办法如下:将插入进来的表格的属性格式调整一下即可

    免费DataGridView打印及.NET轻松打印控件5.6版(VB打印,C#打印)

    5.2版控件新增了一个Chartlet的组件,使用非常方便,可以生成柱形图、饼图、折线图等多种图形,而且可以设置2D或3D效果,既可以在打印控件中打印出来,也可以在Graphics对象中显示。 4、分组汇总打印DataGridVeiw...

    免费DataGridView打印及.NET轻松打印控件6.01版(VB打印,C#打印,图表打印,Excel导入导出,多表头显示与打印)

    5.2版控件新增了一个Chartlet的组件,使用非常方便,可以生成柱形图、饼图、折线图等多种图形,而且可以设置2D或3D效果,既可以在打印控件中打印出来,也可以在Graphics对象中显示。 4、文本打印输出功能,控件提供...

    精确打印工具软件5.0

    9.增加了文字对象的文字对象名称属性,这样您就可以使多个对象保持相同的打印内容,在打印金额大小写之类的内容时更加方便。 10.增加了文字对象的调整文字间隔属性,现在您可以任意控制打印字间距了。 11.增加了文字...

    关于axios返回空对象的问题解决

    但打印出来的时候就变成了空对象。 分析原因: 返回的参数都是正确的,只是打印的时候有问题,所以是打印的代码有误 查了一下 axios 的官方文档,才知道 console.log 的变量与字符串之间不能用 ‘+’ 连接,应该用 ...

    python 打印对象的所有属性值的方法

    以上这篇python 打印对象的所有属性值的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。 您可能感兴趣的文章:python 打印出所有的对象/模块的属性(实例代码)python ...

    Web打印控件(目前最强大最专业最新版本)

    2.如何选材打印当前页面内容见样例二 3.如何用代码生成打印页见样例三 4.如何打印设计和定位套打见样例四 5.如何控制纸张大小和连续打印见样例五 6.如何输出多页长文档及双面打印见样例六 7.如何定向输出见样例七 8....

    打印json对象的内容及JSON.stringify函数应用

    json对象的内容在调试的时候用的到通过JSON.stringify函数,可以转换json对象为字符串,接下来为大家详细介绍下,感兴趣的朋友可以参考下哈

    免费DataGridView打印及.NET轻松打印控件5.5版(VB打印,C#打印)

    借助该函数,您只需要在您的容器控件中设计好要打印的内容及打印内容的相对位置,控件轻松帮你打印出来(如果超过一页,控件会自动换页续打)。 13、特殊文字效果打印功能。控件具有打印浮雕文字、阴影文字、空心文字...

    RePrint表格打印控件(打印 datasource,dbgrid,stringgrid)

    PrintObject 选择打印的对象(datasource 、dbgrid、 stringgrid ) 5、pagefooter 页脚 pageheader 页眉 (1) Text:string;;内容 (2) Font:tfont;;字体 (3) Print:boolean;;是否打印 (4) Alignment:...

    MIS金智打印通

    完成了整体的框架、涉及打印的几个对话框、核心打印程序及几个基本打印对象并由BillPrinter类将其组织起来,程序基本定型。 本程序为通用打印程序,单据、会计凭证、发票清单、报表、任意复杂表格、合并表格如...

    免费DataGridView打印及.NET轻松打印控件5.7版(VB打印,C#打印,Excel导入导出,多表头显示与打印)

    5.2版控件新增了一个Chartlet的组件,使用非常方便,可以生成柱形图、饼图、折线图等多种图形,而且可以设置2D或3D效果,既可以在打印控件中打印出来,也可以在Graphics对象中显示。 4、分组汇总打印DataGridVeiw...

    打印软件(打印 datasource,dbgrid,stringgrid)

    Vertical 竖向打印(字段竖向排列适合打印字段内容较长的报表如“会议纪录”) (2)aotureturn 打印明细字段时,如果字段超过列的宽度是否允许自动换行 (3)colsline 设置竖线属性引用tpen类 (4)footer 设置...

    go-cliview:在不同的CLI视图中打印Go对象

    通过可自定义的格式和样式支持,在树形视图或表视图中打印复杂的数据对象。 用法 import ( cv "github.com/easeway/go-cliview" ) func ShowTable ( data [] interface {}) { cv. Table { Columns : []cv. ...

    张志晨VB2010实例教程之窗体打印扩展

    张志晨VB2010实例教程之窗体打印2011-10-07 07:06VB升级到2010后,许多网友不会在窗体上打印了。... ' str 要打印的内容,字符型 '函数有返回值,打印成功返回true,失败返回false '编者:张志晨 ' 2011年10月7日

Global site tag (gtag.js) - Google Analytics