Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > Converting legacy code to use generics
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 4th November 03:50
john steel
External User
 
Posts: 1
Default Converting legacy code to use generics



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
  Reply With Quote


 


2 4th November 03:50
gillmer j. derge [teamb]
External User
 
Posts: 1
Default Converting legacy code to use generics



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]
  Reply With Quote
3 4th November 03:50
john steel
External User
 
Posts: 1
Default Converting legacy code to use generics


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.
  Reply With Quote
4 4th November 03:50
gillmer j. derge [teamb]
External User
 
Posts: 1
Default Converting legacy code to use generics


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]
  Reply With Quote
5 4th November 03:51
john steel
External User
 
Posts: 1
Default Converting legacy code to use generics


That did it - thanks. I even fixed the Comparater one after your help too -
thanks again.

-- John
www.phonewebcam.com
  Reply With Quote


 


Reply


Thread Tools
Display Modes




666