Unable to call a gtk instance method of a derived class if there is a virtual function with the same name #303
Labels
No labels
bug
dependencies
documentation
duplicate
enhancement
github_actions
good first issue
help wanted
invalid
java
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
java-gi/java-gi#303
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
I have a class
MediaSubsonicwhich is derived fromMediaStream.MediaStreamcontains both a virtual method calledseekand an instance method calledseek.The instance method is the only way to set the seeking property on a
MediaStreaminstance.However, I also need to override the virtual method because this is how the actual work gets performed.
If i call the
seekmethod on aMediaStreamreference which points to an instance of my derived class,Java directly calls the overridden virtual function. This way, the
seekingproperty is never set.I also can't call the instance method using
superin the virtual method, because this leads to a loop.Is it possible in any way to call the instance method instead of the virtual method on an instance of my derived class?
You can call the virtual method of the parent class with
asParent().seek().Yes, but I want to call the instance method of the parent. The one that is registered on the instance struct and accessible in the derived class as super, but outside of the derived class.
So I did some more experimentation.
If I instantiate
MediaStream$Implwith a handle it works how I intend it to work.However, I am not sure, whether this is the proper way to do this.
Huh, you’re right. The instance method sets the
seekingproperty after the virtual method completes. That’s odd.I think that is by design. The virtual method is for the implementation and the instance method for the caller. That just doesn't map well to Java
This should call the instance method:
It's a workaround though. I need some more time to properly fix this.
(Edit: Apparently I missed your earlier comment that this workaround indeed works)
No problem. Thanks for the suggestion 🙏
I've created a new
cast()method that will help deal with this issue and with #299.Instead of
new MediaStream.MediaStream$Impl(s.handle()).seek()it allows to do:Or, in case of #299:
It's still a workaround, but much cleaner then manually passing a memory address into an Impl class, and the
cast()method will perform a runtime check to ensure the instance actually has the requested GType.The solution is inspired by a similar function in Gtk-rs.
Thanks. That looks much nicer than instanciating the Impl class.
That's exactly what I was looking for.