SageTV Community  

Go Back   SageTV Community > SageTV Development and Customizations > SageTV Studio
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

SageTV Studio Discussion related to the SageTV Studio application produced by SageTV. Questions, issues, problems, suggestions, etc. relating to the Studio software application should be posted here.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 10-16-2010, 05:29 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Cleanin up Java unchecked conversions

I'm in the process of cleaning up some Java code and can't figure out how to eliminate an "unchecked conversion" warning I'm getting. Here is the code:

Code:
        // Get the RSSItems in a LinkedList.
        LinkedList<RSSItem> ChanItems = new LinkedList<RSSItem>();
        ChanItems = rsschan.getItems();
The problem is that rsschan.getItems() returns a LinkedList and not specifically a LinkedList<RSSItem>. (I'm assuming that's because getItems() was not written using generics.)

I'd like to avoid using @SuppressWarnings because that might hide warnings I really need to see.

What is the best way to accomplish this?
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #2  
Old 10-16-2010, 05:55 AM
stuckless's Avatar
stuckless stuckless is offline
SageTVaholic
 
Join Date: Oct 2007
Location: London, Ontario, Canada
Posts: 9,713
Quote:
Originally Posted by tmiranda View Post
I'm in the process of cleaning up some Java code and can't figure out how to eliminate an "unchecked conversion" warning I'm getting. Here is the code:

Code:
        // Get the RSSItems in a LinkedList.
        LinkedList<RSSItem> ChanItems = new LinkedList<RSSItem>();
        ChanItems = rsschan.getItems();
The problem is that rsschan.getItems() returns a LinkedList and not specifically a LinkedList<RSSItem>. (I'm assuming that's because getItems() was not written using generics.)

I'd like to avoid using @SuppressWarnings because that might hide warnings I really need to see.

What is the best way to accomplish this?
I guess there are a couple of options...

Adding @SuppressWarnings("unchecked") just above the statement...

Code:
        // Get the RSSItems in a LinkedList.
        @SuppressWarnings("unchecked")
        LinkedList<RSSItem> ChanItems = new LinkedList<RSSItem>();
        ChanItems = rsschan.getItems();
You other option is to use a very generic list...
Code:
        // Get the RSSItems in a LinkedList.
        LinkedList<?> ChanItems = new LinkedList<RSSItem>();
        ChanItems = rsschan.getItems();
Reply With Quote
  #3  
Old 10-16-2010, 06:17 AM
broconne broconne is offline
Sage Aficionado
 
Join Date: Feb 2009
Location: Cary, NC
Posts: 306
Quote:
Originally Posted by tmiranda View Post
I'm in the process of cleaning up some Java code and can't figure out how to eliminate an "unchecked conversion" warning I'm getting. Here is the code:

Code:
        // Get the RSSItems in a LinkedList.
        LinkedList<RSSItem> ChanItems = new LinkedList<RSSItem>();
        ChanItems = rsschan.getItems();
The problem is that rsschan.getItems() returns a LinkedList and not specifically a LinkedList<RSSItem>. (I'm assuming that's because getItems() was not written using generics.)

I'd like to avoid using @SuppressWarnings because that might hide warnings I really need to see.

What is the best way to accomplish this?
Tmiranda,
You have a few options. Although, first looking at the code - the call to "new LinkedList<RSSItem>()" is unnecessary. In your second step you are setting the ChanItems to the value returned by rssChan.getItems(); So effectively that code could become
Code:
LinkedList<RSSItem> ChanItems = rsschan.getItems();
On a minor side note, Java conventions dictate variable names are lowerCamelCase so if you care, the standard convention would be:
Code:
LinkedList<RSSItem> chanItems = rssChan.getItems();
But if you are the only one looking at your code - then it really doesn't matter

For the unchecked warning question. You are getting the error for a good reason, as you speculated you are asking the compiler to guarantee that only Objects of type RSSItem will be in the list named chanItems, however the method you are calling to retrieve that list doesn't use generics and therefore does not guarentee (via generics - it may via its javadoc) that the LinkedList will only have objects of type RSSItem.

(1) If you know that rssChan.getItems(); will always return a LinkedList that contains only RSSItems and you want your program throw a ClassCastException in the case where it does not, there is nothing wrong with using @SuppressWarnings("unchecked") at the site of the call - that will not hide other errors as long as you scope it just to the variable declaration:
Code:
@SuppressWarnings("unchecked")
LinkedList<RSSItem> chanItems = rssChan.getItems();
(2) Cast every object yourself, checking the type with instance of - this guarantees only objects of type RSSItem can get in the list and you can log items in the returned list that weren't of the expected type.
Code:
List<RSSItem> chanItems = new LinkedList<RSSItem>();
for(Object object : rssChan.getItems()){
  if(object instanceof RSSItem){
     RSSItem rssItem = (RSSItem) object;
     chanItems.add(rssItem);
  }
  else{
     Log("OMG Ponies!  Unexpected object type in return list");
  }
}
I didn't compile the above code, so not 100% sure it is accurate, but it should be in spirit.
Reply With Quote
  #4  
Old 10-16-2010, 07:46 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Thanks to both. I didn't realize I could use @SuppressWarnings() at the individual statement level so I think I'll just use that.

I know the new LinkList<> is not necessary, I was juts doing some testing
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
HBO-HD Conversions Have No Sound EdwardATeller SageTV Software 0 03-03-2008 02:37 PM
Automatic Conversions EdwardATeller SageTV Software 2 03-01-2008 05:56 PM
Multiple Conversions at once? SnakeJG SageTV Software 0 05-16-2007 12:27 AM
Conversions Jackal24 SageTV Software 3 03-12-2007 10:35 AM
Conversions Waiting rsagetv99 SageTV Beta Test Software 6 12-04-2006 07:52 PM


All times are GMT -6. The time now is 02:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.