This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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.
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!