Question

I am having trouble using a Java interface from Clojure.

I have the folowing class: public class OpenAccess

which has a method: static Connection connect(String url)

where Connection is a interface: public interface Connection

In Java I would do this to setup a connection:

Connection conn = OpenAccess.connect(url);

I tried the following from Clojure but it doesn't work:

(defn connection [url]
  (let [oa (access.OpenAccess.)
    connection (reify access.Connection
             .....
(.connect oa connection)))

it errors with "IllegalArgumentException No matching method found: connect for class access.OpenAccess"

I can't figure out how to properly execute Java interfaces from Clojure.

Was it helpful?

Solution

Looks like a static call:

(defn connection [url]
  (OpenAccess/connect url))

And you would use it like this if you needed to type-hint it:

(let [^Connection conn (connection "http://foo")]
  // use your conn 
  )

You don't need the ^Connection but it will tell the compiler the type of the method invocations on conn which will avoid reflection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top