Does Java have a method to inspect object type?
In Javascript, if you want to inspect some object in chrome,
cons开发者_JS百科ole.log(object)
will printout variables, and methods about the object.
Ruby also has object.inspect
which returns basic information about the object.
What code should I use in Java to inspect an object?
All the code you need to inspect Java objects is in the java.lang.reflect package.
You'll have to write a fair bit of code yourself using that API though.
Apache's BeanUtils is somewhat easier.
You want to use the Java Reflection API.
For example, try this:
Class c = object.getClass();
System.out.writeln("Looks like you have a "+c.getCanonicalName());
If you need it at Runtime from you code, something like console.log
is provided by log4j
. The information you'll log can be retrieved via Java Reflection
For dumping Java objects, look at the answers over here for various ways of doing it. I'd use XStream
for its ease of use and maturity.
If you need external tools to look into the VM, tools like Java VisualVM(jvisualvm.exe
) and JConsole(jconsole.exe
) are very useful.
精彩评论