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

Construct Gobjects from java classes #164

Merged
merged 5 commits into from
Dec 14, 2024
Merged

Conversation

jwharm
Copy link
Owner

@jwharm jwharm commented Dec 14, 2024

With this PR, Java-GI will internally keep a map of Java Class to GObject GType values. This results in two improvements:

  1. When registering a new type (a Java class that is derived from GObject), it is not necessary anymore to keep the GType in a local field and expose it with a public static Type getType() method anymore.
  2. When creating a new instance, it is now possible to call new GObject(Foo.class). No need to pass the GType (though the overload is still available).

As a result, there is less boilerplate code to write and maintain, when a Java class is registered in the GObject type system and new instances are constructed.

Before:

public class Foo extends GObject {
    private static Type gtype = Types.register(Foo.class);

    public static Type getType() {
        return type;
    }

    public Foo() {
        super(GObject.newInstance(Foo.getType());
    }

    public Foo(MemorySegment address) {
        super(address);
    }
}

After:

public class Foo extends GObject {
    static {
        Types.register(Foo.class);
    }

    public Foo() {
        super(GObject.newInstance(Foo.class);
    }

    public Foo(MemorySegment address) {
        super(address);
    }
}

@jwharm jwharm merged commit ff4be7f into main Dec 14, 2024
@jwharm jwharm deleted the gobject-new-from-java-class branch December 14, 2024 14:50
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

Successfully merging this pull request may close these issues.

1 participant