graalvm

emilaasa 2022-11-02T14:19:20.290959Z

Is there a way to eliminate reflection on zero argument java constructor calls? I'm currently passing around an anonymous fn that looks like this: :listener-ctor #(XListener.) and then it's invoked and used by the caller, but graal can't find it. I have a multitude of different Listeners and they don't share any meaningful base class unfortunately.

lispyclouds 2022-11-02T14:25:07.289009Z

Maybe helping GraalVM along with a reflection.json helps?

{
    "name": "foo.bar.XListener",
    "queryAllDeclaredConstructors": true,
    "queryAllPublicConstructors": true
  },

borkdude 2022-11-02T14:26:24.625609Z

What does the Clojure compiler say when you set:

(set! *warn-on-reflection* true)

borkdude 2022-11-02T14:26:50.605979Z

If Clojure doesn't complain, it's maybe a different issue

emilaasa 2022-11-02T14:29:05.697449Z

It does complain 🙂

borkdude 2022-11-02T14:29:28.640599Z

Then you can likely fix it within Clojure itself without adding reflection config?

emilaasa 2022-11-02T14:29:36.219999Z

Let me double check what it thinks the problem is...

emilaasa 2022-11-02T14:30:34.613799Z

Yes @rahul080327 it does work to do that, and that might be my fallback approach.

lispyclouds 2022-11-02T14:31:34.497219Z

yeah eliminating it via a typehint should be nicer

emilaasa 2022-11-02T14:32:38.233779Z

Yeah so the problem I guess is that in the call site we use a .instanceMethod type of call, and that function that looks roughly like this:

(defn ->top-secret
  [listener]
  (->> listener
       .getFunctions
       parsed-fns->internal-format
       (into [])))

emilaasa 2022-11-02T14:33:01.683649Z

*warn-on-reflection* doesn't like that one, but I have 25 different listeners without a common base class to hint against

borkdude 2022-11-02T14:33:04.002269Z

you can add a type-hint to the listener argument

emilaasa 2022-11-02T14:34:13.001279Z

I might have to pass it the ->top-secret function or a wrapped getFunctions instead I think? because the caller does not have access to my 25 different types of Listener and there's no commonality between them

emilaasa 2022-11-02T14:34:29.356689Z

But I could construct a type-hinted getFunctions wrapper per Listener

emilaasa 2022-11-02T14:34:40.920159Z

maybe 😮

borkdude 2022-11-02T14:34:47.437889Z

or maybe there can be an interface introduced?

➕ 1
emilaasa 2022-11-02T14:35:19.640849Z

It's generated code so I'm a bit tied up on the java side - but could I do something in clojure?

borkdude 2022-11-02T14:37:03.399529Z

you can't retrofit an interface to JVM types in Clojure, but you can introduce protocols. You'll have to extend it to your 25 concrete classes though, which doesn't buy you much

emilaasa 2022-11-02T14:37:43.319229Z

All right, thanks so much for the help I'll try to pass something with type hints 🙂

emilaasa 2022-11-02T14:37:58.585809Z

Otherwise I guess I dig into the classes and generate the reflect config

emilaasa 2022-11-02T15:34:46.079969Z

Maybe you fellows know a good programmatic way to access a namespaces' imports? 🙂

borkdude 2022-11-02T15:35:57.616169Z

(ns-imports *ns*)

emilaasa 2022-11-02T15:36:09.013869Z

❤️