Search code examples
clojureclojure-java-interop

Clojure: Is it possible to call package protected methods of java?


I am not able to find any documentation (or blogs) regarding this. Is it possible to call package protected methods from clojure? We have a huge java code base and I was thinking if its possible to start developing some clojure libraries around them.

I tried making the namespace same as that of the package in java but the method (static) was not found.


Solution

  • Here's a quick code example:

     (def a (ref 0))
     (def klass (class a))
     (def m (.getDeclaredMethod klass "currentVal" (into-array Class [])))
     (.setAccessible m true)
     (.invoke m a (into-array []))
    

    You should probably have a look at the reflect API

    Although, I would recommend only using the publicly declared fields and methods from your old java code base.