• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to understand an unchecked conversion warning  RSS feed

 
Jon Swanson
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a jar that has a lot of classes defined. In the process of using some of these classes I get an unchecked conversion warning and I am unsure exactly what it is trying to tell me.

350: warning: [unchecked] unchecked conversion
found : java.util.List
required: java.util.List<org.jfree.data.xy.XYDataItem>
item = data.getItems();

I see where it is coming from, but I'm not sure how to make a proper conversion, since I need a List<XYDataItem>, here is the code that deals with item:



The documentation on XYSeries says:

java.util.List getItems()
Returns the list of data items for the series (the list contains XYDataItem objects and is unmodifiable).

So I thought that I should declare a list of XYDataItems as java.util.List<XYDataItems>. If I do, I get that warning, if I declare item as java.util.List, I get errors related to my comparator. It needs to call methods on XYDataItem objects. I thought I could just change the item assignment to:

item = (java.util.List<XYDataItems>) data.getItems();

but then I get an unchecked caste warning. Could someone explain what Java concept I am missing here so I can put this bit together correctly?
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem, as you have noted, is that the library you're using uses a raw List. While it's legal to assign a raw type to a variable that holds parameterized types, it will give you a warning.

If you can't modify the library you're working with, then if you're absolutely sure that the List will always hold XYDataItem instances, then you can add the following annotation above the line that's giving you problems:

@SuppressWarnings("unchecked")

Also note that in your method, item is a very poor variable name, and should probably be called items instead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!