File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type Casting Generics

 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
camilo lopes
Ranch Hand
Posts: 202
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change Object by String

 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is right.
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Josh Allen
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic