Warning when casting to generic type

I can do the following without problem:

ArrayList list = (ArrayList)object;

But doing the following gives an "unchecked cast" warning:

ArrayList

= (ArrayList

)object;

Why is it not caring about casting to non generic types, but gives a warning when casting to a generic type? Generics aren't forbidden magic as far as I'm concerned... :)

Also, what does it want me to do to solve the warning? I don't want to put a "if(object instanceof ArrayList)" around this code in any case. it also doesn't require that when casting to a non-generic type... If I press "alt enter" on the warning, the only options are "convert to atomic", and "try to generify the class", both of which are totally pointless and would break my code.

---
Original message URL: http://devnet.jetbrains.net/message/5272329#5272329
1
1 comment

Hello Aardwolf,

This warning is also shown by the Java compiler, so we're simply following
the language specification when displaying it. See http://bit.ly/dC8Oda for
more information.

I can do the following without problem:

ArrayList list = (ArrayList)object;

But doing the following gives an "unchecked cast" warning:

ArrayList<Object> = (ArrayList<Object>)object;

Why is it not caring about casting to non generic types, but gives a
warning when casting to a generic type? Generics aren't forbidden
magic as far as I'm concerned...

Also, what does it want me to do to solve the warning? I don't want to
put a "if(object instanceof ArrayList<Object>)" around this code in
any case. it also doesn't require that when casting to a non-generic
type... If I press "alt enter" on the warning, the only options are
"convert to atomic", and "try to generify the class", both of which
are totally pointless and would break my code.

---
Original message URL:
http://devnet.jetbrains.net/message/5272329#5272329


--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.