在传递非基本对象数组时避免反射

时间:2012-04-27 18:58:02

标签: clojure

我正在从Clojure 1.4.0中调用com.rabbitmq.client.ConnectionFactory.newConnection(Address[]),如下所示:

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

通话有效,但会发出反射警告:

call to newConnection can't be resolved.

反映在ConnectionFactory类上,显然有一个表单只接受com.rabbitmq.client.Address[]类型的单个参数,这正是(into-array Address ())返回的内容:

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>

我需要做些什么来避免反思?

1 个答案:

答案 0 :(得分:1)

不幸的是,高阶函数总是使用Object作为它们的参数类型,并且编译器还没有跟随调用HOF的类型提示。这基本上是因为它编译了匿名函数

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

才知道它是如何使用的。这可能很快就会改善。

如果你可以解决filter

的电话,你可以解决这个问题