From: Wojtek on
Lew wrote :
> Stefan Ram wrote:
>> � If everything else fails, I use
>>
>> @java.lang.SuppressWarnings( "unchecked" )
>
> Note that if one's analysis is incorrect, i.e., that the types don't
> actually match, then the suppression of warnings defeats the purpose
> of generics, to eliminate ClassCastException.
>
> Suppressing the "unchecked" warning is reserved for times when the
> programmer has guaranteed that there cannot be a ClassCastException,
> but cannot satisfy the compiler that this is so, e.g., when using an
> expression like casting '(T)'
> to a parametrized type.
>
> If the programmer has not made that guarantee, then there's no point
> to suppressing the warning.

Moreover the suppress warnings acts on the entire method, so any and
all questionable casts will be ignored.

The only place I use SuppressWarnings( "unchecked" ) is in a method
which only does that one cast to return the object.

--
Wojtek :-)


From: Lew on
Stefan Ram wrote:
>>>   If everything else fails, I use
>
>>> @java.lang.SuppressWarnings( "unchecked" )
>

Lew wrote:
>> Note that if one's analysis is incorrect, i.e., that the types don't
>> actually match, then the suppression of warnings defeats the purpose
>> of generics, to eliminate ClassCastException.
>
>> Suppressing the "unchecked" warning is reserved for times when the
>> programmer has guaranteed that there cannot be a ClassCastException,
>> but cannot satisfy the compiler that this is so, e.g., when using an
>> expression like casting '(T)'
>> to a parametrized type.
>
>> If the programmer has not made that guarantee, then there's no point
>> to suppressing the warning.
>

> Moreover the suppress warnings acts on the entire method, so any and
> all questionable casts will be ignored.
>

Only if you apply it to the entire method. You should probably not do
that, but apply it to a narrower scope.

> The only place I use SuppressWarnings( "unchecked" ) is in a method
> which only does that one cast to return the object.
>

Apply the annotation to the narrowest scope feasible. I assign a
variable to absorb the uncheckedness.

public <T> T makeT( String specifiesT )
{
@SuppressWarnings("unchecked") // the String arg will prevent
ClassCastException
final T foo = (T) Class.forName( specifiesT );
return foo;
}

This snippet does not properly show how the code guarantees the
correct type, so actually it's wrong. If I were doing this correctly,
there'd be code to ensure that the '@SuppressWarnings()' actually is
safe, and the comment would explain that, consistent with the
recommendations in /Effective Java/ by Joshua Bloch.

<http://java.sun.com/docs/books/effective/generics.pdf>

--
Lew
From: Lew on
Steven Simpson wrote:
> Note that you've already statically referred to action.SomeClass, so
> it's already loaded,

but not initialized, at least not by the reference to the class.

--
Lew