• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is up with this warning about "unchecked" (only comes up in eclipse, not netbeans)  RSS feed

 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was using netbeans, but decided to change to eclipse.

This project (that I didn't write myself, but am going to try to tweak) didnt have any warnings in netbeans.

But is gets the warning below in eclipse. I'm sure I could just do a quick fix, or ignore the warning. But I would like to understand what the issue is, and "correct" way to handle it. (i.e. other than just suppressing warnings)

The javadoc seems to say that the method returns a list of element objects. Which is exactly what is catching the output. Im not understanding why conversion would be needed.


org.jdom.Element
ScreenClip.png
[Thumbnail for ScreenClip.png]
warning message
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those attached screenshots are diffcult to read.

What are you returning and what are yo assigning it to? what are their types?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDOM does not use any generics. getChildren does not return a List<Element>, it returns a List - the raw type. You can't assign that to a List<Element> reference without a warning, because the compiler cannot guarantee that the List is in fact a List<Element>.

With these legacy methods (which it is because it's not updated to use generics) I usually use a List<?>, then cast each separate element to Element:
 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Those attached screenshots are diffcult to read.

What are you returning and what are yo assigning it to? what are their types?



Yea -- not sure why the pic is being displayed as low res thumbnail. If you download it you can see crystal clear what it says.

Anyway the method is from org.jdom.Element. Calling getChildren method. Which the Javadoc from my previous post says returns a list of element objects -- it is being assigned to List<Element>



 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:JDOM does not use any generics. getChildren does not return a List<Element>, it returns a List - the raw type. You can't assign that to a List<Element> reference without a warning, because the compiler cannot guarantee that the List is in fact a List<Element>.

With these legacy methods (which it is because it's not updated to use generics) I usually use a List<?>, then cast each separate element to Element:


Thanks -- so I am just confused by the java doc then... it says

# java.util.List getChildren(java.lang.String name)
# This returns a List of all the child elements nested directly (one level deep) within this element with the given local name and belonging to no namespace, returned as Element objects.

Why does it say "returned as element objects" if it just returns a list?
 
Ben David
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically I could do something like this instead...



does that look good? would there be a better way to do it?

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the API indicates that each List element is an Element you can cast to List<Element>. That produces a warning but in this case you can suppress it safely using @SuppressWarnings("unchecked").
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!