调用invokeStaticMethod无法解决?

时间:2012-10-14 10:51:16

标签: clojure

我写了一个相当原始的测试程序(我正在编写一个围绕LWJGL的OpenGL类的包装器,主要是出于美学原因,我决定在那里引入多线程,因为我之前从未编写过并发程序)。 当程序结束时,我总是收到警告

在程序进入main函数之前,我实际上收到了警告。很抱歉这个混乱(我想这可能不是我的程序,而是clojure本身的东西):

Reflection warning, NO_SOURCE_PATH:1 - call to invokeStaticMethod can't be resolved.

我在编译期间没有收到任何警告,我觉得很奇怪。无论如何,这是程序:

(ns opengltuts.core
  (:import (org.lwjgl.opengl GL11 Display))
  (:use opengltuts.opengl)) ;my wrapper

(def world-state (ref {:running true :color [1.0 0.0 0.0 1.0]}))

(defn render-world [state]
  (apply glClearColor (:color state))
  (glClear GL11/GL_COLOR_BUFFER_BIT)
  (Display/update))

(defn render []
  (Display/create)
  (loop []
    (let [world @world-state]
      (when (:running world)
        (do (render-world world)
          (recur)))))
  (Display/destroy))

(defn -main []
  ; without the annotation, I get a warning here too.
  (let [render-thread (doto (Thread. ^Runnable render) (.start))]
    (Thread/sleep 3000)
    (dosync (commute world-state assoc :color [0.0 1.0 0.0 1.0]))
    (Thread/sleep 3000)
    (dosync (commute world-state assoc :running false))
    (.join render-thread)))

这可能不是太习惯了(我听说在Clojure中你通常不会用new Thread启动线程,而是使用Agents或者其他东西,但是我还没有完全掌握它是如何工作的),但我觉得这么短的节目并不重要。

1 个答案:

答案 0 :(得分:0)

您的Clojure源代码正在运行时加载和编译(请参阅http://clojure.org/compilation),这就是为什么在程序执行之前看不到反射警告的原因。

很难确定你在静态方法上进行反射调用的位置,所以这是我推荐的步骤:

  1. (:gen-class)添加到您的(ns)声明
  2. (set! *warn-on-reflection* true)(binding [*warn-on-reflection* true] body)添加到您怀疑正在进行反思调用的位置。