(This answer now includes two working solutions, one based on my original idea with intern and one based on danlei's suggestion for using ccimport-static . I think I will need to clean this up a bit later, but now I can’t spend anymore it takes more time ...)
To extract static fields:
(filter
Then match #(intern *ns* (str "a-prefix-" (.getName %)) (.get YourClass nil)) to get the value ... Note: this bit is not checked and in particular I'm not sure about this nil in .get ; experiment with java.lang.Field and see what works with your class.
Update 2:
Well, actually the intern based approach is not that badly readable:
user> (map #(intern *ns* (symbol (str "integer-" (.getName %))) (.get % java.lang.Integer)) (filter #(bit-and java.lang.reflect.Modifier/STATIC (.getModifiers %)) (.getFields java.lang.Integer))) (#'user/integer-MIN_VALUE #'user/integer-MAX_VALUE #'user/integer-TYPE #'user/integer-SIZE) user> integer-MIN_VALUE -2147483648 user> integer-MAX_VALUE 2147483647 user> integer-TYPE int user> integer-SIZE 32
Update: (leaving the first update in place as an alternative solution)
Combining the knowledge of danlei clojure.contrib with the above, we get the following:
user> (map
It uses eval ... well, so it’s unlikely that it will "kill performance", and in fact it is readable enough that a complex expression using intern may not be. (This is not so bad ... :-)) If you prefer intern , although at least the import-static implementation can give you the right ideas if my sketch above turns out to be incorrect.
Michał Marczyk
source share