Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Java fields dynamically in Clojure?

I'm a novice in clojure and java.

In order to access a Java field in Clojure you can do:

Classname/staticField

which is just the same as

(. Classname staticField)

(correct me if I'm wrong)

How can I access a static field when the name of the field in held within a variable? i.e.:

(let [key-stroke 'VK_L
      key-event KeyEvent/key-stroke])

I want key-stroke to be evaluated into the symbol VK_L before it tries to access the field.

like image 377
AnnanFay Avatar asked Jul 08 '11 20:07

AnnanFay


1 Answers

Reflection is probably the proper route to take, but the easiest way is

(eval (read-string (str "KeyEvent/" key-stroke)))

This will just convert the generated string into valid clojure and then evaluate it.

like image 169
jk. Avatar answered Oct 30 '22 23:10

jk.