Fork me on GitHub
#graalvm
<
2022-11-02
>
emilaasa14:11:20

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.

lispyclouds14:11:07

Maybe helping GraalVM along with a reflection.json helps?

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

borkdude14:11:24

What does the Clojure compiler say when you set:

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

borkdude14:11:50

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

emilaasa14:11:05

It does complain 🙂

borkdude14:11:28

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

emilaasa14:11:36

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

emilaasa14:11:34

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

lispyclouds14:11:34

yeah eliminating it via a typehint should be nicer

emilaasa14:11:38

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 [])))

emilaasa14:11:01

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

borkdude14:11:04

you can add a type-hint to the listener argument

emilaasa14:11:13

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

emilaasa14:11:29

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

borkdude14:11:47

or maybe there can be an interface introduced?

1
emilaasa14:11:19

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

borkdude14:11:03

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

emilaasa14:11:43

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

emilaasa14:11:58

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

emilaasa15:11:46

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

borkdude15:11:57

(ns-imports *ns*)