If we can't eliminate the “unchecked cast” warning and we're sure that the code provoking the warning is typesafe, we can suppress the warning using the SuppressWarnings(“unchecked”) annotation. When we use the @SuppressWarning(“unchecked”) annotation, we should always put it on the smallest scope possible.
The warning message “unchecked conversion” implies that we should check the conversion before the assignment. To check the type conversion, we can go through the raw type collection and cast every element to our parameterized type.
Unchecked assignment: 'java.util.List' to 'java.util.List<java.lang.String>' It means that you try to assign not type safe object to a type safe variable. If you are make sure that such assignment is type safe, you can disable the warning using @SuppressWarnings annotation, as in the following examples.
This is a common problem when dealing with pre-Java 5 APIs. To automate the solution from erickson, you can create the following generic method:
public static <T> List<T> castList(Class<? extends T> clazz, Collection<?> c) {
List<T> r = new ArrayList<T>(c.size());
for(Object o: c)
r.add(clazz.cast(o));
return r;
}
This allows you to do:
List<SyndEntry> entries = castList(SyndEntry.class, sf.getEntries());
Because this solution checks that the elements indeed have the correct element type by means of a cast, it is safe, and does not require SuppressWarnings
.
Since getEntries
returns a raw List
, it could hold anything.
The warning-free approach is to create a new List<SyndEntry>
, then cast each element of the sf.getEntries()
result to SyndEntry
before adding it to your new list. Collections.checkedList
does not do this checking for you—although it would have been possible to implement it to do so.
By doing your own cast up front, you're "complying with the warranty terms" of Java generics: if a ClassCastException
is raised, it will be associated with a cast in the source code, not an invisible cast inserted by the compiler.
It looks like SyndFeed
is not using generics.
You could either have an unsafe cast and a warning suppression:
@SuppressWarnings("unchecked")
List<SyndEntry> entries = (List<SyndEntry>) sf.getEntries();
or call Collections.checkedList - although you'll still need to suppress the warning:
@SuppressWarnings("unchecked")
List<SyndEntry> entries = Collections.checkedList(sf.getEntries(), SyndEntry.class);
Did you write the SyndFeed
?
Does sf.getEntries
return List or List<SyndEntry>
? My guess is it returns List
and changing it to return List<SyndEntry>
will fix the problem.
If SyndFeed
is part of a library, I don't think you can remove the warning without adding the @SuppressWarning("unchecked")
annotation to your method.
If you are using Guava and all you want to do is iterate through your values:
for(SyndEntry entry: Iterables.filter(sf.getEntries(), SyndEntry.class){
...
}
If you need an actual List you can use
List<SyndEntry> list = Lists.newArrayList(
Iterables.filter(sf.getEntries(), SyndEntry.class));
or
List<SyndEntry> list = ImmutableList.copyOf(
Iterables.filter(sf.getEntries(), SyndEntry.class));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With