New Java bindings, feedback appreciated

For the past few months I have been working on a new set of Java bindings for GTK using gobject-introspection and the new Foreign Function & Memory API (Project “Panama”) that is currently in preview in OpenJDK 19. Panama allows interoperability with C code from Java code without an intermediate library.

Link: https://github.com/jwharm/java-gi

A few things I hope are interesting:

  • There is very few custom code in the API; I’ve tried to work with the gobject-introspection data only, so future GTK releases will not require a lot of rework.
  • The entire GObject/Glib/GTK library stack is mapped to Java classes, and LibAdwaita too.
  • Registering new GTypes in a Java subclass is possible.
  • I have tried to add things that improve the development experience. For example, all classes include a builder so you can construct objects with properties using the builder pattern (like gtk-rs also supports), varargs are supported, GErrors are mapped to exceptions, and I’ve developed a crude converter for the docstrings, so the Java bindings include Javadoc comments with valid markup, cross-references, exception and deprecation attributes etc.

If you have experience with Java, I would be very happy if you give the bindings a try and send me your feedback. You can download the JAR file from the Github page, or clone the repository and generate the bindings by yourself using the java-gi tool.

Looking forward to your feedback!

6 Likes

This looks quite nice, judging from the example :partying_face: Thanks for working on this!

I have some questions from going through the README

  1. Did you look at generating bindings for GStreamer? That’s usually an API that shows some problems in bindings infrastructure, especially with regards to multi-threading and memory/resource handling.
  2. In relation to that, does Java provide ways to teach the GC about how “heavy” an object is and ideally also a way to traverse the object reference graph? Especially with GStreamer we noticed that languages with a tracing GC have a lot of problems dealing with native objects as they looks just like a small pointer sized value for the GC and there’s not much pressure to free them, while in reality a whole object graph could sit on them and a lot of memory. So in the end applications use a lot of memory over time because of objects that are not cleaned up.
  3. Do you handle non-GObject fundamental classed types already? For example GTK expressions or GDK events?
  4. You mention that GValues work via an overloaded create() constructor. Does this allow extending for types defined outside of GLib? For example, GTK expressions or GStreamer fractions?

This is great! Feel free to open an issue if you want to add a page about java-gi on the language bindings page of the GTK website.

Thanks for your feedback!

  1. I only tested the generated with GLib and GTK, but browsing through the GStreamer gir files, I expect the generator to work without too many issues. I’ll definitely try this out!

  2. Garbage collection in Java is non-deterministic (by design). I use a Cleaner to be notified when an object becomes unreachable, and call g_object_unref. As far as I know, both the GC and Cleaner don’t allow special treatment of ‘heavy’ resources.

  3. Apparently I don’t handle non-GObject fundamental classed types correctly at all. In the generated bindings, GtkExpression and GObject both extend from GObject. I’m going to fix that.

  4. Java doesn’t support mixins, so you cannot add new create() constructors as part of the Value class itself. However, the create() methods don’t do anything special. You can always create an uninitialized GValue struct using var v = Value.allocate(); and set the type with v.init(MY_GTYPE);.

I’d love to have this on the GTK website, but not in its current state! I want do a lot more testing first.

1 Like

Are you reachable via matrix chat? There is the room #introspection:gnome.org for people interested in gobject introspection.

I’m working on C# bindings and would be interested to talk to you as I assume the problems which arise are probably pretty similar.

1 Like

Not yet, but I’ll sign up when I have some time later this week.

Small update about GStreamer bindings: I installed the gstreamer1-devel Fedora package, which provided the following GIR files: Gst, GstBase, GstCheck, GstController and GstNet. I had to fix a few bugs, but am now able to run the “Hello World” application from the GStreamer tutorial from Java (link to the source).

Regrettably I know very little about GStreamer, so I’m going to read the documentation (always a good idea, I suppose), and find out which packages I need to install to expose more of the API.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.