Thread View: borland.public.jbuilder.java.language
5 messages
5 total messages
Started by John Steel
Fri, 29 Jun 2007 10:42
Converting legacy code to use generics
Author: John Steel
Date: Fri, 29 Jun 2007 10:42
Date: Fri, 29 Jun 2007 10:42
32 lines
1731 bytes
1731 bytes
Hi, I'm trying to get rid of the warning ""GameListRenderer.java":
[unchecked] unchecked conversion; found : java.util.Collection, required:
java.util.Collection<? extends java.util.Collection> at line 95, column 83
in the following line:
ArrayList<Collection> gameList = new ArrayList <Collection>
(games.getValues());
games is an instance of this:
public class GameListManager {
TreeMap<String, GameRec> gameListTreeMap;
public GameListManager() {
gameListTreeMap = new TreeMap<String, GameRec>();
}
public Collection getValues() {
return gameListTreeMap.values();
}
...
}
As you can see getValues() returns a Collection, so I can't see what the
warning is trying to tell me.
Thanks very much
JB2006/jdk6.01
--
www.phonewebcam.com
Re: Converting legacy code to use generics
Author: Gillmer J. Derge [TeamB]
Date: Fri, 29 Jun 2007 12:39
Date: Fri, 29 Jun 2007 12:39
38 lines
2518 bytes
2518 bytes
John Steel wrote:
> public Collection getValues() {
> return gameListTreeMap.values();
> }
> ...
> }
>
> As you can see getValues() returns a Collection, so I can't see what the
> warning is trying to tell me.
gameListTreeMap is a TreeMap<String, GameRec>, so the values method
returns Collection<GameRec>. Since your getValues method returns a
plain Collection without any generic type parameters, you've lost the
restriction to GameRec, which is an unchecked conversion from
Collection<GameRec> to Collection. There are a few ways you can get rid
of the problem.
1. Change your getValues method's return type to Collection<GameRec>.
This is probably the most type safe solution, but depending on how big
your API is, it might just propagate the problem up the tree. Now
anyone that calls your getValues method expecting a Collection is going
to get a similar warning. In other words:
Collection foo = gameListManager.getValues(); // warning!
2. Add a SuppressWarnings annotation to the getValues method. This
basically tells the compiler, "Shut up, I know what I'm doing." It
works, and you probably do know what you're doing, but my preference is
to only do that when I have no other choice (ex. the warning comes from
interacting with some 3rd party API that I can't change).
@SuppressWarnings("unchecked")
public Collection getValues() {
return gameListTreeMap.values();
}
--
Gillmer J. Derge [TeamB]
Re: Converting legacy code to use generics
Author: John Steel
Date: Fri, 29 Jun 2007 23:36
Date: Fri, 29 Jun 2007 23:36
47 lines
3228 bytes
3228 bytes
"Gillmer J. Derge [TeamB]" <spam@gillmerderge.com> wrote in message
news:4684fd6d@newsgroups.borland.com...
>> As you can see getValues() returns a Collection, so I can't see what the
>> warning is trying to tell me.
>
> Collection<GameRec> to Collection. There are a few ways you can get rid
> of the problem.
>
> 1. Change your getValues method's return type to Collection<GameRec>. This
> is probably the most type safe solution, but depending on how big your API
> is, it might just propagate the problem up the tree. Now anyone that
> calls your getValues method expecting a Collection is going to get a
> similar warning. In other words:
>
> Collection foo = gameListManager.getValues(); // warning!
Thanks very much for your help, this is very kind of you.
I am in control of all the source, and the api is pretty much limited to
these 2 classes (at present!).
As expected, when I changed the GameListManagers getValues() to read
public Collection<GameRec> getValues() {
return gameListTreeMap.values();
}
I found it compiled fine but then the original calling line now gives these
2 errors:
"GameListRenderer.java": cannot find symbol; symbol : constructor
ArrayList(java.util.Collection<GameRec>), location: class
java.util.ArrayList<java.util.Collection> at line 94, column 40
"GameListRenderer.java": internal error; cannot instantiate
java.util.ArrayList.<init> at java.util.ArrayList<java.util.Collection> to
() at line 94, column 40
I'm trying to think this through by describing it in English - I think I'm
asking for an ArrayList of GameRecs, but we know games.getValues() now
returns the type safe Collection so its almost as if the syntax end up like
ArrayList<Collection<GameRec>> gameList =... , but that wouldn't compile at
all
(the original caller is ArrayList<Collection> gameList = new ArrayList
<Collection> (games.getValues()); ).
I too would like a clean build without "hacks". There are a few others of
this ilk relating to Comparators, so I'd like to fix those having understood
this one.
Re: Converting legacy code to use generics
Author: Gillmer J. Derge [TeamB]
Date: Sat, 30 Jun 2007 00:20
Date: Sat, 30 Jun 2007 00:20
12 lines
1458 bytes
1458 bytes
John Steel wrote: > (the original caller is ArrayList<Collection> gameList = new ArrayList > <Collection> (games.getValues()); ). Your new ArrayList isn't going to be a list of Collection's. It's a list of GameRec's, containing the same GameRec's that are in the games.getValues() collection. ArrayList<GameRec> gameList = new ArrayList<GameRec>(games.getValues()); -- Gillmer J. Derge [TeamB]
Re: Converting legacy code to use generics
Author: John Steel
Date: Sun, 01 Jul 2007 16:46
Date: Sun, 01 Jul 2007 16:46
18 lines
1805 bytes
1805 bytes
"Gillmer J. Derge [TeamB]" <spam@gillmerderge.com> wrote in message news:4685a1e7@newsgroups.borland.com... > John Steel wrote: >> (the original caller is ArrayList<Collection> gameList = new >> ArrayList <Collection> (games.getValues()); ). > > Your new ArrayList isn't going to be a list of Collection's. It's a list > of GameRec's, containing the same GameRec's that are in the > games.getValues() collection. > > ArrayList<GameRec> gameList = new ArrayList<GameRec>(games.getValues()); > That did it - thanks. I even fixed the Comparater one after your help too - thanks again. -- John www.phonewebcam.com
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads