File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Getting rid of generics warnings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting rid of generics warnings" Watch "Getting rid of generics warnings" New topic
Author

Getting rid of generics warnings

Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
I read this and I think that it's absolutely great. Now I'm trying to implement my own version which works fine, but I can't seem to get rid of some generics warnings.

My class looks like (simplified, evil stands for ev al which the forum doesn't allow ):


This gives me the following warning:
Type safety: The method compareTo(Object) belongs to the raw type Comparable. References to generic type Comparable<T> should be parameterized

Parameterizing to the following:

Gives me the warning:
Type safety: The cast from Object to T is actually checking against the erased type Object

How can I get rid of these warnings without using @SuppressWarnings?

Thanks in advance.
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
Noone has got an idea?
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Compile with the -nowarn flag.

Example:
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
What you've done here is found one of the pitfalls of the now broken Comparable interface. This interface is broken because of 'generics by type erasure', which is the underlying flaw.

How you might ask? interface Comparable should have been interface Comparable<T>{T compareTo(T t);} (actually, the method *should* declare to throw NullPointerException, which is the optimal (but not ideal) workaround to the existance of 'null', which is a language design flaw). So why wasn't it done like that? Because it would break existing clients, so instead, we have a very broken interface, which is intrinsic to a very broken collections API (in many other ways).

What can you do about it? Rewrite collections (as I have done partially), or not use Java - it's that simple (I say this because perservering with the compile-time warning fits the category of "not use Java" afaic). At some point, you have to concede to the brokenness; acknowledging it is important in making informed decisions. Acknowledging it is often misconceived as cynicism or what have you, but that's just the internet

Here's something I wrote that may be of interest: http://qa.jtiger.org/GetQAndA.action?qids=70&showAnswers=true


Edit:
Gah! I just reread my post.
"should have been interface Comparable<T extends Comparable>{int compareTo(T t);}" is what I meant - sorry for any confusion.
[ July 08, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
Ok, so there's no choice but to suppress the warnings. I'll do that, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting rid of generics warnings