Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent to python "dir"?

Is there an equivalent to "dir" in python for java, or a library which provides similar functionality (i.e. properties of objects and classes output as informative strings)?

This question is similar this question for clojure and probably has something to do with Java Reflection as in this question, which seems to be about a more complicated, but similar, topic.

like image 627
tjb Avatar asked Mar 10 '11 05:03

tjb


1 Answers

There's nothing in the standard library that accomplishes exactly what dir() does, but you can get the same information using java.lang.reflect. Specifically, investigating and discovering members of a class is explained in the documentation on discovering class members. Using that API you can easily find out what you need to know about the attributes of a class.

In fact, implementing dir() yourself would just be a matter of defining a method that introspects the methods and fields of a class and either assembles a collection of the info or prints whatever information you'd like to know.

dir() has limited usefulness in Java because Java is not interactive, but if you need it for educational/investigative purposes or for application logic, the reflection API is always there.

like image 84
Rafe Kettler Avatar answered Sep 30 '22 02:09

Rafe Kettler