Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Aphexus opened this issue Sep 8, 2024 · 5 comments
Open

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

Aphexus opened this issue Sep 8, 2024 · 5 comments

Comments

@Aphexus
Copy link

Aphexus commented Sep 8, 2024

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

@jwhpryor
Copy link
Collaborator

jwhpryor commented Sep 9, 2024

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
Copy link

llvee commented Sep 17, 2024

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
Copy link
Collaborator

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
Copy link
Author

Aphexus commented Sep 18, 2024

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
Copy link

llvee commented Sep 18, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants