-
Notifications
You must be signed in to change notification settings - Fork 46
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
Casting of empty ListTag is error-prone #14
Comments
This would not change by passing the type class to the constructor. |
When the type is passed to the constructor it would already be initialized when you try to add an element of the incorrect type. Then the special case in NBT/src/main/java/net/querz/nbt/ListTag.java Line 335 in 947c750
And the modification of the tag type in the add method NBT/src/main/java/net/querz/nbt/ListTag.java Lines 88 to 89 in 947c750
Additionally it would probably be good to have a protected or private method |
I'll have too look into how deserialisation works with that, because the type of the list is read after it's been instantiated. |
An empty ListTag can now only be untyped if created using the protected static method
The ListTag constructor now needs a mandatory type class additionally to the ListTag generic type specification. The type class passed to the constructor must be of the exact same type as the generic type. ListTag<StringTag> s = new ListTag<>(StringTag.class);
ListTag<ListTag<?>> l = new ListTag<>(ListTag.class); |
You can change the type of an empty
ListTag
by adding an element of a different type. This appears to be "intended", seeListTagTest.testCasting()
, but in my opinion this is pretty error-prone.It makes it pretty easy to change the list type by accident and when you then use the list at a completely unrelated place you suddenly get an exception.
I would suggest adding a
Class<? extends Tag>
to the constructor or using some new class to store both class and typeID and then validate every added element against them.The text was updated successfully, but these errors were encountered: