To begin with, I’m a very happy Clojurian, but not when I’m working with interops. I’m using Google apis extensively for some features in my product; and so far the experience has been quite awful. I’ve been contemplating it for a while and here are my pain points.
- It is hard to look up for what method a java object supports.
- It is hard to understand the inheritance and polymorphism designs without actually looking at the java codes.
- 1,2 are amplified all the more because I’m using calva with vscode. The IDE’s java support is not as good as that of intellij.
How do you work with interops in general? I welcome any tips/advices/know-hows.
When I started Clojure 3 years ago I had zero Java and Clojure knowledge but was experienced in C, Python, Shell etc. WhileI had no issues learning Clojure I did struggle with Java in Clojure, where Java by itself wasnt an issue.
I use VSCode with Calva aswel.
Clojure.reflect helps a lot and so does bean.
I made this snippet in a dev ns to require and use to get methods: (ns myapp.dev (:require [clojure.pprint :refer [print-table]] [clojure.reflect :refer [reflect]]))
(defn get-members [in] (print-table [:name :flags :parameter-types :return-type] (sort-by :name (:members (reflect in)))))
And I got this helper from a gist: (comment “call private methods from https://gist.github.com/egamble/7781127”)
(defn call-method [obj method-name & args] (let [m (first (filter (fn [x] (… x getName (equals method-name))) (… obj getClass getDeclaredMethods)))] (. m (setAccessible true)) (. m (invoke obj (into-array Object args)))))