Look for standard library or technique to get pretty-printed representation of OBJECT for Java

╄→尐↘猪︶ㄣ 提交于 2019-12-12 10:59:43

问题


In order to understand internals of some code or print dumps on errors I use pp-like functions in Python and Emacs lisp.

Now I come to to Java and look for standard library or tecnique to get pretty-printed representation of OBJECT for Java.

Seems that current Java specification allow introspection of Java object at runtime. But introspection may be not so powerful. m(Object o) can not be called with new Object [] arg?

NOTE I am NOT looking to source code beautifier! I am looking for runtime pretty print Java object dumping.

NOTE2 These questions similar but not exactly same:

  • What's the simplest way to print a Java array?
  • Best pretty-printing library for Java?
  • Java dump an object

回答1:


You could use the ReflectionToStringBuilder from the Apache Commons Lang library.

Sample:

String dump = ReflectionToStringBuilder.toString(object);

As to your question:

m(Object o) can not be called with new Object [] arg?

Sure it can, arrays of Object is a subtype of Object.




回答2:


For a quick and dirty solution to show the output of a Java object, you could use Jackson http://jackson.codehaus.org/ this will output the object in JSON.



来源:https://stackoverflow.com/questions/9174268/look-for-standard-library-or-technique-to-get-pretty-printed-representation-of-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!