Re: checking casts




"Thomas Hawtin" <usenet@xxxxxxxxxxxxxxxxx> wrote in message
news:438cc365$0$1471$ed2619ec@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Dan Upton wrote:
>> When trying to compile something I was working on today, I got this error
>> based on a performing a cast:
>>
>> PuzzleSolver.java:311: warning: [unchecked] unchecked cast
>> found : java.lang.Object
>> required: java.util.Vector<Piece>
>> piecesClone = (Vector<Piece>)pieces.clone();
>>
>> I know it's a safe cast, but just for the sake of getting javac to leave
>> me alone I tried sticking it in a try...catch block:
>
> The compiler is complaining that that you are casting to Vector<Piece>,
> but the runtime will not be able to check that the object isn't actually,
> say Vector<String>.
>
> The easy way around it is to write:
>
> piecesClone = new ArrayList<Piece>(pieces);

Alternatively, you could add:

@SuppressWarnings("unchecked")

to suppress the warning within a block of code (might be a good idea to
add a comment explaining why you believe it's safe to ignore this warning).
Sun's "JavaC" compiler is supposed to support the SuppressWarnings
annotation in its next release, and Eclipse's compiler already does support
it.

- Oliver


.



Relevant Pages

  • Re: (FAQ details:) malloc(), void * and casts
    ... The rationale for this is that 1) the ... cast is not needed and 2) the cast may mask errors. ... correct C++ compiler. ... But it is an extraneous warning. ...
    (comp.lang.c)
  • Re: (FAQ details:) malloc(), void * and casts
    ... cast is not needed and 2) the cast may mask errors. ... it's quite a good rationale when taken with the very solid ... correct C++ compiler. ... It silences at least one warning on all of my 'modern' compilers. ...
    (comp.lang.c)
  • Re: Array of pointers in a struct
    ... >>cast it, there's a warning, if I don't, there isn't. ... > This is exactly the warning you need to avoid undefined behavior. ... > warning tells you the compiler thinks malloc is returning an integer. ...
    (comp.lang.c)
  • Re: problem with memcpy and pointers/arrays confusion - again
    ... this second method is known as an explicit conversion, or cast. ... The cast, in effect, tells the compiler: ... the malloc function. ... function taking a size_t as a parameter and returning a void pointer (i.e. ...
    (comp.lang.c)
  • Re: [Q] C -> C++ Converter
    ... >> would require a cast (which in turn would be bad practice if writing C). ... but you should check exactly why you're getting a warning. ... A compiler is of course at liberty to generate a diagnostic about anything it ...
    (comp.os.linux.misc)