问题
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