google / jni-bind

JNI Bind is a set of advanced syntactic sugar for writing efficient correct JNI Code in C++17 (and up).
Apache License 2.0
244 stars 29 forks source link

Automatically generate C class from Kotlin/Java class? #333

Open Aphexus opened 1 week ago

Aphexus commented 1 week ago

Surely this can be done relatively easily? It would remove the excess boilerplate code which would also reduce bugs.

jwhpryor commented 1 week ago

Yeah, I'd actually love that, and I've thought about it quite a bit, but there's a couple blockers.

First, I think Inheritance needs to land, because without it, every subclass will require redeclaring parent class implementations, and that would look very weird. Also, it enables enum classes, which probably are also required.

Even with this, there would still be missing functionality like weak pointers. I'm also not sure how to represent potentially class loaded types, because hypothetically a single function could return an object built from any class loader I think. It might just be fine to ignore this for now.

Second, there needs to Starlark rules written to generate targets. I'm sure you could do other things like a command line tool that would be useful too, but Starlark would really be the best way to work with it, since then you would see changes updated as soon as you modified your Java code.

All this is to say, I've been thinking about this since pretty early on in the project, but, I'm afraid it's probably still work (as anything is), and usually nothing is ever easy. I am but one programmer sadly 😓

llvee commented 2 days ago

This is possible, as indicated by some existing repos. Certainly not an easy feat as there is no live working solution on Github. I am interested in contributing, potential work & partnership opportunities at, with Google.

jwhpryor commented 1 day ago

I'm afraid I can only speak to this library, but I would be happy for help (I am just one engineer however, I do not speak for Google in any way nor can I meaningfully endorse anything on it's behalf beyond my own code).

That said, it would be an excellent start to try writing some [Starlark rules|Bazel targets] and see if you can generate a simple text file with every function in it. That would at least give a good sense of what's required from the build system.

From there we can understand how to support arguments etc. but I imagine the initial step is the hardest.

Aphexus commented 5 hours ago

I'm pretty new to Java/Android(which is what I'm using jni bind with)/Kotlin so I don't really have my bearings with me.

In a language like D which I used to use one would be able to do such things quite easily by using compile time reflection. Since Java has reflection it shouldn't be that hard to iterate over the properties and fields and output them in the appropriate format for jni-bind. There is a one to one correspondence so it's just essentially rewriting. At least for most cases I imagine. Anything is better than nothing though and if one can generate the jni classes for most cases then it should be enough. I don't know anything about Starlark/Bazel though so. But if I were gonna do this on my end I would either use some preprocessor that could reflect over the class or just do it in the code and have to run the project twice and have it spit something out that would get most of the work done.

llvee commented 5 hours ago

@jwhpryor Thank you for sharing. I will consider contributing more. I am presently working on finding support, sponsorship for engineering contributions. If you know of communities that may be interested in offering support and/or sponsorship to contributors please let me know.