How do I remove the reflection warning from this Clojure code that is attempting to call a Java clone method:
(defn clone-foo [m]
(.clone m))
=> Reflection warning, NO_SOURCE_PATH:2 - reference to field clone can't be resolved.
Surely if m is any instance of java.lang.Object it must have the clone method?
Is it something to do with clone being protected?
Yes, you can’t call the
clone()on an arbitraryObject. And, since theclone()method is not exposed byCloneable, cloning things in a “generic” fashion is somewhat difficult. This is one of those leftover bits from the early days of java which is too late to change at this point. If you control all the objects you need to clone, you could create your ownReallyCloneableinterface which exposes theclone()method, or you could play your own tricks with reflection.