aspose file tools*
The moose likes Java in General and the fly likes dealing with compiler 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 "dealing with compiler warnings" Watch "dealing with compiler warnings" New topic
Author

dealing with compiler warnings

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Hi there,

I'm using JDK 1.5 and created an instance of an ArrayList, then after loading the ArrayList I passed it as an arguement to the Collections.sort(...) method. When I compile using:

javac -Xlint:unchecked -classpath "C:\Program Files\Apache Softwa
re Foundation\Tomcat 5.5\common\lib\servlet-api.jar;C:\Program Files\Apache Soft
ware Foundation\Tomcat 5.5\common\lib\jsp-api.jar;C:\Program Files\Apache Softwa
re Foundation\Tomcat 5.5\webapps\scholastic\WEB-INF\classes;C:\Program Files\Apa
che Software Foundation\Tomcat 5.5\webapps\scholastic\WEB-INF\classes\mvcs;C:\Pr
ogram Files\Apache Software Foundation\Tomcat 5.5\webapps\scholastic\WEB-INF\cla
sses\com\components;C:\Program Files\Apache Software Foundation\Tomcat 5.5\webap
ps\scholastic\WEB-INF\classes\com\ezjavabeans\ezsmtp;C:\Program Files\Apache Sof
tware Foundation\Tomcat 5.5\webapps\scholastic\WEB-INF\lib\jasperreports-0.6.7.j
ar" *.java

I got the following warning message:

TestProcessor.java:153: warning: [unchecked] unchecked method invocation: <T>sor
t(java.util.List<T> in java.util.Collections is applied to (java.util.ArrayList
<mvcs.TestProcessor.Answer>
Collections.sort(list);
^
1 warning

What is it I need to do further to prevent this warning message from popping up all the time? Am I supposed to ignore it or is there something I'm expected to do to prevent this warning message?

Please advise,

Alan
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

In a word: Generics. Rather than using just an ArrayList, you can and should use an ArrayList<Widget> -- an ArrayList of Widgets (where "widget" stands in for whatever kind of object you're actually using.)


[Jess in Action][AskingGoodQuestions]
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
I did use Generics with my code. I defined a class named Answer and loaded up several instances of Answer into the ArrayList:

ArrayList<Answer> list = new ArrayList<Answer>();

for(int i = 0; i < ...; i++)
list.add(new Answer(...));
...

Collections.sort(list);

So, you see, even though I defined a specific type for ArrayList, I still get the error message described earlier.

Alan
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Provide the code (or a derivation of) that you allege is producing the problem. Collections.sort is a parameterised method where the type T is inferred.


Tony Morris
Java Q&A (FAQ, Trivia)
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
Ok, Here's the majority of my code from a servlet I'm working on. First I define an inner class named Answer, then you'll see the beginning of the doPost() method right up to the point where I call Collections.sort(list);
It's that method call that is causing the warning message during compilation. Does this help any?

Alan


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Ah! OK, here's the deal: Foo implements the unsafe version of Comparable, and that's what's causing the warning -- i.e., it's an ArrayList<Comparable> rather than an ArrayList<Comparable<Foo>>. You need to make two small changes:

1) class Answer implements Comparable<Answer>

2) public int compareTo(Answer ans) ...

and the warning will go away. Sorry I didn't think of this before. The error message is really somewhat inaccurate, isn't it?
Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 237
I was able to duplicate the compiler warning message by creating a small test application. See below:

 
 
subject: dealing with compiler warnings