A generic type is a generic class or interface that is parameterized over types. For example, java.util.List
has one type parameter:
the type of its elements.
Using generic types raw (without binding arguments to the type parameters) prevents compile-time type checking for expressions that use these type
parameters. Explicit type casts are necessary for them, which do perform a runtime type check that may fail with a
ClassCastException
.
What is the potential impact?
The compiler cannot assert that the program is inherently type safe. When a cast fails, a ClassCastException
is thrown during runtime
and the program most likely crashes. Therefore, this issue might impact the availability and reliability of your application.
Exceptions
The rule does not raise an issue for the simple instanceof
operator, which checks against runtime types where type parameter
information has been erased. Since it does not return a rawly typed instance but a boolean value, it does not prevent compile-time type checking.
This, however, is not the case for the cast
operator as well as the extended instanceof
operator which are both not an
exception from this rule. Since they operate on the erased runtime type as well, they must use wildcard type arguments when checked against a
parameterized type (see the examples).