• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Type Casting Generics

 
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Recently, I faced this issue when I was type casting a generic type. The code is as followed (this is a simplified version to show the issue) :



The type casting in the last line gives a compiler warning :



Whats the solution to eliminate this warning ? (Except @SupressWarnings of course)

Thanks.
[ December 29, 2007: Message edited by: Yohan Liyanage ]
 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change Object by String

 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I don't get you.

String o = lst;


I'm afraid that's not possible. lst is a List object. So it cannot be assigned to a String reference variable. It will result in a compiler error. Object is the super-type of all types in Java, so it can be assigned to a Object reference variable.

May be I have mis-understood your reply. Can you please explain ?

P.S. :
As I have stated, the code I have posted is not exactly where I faced this problem. It's a simplified version of it. I faced this issue when I was retrieving an attribute from a Request object in a Servlet. request.getAttribute() always returns the object as an Object.
 
Sheriff
Posts: 22855
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AFAIK, there is no way except using the @SupressWarnings annotation. It may suck, but that's the way it is.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob is right.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys.

Will Sun do anything regarding this in a future compiler? It looks really bad when I put @SuppressWarnings in the code.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, to remove the warning, Sun had to remove the root cause - namely that the generic type of the list is unknown at runtime.

It's not totally unthinkable, but I wouldn't hold my breath...
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yohan Liyanage wrote:Thanks guys.

Will Sun do anything regarding this in a future compiler? It looks really bad when I put @SuppressWarnings in the code.



No, because it's your code that's the problem and not the compiler. You are doing something dangerous and it's being helpful by giving you a warning. Generics provide type safety and you are circumventing that type safety here. This will NEVER change.

Be thankful you have @SuppressWarnings to let the code reader know that you know what you are doing and the warning can be safely ignored.
 
Rob Spoor
Sheriff
Posts: 22855
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although you are 100% correct, did you really think it was necessary to post in this year old thread?
 
Josh Allen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:Although you are 100% correct, did you really think it was necessary to post in this year old thread?



Point noted, but yes, because I found this on my google search, and others are sure to find it in the same manner. It'll probably still be served up years from now.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic