开发者

Does Java have a method to inspect object type?

开发者 https://www.devze.com 2023-04-02 03:24 出处:网络
In Javascript, if you want to inspect some object in chrome, cons开发者_JS百科ole.log(object) will printout variables, and methods about the object.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号