Typesafety lost for template class (bug?)

Hi all,

I have this template class BlResponse<T> containing a List<String>.

When I call the contructor as

BlResponse<Long> typesafeObject =new BlResponse<Long>();

then the following statement returns a List<String> typesafeObject.getMessages();

But, when I do this BlResponse duplicateObject = typesafeObject;

then the following statement no longer returns a List<String> but a List, and all typesafety is gone duplicateObject.getMessages();

I've been using both Netbeans 5 as Eclipse 3.1, and both act the same. About a month ago I posted this as a possible bug, but I haven't heard anything about it since. So I was wondering, is this a bug? Or can it be solved in another way?

Here's the complete code:

import java.util.LinkedList;

import java.util.List;

publicclass BlResponse<T>{

private List<String> messages;

private T object;

public BlResponse(){

messages =new LinkedList<String>();

}

public T getObject(){

return object;

}

publicvoid setObject(T object){

this.object = object;

}

public List<String> getMessages(){

return messages;

}

publicvoid setMessages(List<String> messages){

this.messages = messages;

}

publicstaticvoid main(String[] args){

BlResponse<Long> typesafeObject =new BlResponse<Long>();

typesafeObject.getMessages();// getMessages returns List<String>

BlResponse duplicateObject = typesafeObject;

duplicateObject.getMessages();// BUG: getMessages returns List !

}

}

cheers, pj

[2865 byte] By [PJ_Savata] at [2007-10-2 22:01:09]
# 1
1) Java doesn't have templates. It has generics. If you used the word "template" because you come from a C++ background, it's probably vitally important for you to understand the difference.2) Declare your duplicateObject to be of type BlResponse<Long>
dannyyatesa at 2007-7-14 1:17:25 > top of Java-index,Core,Core APIs...
# 2

First:

"What's in a name..." I use both templates as generics. But you are correct.

Second:

The current version of the BlResponse class does not have the T

and contains a private Object object;

Because this BlResponse is used in many client applications as the returning object of some remote method call, I want to be backwards compatible for the new version, at least for what the code is concerned.

Meaning, if I add the T, I do not want rewrite the following lines of codes in tons of places:

from

BlResponse response = server.doSomething();

for (String s : response.getMessages()) {

// typesafe list of strings

}

to

BlResponse response = server.doSomething();

List<String> castList = (List<String>) response.getMessages();

for (String s : castList ) {

// list no longer typesafe, a cast is needed

}

PJ_Savata at 2007-7-14 1:17:25 > top of Java-index,Core,Core APIs...