Skip to content

Add cast() method#305

Merged
jwharm merged 1 commit intomainfrom
cast-method
Nov 27, 2025
Merged

Add cast() method#305
jwharm merged 1 commit intomainfrom
cast-method

Conversation

@jwharm
Copy link
Owner

@jwharm jwharm commented Nov 27, 2025

This allows to cast a type instance to another type, even if the Java type would not allow it.

This is of course terribly unsafe, but there is a runtime check using g_type_is_a() so at least there's a runtime type check.

The cast is intended to be used in some special cases:

  • When a Java wrapper class for a non-public GType doesn't fit the intended use case. In this case, a normal cast in Java to the intended type would raise a ClassCastException.
  • When a GObject is subclassed in Java, and the Java class overrides a virtual function, but it is still necessary to call the invoker method from Java. This isn't possible because Java-GI "merges" instance methods and virtual methods into one Java method, and when overridden, the virtual method will always take precedence over the parent's instance method. Using the cast() method introduced here, to "cast" the class to the parent type, it becomes possible again to call the instance method.

This allows to cast a type instance to another type, even if the
Java type would not allow it
@jwharm jwharm merged commit bc8e427 into main Nov 27, 2025
@jwharm jwharm deleted the cast-method branch November 27, 2025 22:29
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