tags:

views:

57

answers:

1

I'm trying to use some Java-classes with Clojure. I've tried it with Scala with success, but with Clojure, I get an IllegalArgumentException.

Here's the API: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/reflect/Method.html

The code below:

(import '(javax.sound.midi MidiSystem Sequencer Sequence))
(def mySequencer (MidiSystem/getSequencer))
(def mySequence (Sequence. Sequence/PPQ 10))
((.setSequence mySequencer) mySequence)

throws an exception:

Exception in thread "main" java.lang.IllegalArgumentException: No matching field found: setSequence for class com.sun.media.sound.RealTimeSequencer (recordmidi.clj:0)
    at clojure.lang.Compiler.eval(Compiler.java:5341)
    at clojure.lang.Compiler.load(Compiler.java:5736)
    at clojure.lang.Compiler.loadFile(Compiler.java:5699)
    at clojure.main$load_script__6268.invoke(main.clj:213)
    at clojure.main$script_opt__6296.invoke(main.clj:265)
    at clojure.main$main__6314.doInvoke(main.clj:346)
    at clojure.lang.RestFn.invoke(RestFn.java:409)
    at clojure.lang.Var.invoke(Var.java:365)
    at clojure.lang.AFn.applyToHelper(AFn.java:165)
    at clojure.lang.Var.applyTo(Var.java:482)
    at clojure.main.main(main.java:37)
Caused by: java.lang.IllegalArgumentException: No matching field found: setSequence for class com.sun.media.sound.RealTimeSequencer
    at clojure.lang.Reflector.getInstanceField(Reflector.java:245)
    at clojure.lang.Reflector.invokeNoArgInstanceMember(Reflector.java:267)
    at user$eval__11.invoke(recordmidi.clj:4)
    at clojure.lang.Compiler.eval(Compiler.java:5325)
    ... 10 more

Tried in Clojure 1.1.0 and 1.2.0. Works fine in Scala. I also tried looking at the methods of the Sequencer class using Clojure, and yes, the setSequence(Sequence) method is there.

This is the only problem with Java-calls I've had.

+2  A: 

Try

(.setSequence mySequencer mySequence)

This returns nil in the REPL, which seems about right given the method's void return value (and translates to mySequencer.setSequence(mySequence) which sounds like what you want).

I also once blogged about using Java MIDI libs from Clojure, maybe it helps a bit:

http://citizen428.net/archives/396

Michael Kohl
Thanks, that was it. I simply used Clojure's way of invoking Java methods the wrong way :-) And thanks for the link.
Grav