TreeListStore exception due to wrong item typing #349
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
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
java-gi/java-gi#349
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?
Originally logged on github, relogged here for convenience.
Hi 👋
Here's a minimal test case showcasing the error:
I'm assuming the issue derives from the fact that
TreeListModel<T>extendsListModel<T>, when it should instead derive fromListModel<TreeListRow>. Less handy in terms of generics, but I guess that's what the API allows for now (unless I miss something).Note: the GTK doc mentions that it extends ListModel, but not the type of the items
Did some more investigation: it turns out this code behaves perfectly when creating a TreeListModel with passthrough parameter
true.So the typing is only wrong in case you use
passthrough = false(like I did).Maybe the
TreeListModel.getItem(int)method could just use:super.getItem(int)ifpassthroughistruegetRow(int).getItem()ifpassthroughisfalseWhat do you think?
Thanks for re-logging this here 🙂
The docs of the
passthroughproperty says: "If FALSE, the GListModel functions for this object return custom GtkTreeListRow objects. If TRUE, the values of the child models are pass through unmodified."So I think it's supposed to return a TreeListRow, and it's not correct to declare the model as
TreeListModel<StringObject>.Ok, indeed this is a misusage, that makes sense, thanks.
So I guess there's nothing to fix, aside maybe from offering more guidance in the exception?
Hmm not sure how to do that… Suggestions?
Adding generation of a TreeListModel getItem method like:
What do you think? or UnsupportedOperationException maybe?
Because indeed, the usage of getItem doesn't make sense in the context of a non-passthrough model.
I'm keeping this open as an enhancement.
Currently it's really difficult in Java-GI to override functions with a custom implementation, and I have ideas to improve that, so it becomes easier to generate these kind of checks.
@mrlem I studied the documentation of TreeListModel a bit more, and I don't think the proposed check is correct. Creating a
TreeListModel<TreeListRow>withpasstrough=falseand then retrieving TreeListRows withgetItem()is a valid usecase. (If it wasn't a valid usecase, thepasstroughproperty would be useless and would not be part of the API.)I tried to build a check that ensures
passtrough=falsewhen the list model is not aTreeListModel<TreeListRow>, but haven't been succesful. Java's type erasure makes it impossible to determine insidegetItem()whether the returned type is correct. For example, this doesn't do anything:This doesn't do anything because
getItem()actually doesn't throw anything. The ClassCastException occurs at the call site, in your own code. Inside thegetItem()method, the generic type is erased and no ClassCastException occurs. (Even an explicit cast like(T) super.getItem()didn't throw.)The only thing I can think of, is to add Javadoc warnings to TreeListModel's constructor, to clarify the danger of setting
passtrough=false. Would that have prevented the issue for you, or do you expect that most people will not read the javadoc?I see your point. Maybe we could add a javadoc warning in the constructor as you mentionned, and add one in getItem() too? (as it's the likely place it will crash: a @throws clause?)