1

I'm calling com.rabbitmq.client.ConnectionFactory.newConnection(Address[]) from Clojure 1.4.0, as follows:

(defn connect [#^ConnectionFactory factory, addrs]
  (.newConnection factory (into-array Address addrs)))

The call works, but it throws a reflection warning:

call to newConnection can't be resolved.

Reflecting on the ConnectionFactory class, there clearly is a form which takes only a single argument of type com.rabbitmq.client.Address[], and this is exactly what (into-array Address ()) is returning:

com.mefesto.wabbitmq=> (pprint (filter #(.. (.toString %)
                                            (contains "newConnection"))
                               (seq (.getDeclaredMethods
                                     ConnectionFactory))))
(#<Method public com.rabbitmq.client.Connection
    com.rabbitmq.client.ConnectionFactory.newConnection(
      com.rabbitmq.client.Address[])
    throws java.io.IOException>
 #<Method public com.rabbitmq.client.Connection
    com.rabbitmq.client.ConnectionFactory.newConnection(
      java.util.concurrent.ExecutorService,
      com.rabbitmq.client.Address[])
    throws java.io.IOException>
 #<Method public com.rabbitmq.client.Connection
    com.rabbitmq.client.ConnectionFactory.newConnection()
    throws java.io.IOException>
 #<Method public com.rabbitmq.client.Connection
    com.rabbitmq.client.ConnectionFactory.newConnection(
      java.util.concurrent.ExecutorService)
    throws java.io.IOException>)

com.mefesto.wabbitmq=> (into-array Address ())
#<Address[] [Lcom.rabbitmq.client.Address;@953235f>

What do I need to do to avoid reflection here?

1 Answer 1

1

Unfortunately Higher order functions always use Object as their argument type and the compiler does not yet follow type hints across call HOFs. This is basically because it compiles the anonymous function

#(.. (.toString %) (contains "newConnection"))

before it knows how it's used. This is likely to improve soon.

you may be able to get around this if you can work around the call to filter

Sign up to request clarification or add additional context in comments.

1 Comment

This is useful information, but not directly responsive to the question I intended to ask. The HOF given there isn't the one throwing the warning -- that was just demonstrating the availability of a newConnection method with an appropriate argument list. The actual code I'm concerned with for purposes of this question is only the connect function given in the first code block at the top, which doesn't use any HOFs as far as I can see.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.