• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi-
I have a method like

<T extends Customer> List<T> getYYY(){
------
//Legacy code which returns a List

return ex.getResultList() // Unchecked warning

}

Please let me know how I could eliminate "Unchecked warning". Thanks,
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
maha shivaji
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff-
Is this the only way?. Can't use reflection or someother technique to cast the result. I also dont want to type cast each and every item in the List. Thanks,
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by maha shivaji:
Jeff-
Is this the only way?. Can't use reflection or someother technique to cast the result. I also dont want to type cast each and every item in the List. Thanks,



Sounds like you need a for loop here. You can create a new List<T> and copy elements from the old List to the new one. Although, I suspect this will cause a lot more of these "unsafe operation" warnings.

Do you have access to this legacy library? And how much will it take to make the necessary changes to use generics properly? This might be something that is worth looking into.

Layne
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I posted is the recommended way to do it. What's wrong with it?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Albrechtsen:
What I posted is the recommended way to do it. What's wrong with it?


Treating the cause of the disease is better than treating only the symptoms...

In other words, you should find out why you get the unchecked warning and treat the cause of the problem - not just suppress the warning. Here is a Generics FAQ that explains different situations in which you can get unchecked warnings.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. But until you've dealt with legacy code (if you are even allowed to change it) it's nice to suppress *specific* warnings so that you aren't overwelmed when compiling.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic