Call to invokeStaticMethod can't be resolved? - clojure

I've written a rather primitive test program (I am writing a wrapper around LWJGL's OpenGL classes for mostly aesthetic reasons, and I decided to bring multi-threading in there because I actually never wrote a concurrent program before). When the program finishes, I always get the warning
I actually get the warning before the program enters the main function. Sorry for the confusion (I think this may not be my program at all, but rather something in clojure itself):
Reflection warning, NO_SOURCE_PATH:1 - call to invokeStaticMethod can't be resolved.
I don't get any warnings during compilation though, which I find rather strange. Anyway, here is the program:
(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)))
This probably isn't too idiomatic (I heard that in Clojure you normally don't start threads with new Thread, but rather with Agents or something, but I don't fully grasp how that works yet), but I guess for such a short program that doesn't matter.

Your Clojure source code is being loaded and compiled at runtime (see http://clojure.org/compilation), which is why you don't see the reflection warning until program execution.
It is difficult to determine where you're making a reflective call against a static method, so here are steps I recommend:
Add (:gen-class) to your (ns) declaration
Add (set! *warn-on-reflection* true) or (binding [*warn-on-reflection* true] body) where you suspect the reflective call is occurring.

Related

Programmatically redef functions

I'm currently in the start of making a tool that should modify all the functions in another namespace, and the run the "main" function in that (other) namespace. Almost like mocking in unit-testing, but for another purpose. The first step to this is to redefine functions from another namespace. The first listing shows code that works, where I explicitly name the function to be replaced.
(ns one)
(defn a-fun []
(println "original"))
(defn caller []
(a-fun))
(ns two)
(with-redefs [one/a-fun #(println "replaced")]
(one/caller))
;; replaced
;; nil
The next step is getting the functions programmatically from the other namespace. This is where I have run into a wall. I've experimented much with ns-publics, but so far no luck. So I try to break the problem down. And the problem is that the replacement of the function is not working. The output should print "replaced" not "original".
(ns two)
(def target (ns-resolve 'one (symbol "a-fun")))
(def caller (ns-resolve 'one (symbol "caller")))
(with-redefs [target #(println "replaced")]
(caller))
;; original
;; nil
Now, the #'two/target shows #'one/a-fun when I evaluate it in the repl. I can call one/a-fun by entering ((ns-resolve 'one (symbol "a-fun"))) in the repl, so I don't understand what part is not working.
I've been through a lot of documentation today, and I'm not really getting any closer.
You could try using with-redefs-fn like so:
(defn p [] "old")
(with-redefs-fn
{(ns-resolve *ns* 'p) #(println "new")}
(fn []
(p)))
;; => new
This does mean that the body of with-redefs-fn must be a function that uses your redefined Vars.

How does the binding function work with core.async?

Is it ok to use binding with core.async? I'm using ClojureScript so core.async is very different.
(def ^:dynamic token "no-token")
(defn call
[path body]
(http-post (str host path) (merge {:headers {"X-token" token}} body)))) ; returns a core.async channel
(defn user-settings
[req]
(call "/api/user/settings" req))
; elsewhere after I've logged in
(let [token (async/<! (api/login {:user "me" :pass "pass"}))]
(binding
[token token]
(user-settings {:all-settings true})))
In ClojureScript1, binding is basically with-redefs plus an extra check that the Vars involved are marked :dynamic. On the other hand, gos get scheduled for execution1 in chunks (that is, they may be "parked" and later resumed, and interleaving between go blocks is arbitrary). These models don't mesh very well at all.
In short, no, please use explicitly-passed arguments instead.
1 The details are different in Clojure, but the conclusion remains the same.
2 Using the fastest mechanism possible, setTimeout with a time of 0 if nothing better is available.

core.async machine that loops until a fx returns true

While loading a web site, I often have the need to load code AFTER some other DOM structural event has occurred. Therefore I often end up with a lot of functions that first check if some element exist in the DOM and then do their thing in the event that said element exist.
(ns example.core
(:require
[cljs.core.async :as async]
goog.async.AnimationDelay)
(:require-macros
[cljs.core.async.macros :refer [go go-loop]]))
(defn register-click-handler []
(when js/window.document.body
(goog.events.listen
js/window.document.body
goog.events.EventType.CLICK
#(js/console.log "I was clicked!"))
true))
(defn loop-until-true [fx]
(let [c (async/chan)
f (goog.async.AnimationDelay. #(async/put! c :loop))]
(async/put! c :initial-loop)
(go-loop []
(async/<! c)
(when-not (true? (fx))
(.start f)
(recur)))))
(loop-until-true register-click-handler)
I think I like this pattern as it lets me write fx that keeps trying until the DOM is in a state where the function can succeed (return true); without having to go through a great deal of ceremony in terms of dealing with retrying (just return true on success).
I am looking for improvements to loop-until-true, a function that registers another function to keep executing until that function returns true. I don't want to actually block the execution of the code that might cause fx to pass, so I am using a core.async block to IOC the code. This code seems to work, but I am looking for improvements and critics.
<body onload="f()"> and place your code that listens on click into (defn f [] …).

Finding vars from dynamically created namespaces in clojure

The following test fails:
(ns clojure_refactoring.rename-fn-test
(:use clojure.test))
(deftest test-fn-location
(in-ns 'refactoring-test-fn-rename)
(clojure.core/refer-clojure)
(defn a [b] (inc b))
(in-ns 'clojure_refactoring.rename-fn-test)
(is (not= (find-var 'refactoring-test-fn-rename/a)
nil))
(remove-ns 'refactoring-test-fn-rename))
That is, find-var (of a var I've just created, in a namespace I've just create) returns nil. This behaviour doesn't happen at the repl, where typing out the steps of the test works just fine.
Am I doing something wrong, or is this just something that doesn't work in clojure right now?
Updated to a version which really seems to do the intended thing, in contrast to my original answer...
This version seems to work:
(ns clojure-refactoring.rename-fn-test
(:use clojure.test
[clojure.contrib.with-ns :only [with-ns]]))
(deftest test-fn-location
(create-ns 'refactoring-test-fn-rename)
(with-ns 'refactoring-test-fn-rename
(clojure.core/refer-clojure)
(defn a [b] (inc b)))
(is (not= (find-var 'refactoring-test-fn-rename/a)
nil))
(remove-ns 'refactoring-test-fn-rename))
Also, you really need to change all occurrences of _ in namespace names to -, and the other way around for filenames.
With these changes in place, the test runs fine for me. (I haven't even tried to run it Apparently it still works without making the _ / - changes, but really, you need to do it! That's the accepted convention and things are not guaranteed to work if you don't follow it.)
For some reason, the code from the question seems to have been creating the Var a in the namespace the test was being defined in, thus (find-var 'clojure-refactoring.rename-fn-test/a) was returning a Var, whereas the test failed. With the above, (find-var 'clojure-refactoring.rename-fn-test/a) returns nil, as expected.

Improving my first Clojure program

After a few weekends exploring Clojure I came up with this program. It allows you to move a little rectangle in a window. Here's the code:
(import java.awt.Color)
(import java.awt.Dimension)
(import java.awt.event.KeyListener)
(import javax.swing.JFrame)
(import javax.swing.JPanel)
(def x (ref 0))
(def y (ref 0))
(def panel
(proxy [JPanel KeyListener] []
(getPreferredSize [] (Dimension. 100 100))
(keyPressed [e]
(let [keyCode (.getKeyCode e)]
(if (== 37 keyCode) (dosync (alter x dec))
(if (== 38 keyCode) (dosync (alter y dec))
(if (== 39 keyCode) (dosync (alter x inc))
(if (== 40 keyCode) (dosync (alter y inc))
(println keyCode)))))))
(keyReleased [e])
(keyTyped [e])))
(doto panel
(.setFocusable true)
(.addKeyListener panel))
(def frame (JFrame. "Test"))
(doto frame
(.add panel)
(.pack)
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(.setVisible true))
(defn drawRectangle [p]
(doto (.getGraphics p)
(.setColor (java.awt.Color/WHITE))
(.fillRect 0 0 100 100)
(.setColor (java.awt.Color/BLUE))
(.fillRect (* 10 (deref x)) (* 10 (deref y)) 10 10)))
(loop []
(drawRectangle panel)
(Thread/sleep 10)
(recur))
Despite being an experienced C++ programmer I found it very challenging to write even a simple application in a language that uses a radically different style than what I'm used to.
On top of that, this code probably sucks. I suspect the globalness of the various values is a bad thing. It's also not clear to me if it's appropriate to use references here for the x and y values.
Any hints for improving this code are welcome.
Those ifs in keyPressed can be replaced with a single case. Also, the dosync can be moved outside to wrap the case. In fact, alter can be moved out too, so that if you e.g. decide to change it to commute, there's just the one place to make the change. The result:
(def panel
(proxy [JPanel KeyListener] []
(getPreferredSize [] (Dimension. 100 100))
(keyPressed [e]
(let [keyCode (.getKeyCode e)]
(dosync
(apply alter
(case keyCode
37 [x dec]
38 [y dec]
39 [x inc]
40 [y inc])))
(println keyCode)))
(keyReleased [e])
(keyTyped [e])))
You could also rewrite the imports more concisely:
(import [java.awt Color Dimension event.ActionListener])
(import [javax.swing JFrame JPanel])
-- whether you'd want to is a matter of style.
I would rename drawRectangle to draw-rectangle (that's the idiomatic style for function names in Clojure) and, more to the point, rewrite it as a pure function accepting the coordinates as explicit arguments. Then you could write a small wrapper around that to use your Refs, if indeed your design would benefit from the use of Refs. (Hard to say without knowing how you might want to use & modify them etc.)
Prefer while to (loop [] ... (recur)) (see (doc while) and (clojure.contrib.repl-utils/source while)).
Also -- and this is important -- don't put anything except definitions at top level. That's because top-level forms are actually executed when the code is compiled (try loading a library with a (println :foo) at top level). That infinite loop should be wrapped inside a function; the standard name for a "main" function in Clojure is -main; same goes for panel and frame. This doesn't apply when playing around at the REPL, of course, but it's an important gotcha to know about.
Incidentally, (doto foo ...) returns foo, so you can just write (doto (proxy ...) (.setFocusable true) ...).
Otherwise, I'd say the code is fine. Normally you'd want to put it in a namespace; then all the imports would go in the ns form.
HTH
Edit, while writing the below post in a hurry I forgot to say you must not put parens around constants eg java.awt.Color/WHITE.
In addition to Michał's comments:
when you use an infinite loop, condition it with an atom (eg #switch) thus you'll be able to stop it (unless the loops runs on the repl thread -- so lauch it in a separat thread with "future")
you use refs, so it means the values of x and y are intended to be coordinated (in your code they are not: each update is independent); hence in drawRectangle you should wrap the reads in a dosync to be sure that you get a consistent view -- again in your actual code it doesn't matter because x and y are always updated independently.
hth,
(shameless plug: http://conj-labs.eu/)
Although not exactly Clojure advice - consider using KeyAdapter instead of KeyListener. That way you won't have to provide empty implementations for keyReleased and keyTyped. It's generally a good rule to use adapter classes when you're not needing all of the methods of a Listener interface.