clojurejavaparser

How to make a Java object of generic type, in Clojure?


Clojure empty changes types of Java collections. For example a JavaParser MethodCallExpr object mce,

(type (.getArguments mce))
;; => com.github.javaparser.ast.NodeList
(type (emtpy (.getArguments mce)))
;; => nil
(type (into (empty (.getArguments mce)) (.getArguments mce)))
;; => clojure.lang.PersistentList

But (.setArguments mce ???) needs ??? to be a NodeList<Expression>, so how to make an object ??? in clojure?

Thanks!


Solution

  • empty works only for classes that implement clojure.lang.IPersistentCollection. It won't work for anything else.

    In your case, if you know that the type is NodeList, just construct it yourself via Java interop. If you don't know that it's NodeList in some general case, you're out of luck because you can't construct an instance of an arbitrary class based on a single assumption that it's a collection class.