views:

7921

answers:

6

In the Java snippet:

SyndFeedInput fr = new SyndFeedInput();
SyndFeed sf = fr.build(new XmlReader(myInputStream));
List<SyndEntry> entries = sf.getEntries();

the last line generates the warning

"The expression of type List needs unchecked conversion to conform to List<SyndEntry>"

What's an appropriate way to fix this?

+3  A: 

Did you wirte 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.

Alex B
You can also add an explicit cast.
Uri
A cast will just produce another warning, since the code is not type safe.
erickson
A: 

If you look at the javadoc for the class SyndFeed (I guess you are the class com.sun.syndication.feed.synd.SyndFeed), the method getEntries() doesn't return java.util.List<SyndEntry>, but returns just java.util.List.

So you need an explicit cast for this.

Shyam
yes, you are right about the class. i should have mentioned that. thanks for your diligence!
+5  A: 

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);
Jon Skeet
Thanks, that does the trick. Note: one must add an "s":@SuppressWarning --> @SuppressWarnings
@joeyjoejoe: Thanks. Bizarrely enough I checked that when writing the answer - but must have ignored the result of my checking!
Jon Skeet
+6  A: 
erickson
Thanks -- that's an interesting insight about the "warranty" and the invisible cast done by the compiler versus a cast done explicitly in my own code.
Yes, the value of un-reified generics is somewhat limited, but that is one thing it does provide. Just to clarify, this requires that your code compiles without type safety warnings.
erickson
Hi erickson, I agree that this indeed the best solution. Check my answer http://stackoverflow.com/questions/367626/how-do-i-fix-the-expression-of-type-list-needs-unchecked-conversion/2848268#2848268 for a generic version of this solution.
Bruno De Fraine
A: 

If you don't want to put @SuppressWarning("unchecked") on each sf.getEntries() call, you can always make a wrapper that will return List.

See this other question

Boune
+4  A: 

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.

Bruno De Fraine
Yes, that's it.
erickson