On Fri, 9 Dec 2022 18:35:28 GMT, John Hendrikx <jhendr...@openjdk.org> wrote:

>> modules/javafx.base/src/main/java/javafx/beans/property/ReadOnlyListProperty.java
>>  line 119:
>> 
>>> 117: 
>>> 118:         @SuppressWarnings("unchecked")
>>> 119:         final List<E> list = (List<E>)obj;  // safe cast as elements 
>>> are only referenced
>> 
>> I'm not sure why it's safe to cast to a `List<E>`. You're getting a `List`, 
>> but it could be a list of something else. In the `Map` and `Set`  variants 
>> there is a try-catch for casting exceptions.
>
> It's safe as you're never actually using the type `E`, but I suppose the 2nd 
> list can also be `List<?>` as only `equals` is called on it.  The 2nd 
> `ListIterator` would then also be `ListIterator<?>`.
> 
> Casting to a specific generic type (like `List<E>`) never verifies if all the 
> elements are of that type, you're just telling the compiler that they are.
> 
> Only when you do something like:
> 
>     E e = list.get(0);
> 
> ... you may get a CCE here (but the compiler allowed it because you casted it 
> to `List<E>` earlier).  That's why I say it is safe because the follow up 
> code does not do any such action (it in fact does `Object o2 = e2.next()` -- 
> it would be bad if it was `E o2 = e2.next`).
> 
> Looking at it again, I think making the 2nd `List` a `List<?>` is better, as 
> we don't know yet at that stage that they all are of type `E` (even though it 
> doesn't matter for the code following it).

It also removes the need for the `SuppressWarnings` here.

-------------

PR: https://git.openjdk.org/jfx/pull/969

Reply via email to