| Author |
Type Casting Generics
|
Yohan Liyanage
Ranch Hand
Joined: Aug 17, 2007
Posts: 132
|
|
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 ]
|
Yohan Liyanage
http://blog.yohanliyanage.com
|
 |
camilo lopes
Ranch Hand
Joined: Aug 08, 2007
Posts: 202
|
|
change Object by String
|
Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
|
 |
Yohan Liyanage
Ranch Hand
Joined: Aug 17, 2007
Posts: 132
|
|
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.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
AFAIK, there is no way except using the @SupressWarnings annotation. It may suck, but that's the way it is.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
Rob is right.
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Yohan Liyanage
Ranch Hand
Joined: Aug 17, 2007
Posts: 132
|
|
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
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
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...
|
 |
Josh Allen
Ranch Hand
Joined: Jan 15, 2005
Posts: 37
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Although you are 100% correct, did you really think it was necessary to post in this year old thread?
|
 |
Josh Allen
Ranch Hand
Joined: Jan 15, 2005
Posts: 37
|
|
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.
|
 |
 |
|
|
subject: Type Casting Generics
|
|
|