Often users of Clojure wish to be as lazy as possible, and delay the creation of classes and objects. In that same spirit, if I wish to invoke a native function which is resolved during runtime from within Java, I can use com.sun.jna.Function.getFunction("foolibrary", "foofuncname")
, which returns a com.sun.jna.Function
, which can be invoked
.
In Clojure this looks like:
(let [f (com.sun.jna.Function/getFunction "c" "printf")]
(.invoke f Integer (to-array ["Hello World"])))
BridJ, on the other hand, offers an attractive performance benefit and claimed simplier API, however, it is still not clear to me how to use BridJ to do something similar to the runtime binding JNA example. Can someone demonstrate how? Also, if this is possible, are there any performance penalties with this approach? Otherwise, it appears generating the Java source file ahead of time is the only solution. I would appreciate it if someone could confirm this.
Edit:
After better understanding the question & focusing on "dynamically" (without pre-compilation), I still hesitate to claim "it is impossible" ("impossible" is a very strong word/meaning ...like "always"/"never"), but I am very sure that this is not the standard routine of BridJ. I can think of a dynamical solution with Bridj, but this would be very probably dependent from "JNAerator" and this in turn would be dependent from "JNA" (your starting position).
The original answer, describing the "standard routine" for "dynamically invoke any native function with BridJ" (involving code generation):
According to https://code.google.com/p/bridj/ and https://code.google.com/p/bridj/wiki/FAQ, you'll have to:
Sample taken from "their Quickstart":
(this is generated java code)
Hope this helps!