list available java packages and classes within clojure
Asked Answered
B

2

10

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.

Barkley answered 17/4, 2015 at 4:17 Comment(0)
G
2

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!

Gervase answered 28/5, 2015 at 19:34 Comment(0)
P
1

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.

Pilch answered 20/4, 2015 at 18:34 Comment(1)
Here are things that can make this answer way better: a link to that library and an example of code that actually lists classes. Or at least a link to the docs that has or references one.Confidante

© 2022 - 2024 — McMap. All rights reserved.