Eliminate "[unchecked] unchecked call ..." compiler warning Tag(s): Language
import java.util.*;
public class ArrayListGeneric {
public static void main(String[] args) {
ArrayList data = new ArrayList();
data.add("hello");
data.add("world");
Iterator<String> it = data.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
}
C:\ArrayListGeneric.java:21:
warning: [unchecked] unchecked call to add(E) as a member of the raw type
java.util.ArrayList
data.add("hello");
^
C:\ArrayListGeneric.java:22:
warning: [unchecked] unchecked call to add(E) as a member of the raw type
java.util.ArrayList
data.add("world");
^
C:\ArrayListGeneric.java:25: warning: [unchecked] unchecked conversion
found : java.util.Iterator
required: java.util.Iterator
Iterator it = data.iterator();
^
3 WARNINGS Simply add the expected type (between < and >) after the class.
import java.util.*;
public class ArrayListGeneric {
public static void main(String[] args) {
ArrayList<String> data = new ArrayList<String>();
data.add("hello");
data.add("world");
Iterator<String> it = data.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
}
In JDK 1.6, it will be possible to insert a special annotation to suppress this kind of warning, something like :
import java.util.*;
public class ArrayListGeneric {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ArrayList data = new ArrayList();
data.add("hello");
data.add("world");
Iterator it = data.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
}
A complete list of possible @SuppressWarnings parameters can be found at http://mindprod.com/jgloss/annotations.html.
ANT
When compiling with ANT, you see the following output :[javac] Note: Some input files use unchecked or unsafe operations. [javac] Note: Recompile with -Xlint:unchecked for details.
Something like
<target name="compile" description="Compile code">
<mkdir dir="${bin}"/>
<mkdir dir="${lib}"/>
<javac srcdir="${src}" destdir="${bin}"
includeAntRuntime="no"
classpathref="lib.path"
debug="${compile.debug}">
<compilerarg value="-Xlint:unchecked"/>
</javac>
</target>
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com