Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><em>(This answer now includes two working solutions, one based on my initial idea with <code>intern</code> and one based on danlei's suggestion to use <code>c.c.import-static</code>. I guess I'll need to clean this up a bit later, but I can't spend more time on it now...)</em></p> <p>To extract static fields:</p> <pre><code>(filter #(bit-and java.lang.reflect.Modifier/STATIC (.getModifiers %)) (.getFields YourClass)) </code></pre> <p><strike>Then map <code>#(intern *ns* (str "a-prefix-" (.getName %)) (.get YourClass nil))</code> across that sequence to obtain the value... Note that <strong>this bit is untested</strong> and, in particular, I'm not sure about that <code>nil</code> in <code>.get</code>; experiment with <code>java.lang.Field</code> and see what works with your class.</strike></p> <p><strong>Update 2:</strong></p> <p>Ok, actually an <code>intern</code> based approach is not that bad readability-wise:</p> <pre><code>user&gt; (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&gt; integer-MIN_VALUE -2147483648 user&gt; integer-MAX_VALUE 2147483647 user&gt; integer-TYPE int user&gt; integer-SIZE 32 </code></pre> <p><strong>Update:</strong> <em>(leaving the first update in place as an alternative solution)</em></p> <p>Combining danlei's knowledge of <code>clojure.contrib</code> with the above yields the following:</p> <pre><code>user&gt; (map #(eval `(import-static java.lang.Integer ~(symbol (.getName %)))) (filter #(bit-and java.lang.reflect.Modifier/STATIC (.getModifiers %)) (.getFields java.lang.Integer))) (#'user/MIN_VALUE #'user/MAX_VALUE #'user/TYPE #'user/SIZE) user&gt; MIN_VALUE -2147483648 user&gt; MAX_VALUE 2147483647 user&gt; TYPE int user&gt; SIZE 32 </code></pre> <p>It uses <code>eval</code>... well, so what, it's hardly going to "kill performance" and it's actually fairly readable, which an elaborate expression using <code>intern</code> might not be. <em>(It's not so bad actually... :-))</em> <strike>If you prefer <code>intern</code>, though, at least the implementation of <code>import-static</code> can give you the proper ideas if my sketch above turns out to be incorrect somehow.</strike></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload