Java interoperability and Clojure

39 views
Skip to first unread message

Geoff Wozniak

unread,
Apr 2, 2009, 12:22:11 PM4/2/09
to Clojure
What are the limitations of Clojure and Java interoperability? Are
they clearly stated somewhere?

I have been experimenting with using Clojure to test some existing
Java code (being able to do so makes a convincing argument to use it
where I work) and I've noticed that there doesn't seem to be any way
to call or access package-protected methods or fields -- either that
or I'm doing something wrong. :)

I've also been trying use the java.lang.reflect capabilities (such as
setAccessible() in java.lang.reflect.AccessbileObject) and noticed
that they do not have an effect. How much can be done using those
libraries within Clojure to affect Java code?

Any information provided would be most helpful. Being able to use
Clojure to write tests for existing Java code (that extensively
employs package-protected methods and fields) would be a nice way to
demonstrate Clojure's capabilities to some people I work with.

Rayne

unread,
Apr 2, 2009, 10:28:42 PM4/2/09
to Clojure
As far as I know, there is no limit.

Stuart Halloway

unread,
Apr 2, 2009, 11:26:58 PM4/2/09
to clo...@googlegroups.com
Hi Geoff,

You should have no trouble using setAccessible. There are several
demos of this in the source code for the book [1] that use
setAccessible to check private fields in a unit test. (See lancet/test/
step-2-complete.clj [2], for instance).

Hope this helps,
Stu

[1] http://github.com/stuarthalloway/programming-clojure/tree/master
[2] http://github.com/stuarthalloway/programming-clojure/blob/401f348b53ddf8ba9b90a445981a134c5eb20783/lancet/test/step_2_complete.clj

Geoff Wozniak

unread,
Apr 3, 2009, 8:38:11 AM4/3/09
to Clojure
Thanks for the links, Stuart, although I managed to figure out what I
was doing wrong: I didn't realize that the Java vararg type Object...
mapped to a sequence in Clojure, so I wasn't properly calling 'invoke'
in java.lang.reflect.Method.

Again, thanks for the links. I'm sure they'll be helpful in the
future.

On Apr 2, 11:26 pm, Stuart Halloway <stuart.hallo...@gmail.com> wrote:
> Hi Geoff,
>
> You should have no trouble using setAccessible.  There are several  
> demos of this in the source code for the book [1] that use  
> setAccessible to check private fields in a unit test. (See lancet/test/
> step-2-complete.clj [2], for instance).
>
> Hope this helps,
> Stu
>
> [1]http://github.com/stuarthalloway/programming-clojure/tree/master
> [2]http://github.com/stuarthalloway/programming-clojure/blob/401f348b53d...

kyle smith

unread,
Apr 4, 2009, 6:00:49 PM4/4/09
to Clojure
Reply all
Reply to author
Forward
0 new messages