I have been getting rid of these warnings without using @SuppressWarnings
and in a way that I think makes the casts a bit more explicit.

I don't know what you are doing in case (1), but in case (2), you can do
this:

TreeSet<JobTemplate> mySet =  new TreeSet<JobTemplate>();
for(Object jobTemplate : request.getSession().getAttribute(
AcceSessionConstant.JOB_TEMPLATE)){
 mySet.add( (JobTemplate) jobTemplate);
}



On 8/26/06, Scott Van Wart <[EMAIL PROTECTED]> wrote:

Thibaut wrote:
> I migrate a Struts app from jdk 1.4 to jdk1.5
>
> I have warnings I would like to solve :
>
> 1) the clone() function
> I have a "Type Safety" exeption with this kind of code :
> current.setUserRights((TreeSet<Integer>) this.userRights.clone());
>
> 2) Cast make a warning :
> TreeSet<JobTemplate> mySet = (TreeSet<JobTemplate>)
> request.getSession ().getAttribute(AcceSessionConstant.JOB_TEMPLATE);
>

Depending on your IDE (I use Eclipse), you might be able to factor out
the casts into separate methods, and add the @SuppressWarnings(
"unchecked" ) to the new method.  So instead of,

  public void myFunction() {
    current.setUserRights( (TreeSet<Integer>) )this.userRights.clone();
  }

You'd have a helper function:

  @SupressWarnings( "unchecked" )
  public TreeSet<Integer> cloneUserRights( UserRights rights ) {
    return (TreeSet<Integer>)rights.clone();
  }

And use this method accordingly.  Keep in mind that you should probably
be absolutely certain about the real type of the object when you do
this.  The whole generic thing is simply syntactic sugar (the runtime
still does the casts), and the warnings are there as a reminder.

- Scott

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to