Rework constructor generation #339

Merged
jwharm merged 1 commit from rework-constructors into main 2026-03-18 22:07:17 +01:00
Owner

Most classes generated by Java-GI extend the ProxyInstance base class which has a public ProxyInstance(MemorySegment address) constructor for the native memory address. This means all classes generated by Java-GI, in their constructor, must call the base constructor with the native address.

In older Java releases (before JDK 25), the super() keyword was required to be the first statement in a constructor. So we couldn't do this:

public class Foo extends ProxyInstance {
    public Foo() {
        var address = g_foo_new(); // construct a native instance
        super(address);
    }
}

Because the super() keyword must be the first statement.
So Java-GI generated a static helper method:

public class Foo extends ProxyInstance {
    public Foo() {
        super(constructNew());
    }

    private static MemorySegment constructNew() {
        var address = g_foo_new(); // marshal arguments, and construct a native instance
        return address;
    }
}

With JEP 513 "Flexible Constructor Bodies", we can construct the native instance before calling super(), and the private static constructNew() helper methods will not be generated anymore.

For consistency and code reuse, the constructors are now generated in the MethodGenerator class. The ConstructorGenerator class has been removed.

Most classes generated by Java-GI extend the `ProxyInstance` base class which has a `public ProxyInstance(MemorySegment address)` constructor for the native memory address. This means all classes generated by Java-GI, in their constructor, must call the base constructor with the native address. In older Java releases (before JDK 25), the `super()` keyword was required to be the first statement in a constructor. So we couldn't do this: ```java public class Foo extends ProxyInstance { public Foo() { var address = g_foo_new(); // construct a native instance super(address); } } ``` Because the `super()` keyword must be the first statement. So Java-GI generated a static helper method: ```java public class Foo extends ProxyInstance { public Foo() { super(constructNew()); } private static MemorySegment constructNew() { var address = g_foo_new(); // marshal arguments, and construct a native instance return address; } } ``` With JEP 513 "Flexible Constructor Bodies", we can construct the native instance before calling `super()`, and the private static `constructNew()` helper methods will not be generated anymore. For consistency and code reuse, the constructors are now generated in the MethodGenerator class. The ConstructorGenerator class has been removed.
With JEP 513, we can construct a native instance before calling super(). The private static `constructNew()` helper methods are not necessary anymore.

For consistency, the constructors are now generated in the MethodGenerator class. The ConstructorGenerator class has been removed.
jwharm merged commit 7d5bfda4e6 into main 2026-03-18 22:07:17 +01:00
Sign in to join this conversation.
No description provided.