Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list available java packages and classes within clojure

In clojure, the answers to How to list the functions of a namespace? are useful if I have a namespace in mind, in particular (ns-imports 'my-package) can tell me about Java code that has been imported. I can find the list of namespaces with (all-ns). Is there a similar way to find the list of all available java packages, and to preview the java classes that have not been imported yet?

I'm making a lot of JARs and .class files available from Leiningen via :resource-paths and :java-source-paths respectively. But since I'm new to the Java codebase, I don't actually know off the top of my head what packages are defined in order to import them. I could do something with find, xargs, and grep, but it would be nice to have this information available programmatically. Furthermore, in the future, I would like to be able to do "reflection" over the Java codebase from within Clojure, for instance, noticing when a new class has been defined.

In the LISP world, there is a function called `do-external-symbols' and I googled for that, turning up this discussion from 2007. It seems things have been solved for Clojure itself, but it's less clear about Clojure's view of Java.

like image 995
Joe Corneli Avatar asked Apr 17 '15 04:04

Joe Corneli


2 Answers

If you are using cider/emacs or even just lein repl, there's good auto-complete functionality accessible by the tab-button.

=> (java.lang.B  <press tab>
java.lang.Boolean  java.lang.BootstrapMethodError  java.lang.Byte

This functionality seems to be driven by the library compliment, by Alexander Yakushev, so I suggest you take a close look on how it works!

like image 119
claj Avatar answered Oct 18 '22 16:10

claj


As far as i remember you can call Java code from Clojure. I recently discovered a Library called "Reflections" from Google that will list anything in your classpath for you. I have analysed classes that were JPA entities with this library, it was quite easy to use.

like image 41
Thomas Nagel Avatar answered Oct 18 '22 18:10

Thomas Nagel