• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Casting and Generics  RSS feed

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers!

Could anyone remind me something about this generics-related warning?
My jdk is j2se 1.5.0_05.

While compiling the following row(option -Xlint).
Note: getJspContext() returns an Object.

ArrayList<MyImage> articlesList = (ArrayList<MyImage> getJspContext().getAttribute("imageList", PageContext.REQUEST_SCOPE);

this is the output:

src/it/infomedia/barry/EsempioSimpleTag/taghandlers/PicsTagHandler.java:44: warning: [unchecked] unchecked cast
found :java.lang.Object
required:java.util.ArrayList<it.infomedia.barry.EsempioSimpleTag.model.MyImage>
ArrayList<MyImage> articlesList = (ArrayList<MyImage> getJspContext().getAttribute("imageList",PageContext.REQUEST_SCOPE);

Why?

Thank you very much.

Regards,
Barry.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getJspContext().getAttribute() returns an object which cannot directly assigned to ArrayList which is expecting only MyImage objects
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically it's saying "I'm accepting your cast, but I cannot guarantee that you are not going to get a ClassCastException at runtime if the type is not what you promise it is". It's not possible to fix all warnings like this if you have no access to the code for getJspContext().getAttribute(). But you can suppress the warning with an annotation - something like @SuppressWarnings("unchecked")
 
Barry Bassi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Vidya and Barry!


Regards,
Barry.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!