Results 1 to 8 of 8
- 11-15-2010, 02:23 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Strange Compilation Error About Generics
Hi all,
I'm getting a strange type casting compilation error for what seems to be a simple case.
Have the following class:
class GenericResult<T> implements Serializable {
private List<Message> messages = new ArrayList<Message>();
private T data;
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
if I don't specify a type for the class above when creating it (which is valid with a warning) the compiler converts the List of <Messages> to a List of <Object> and I lose the type although the List has nothing to do with the Class type parameter!
So, if I go like this:
GenericResult myResult = new GenericResult();
for(Message msg: myResult.getMessages() ) {
...
}
This will create compilation error on the for loop saying it cannot convert from Object to Message, although the list is defined as Message type!
If I specify any dummy type when creating the class above (say like new GenericResult<Date>()) then the compiler will be happy!
Note that any other non-List property will maintain its type regardless, it seems that the Collection has this problem?
Can someone explain please?
Thanks,Last edited by dhafirnz; 11-15-2010 at 10:46 PM.
- 11-15-2010, 10:36 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Curious.
I'm sure Jos can explain why...
(Tolls waits patiently)
- 11-15-2010, 06:07 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Show us the code for your getMessages method; does it return something of type List<Message>?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-15-2010, 08:18 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Hi,
Yes, the getter/setter all typed.
public List<Message> getMessages() {
return messages;
}
public void setMessages(List<Message> messages) {
this.messages = messages;
}
Thanks for any help in solving this mystery. I am using Java 1.6 by the way.Last edited by dhafirnz; 11-15-2010 at 08:23 PM.
- 11-16-2010, 12:51 AM #5
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
just guessing..
My guess is that because a type was not bound to the generic type, the compiler downgrades the byte code to a pre Java 5 version (like 1.4), and hence a typed List will loose its type!
Very confusing! Why would it do that?
- 11-16-2010, 08:11 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Legacy support; read paragraph 4.8 Raw Types in the JLS; any member of a parameterized type that is also parameterized (no matter if the types are the same as the parameter type of the class you're defining) will be 'erased' to its raw type to avoid 'rare' types. Personally I find it a bit rude but I can see the problems that arise if the erasure isn't complete. They also sort of warn that raw types may not be supported in the future. Interesting, I never thought about it until I read your example; thanks for that ...
kind regards,
Jos
ps. Both javacc and jide (the Eclipse compiler) do this; I don't know about other compilers.When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-16-2010, 08:48 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
"Personally I find it a bit rude"
:)
That made me chuckle.
- 11-16-2010, 08:54 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
bean compilation error
By technical_helps@yahoo.com in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 07-29-2009, 11:21 PM -
java.lang.Error: Unresolved compilation problems
By jon80 in forum New To JavaReplies: 0Last Post: 06-07-2009, 10:04 PM -
JAVA compilation error in UNIX
By satish kumar in forum Advanced JavaReplies: 9Last Post: 08-08-2008, 07:36 AM -
compilation error(version problem?)
By Ms.Ranjan in forum New To JavaReplies: 3Last Post: 07-11-2008, 04:31 PM -
compilation error with Jcreator
By Heather in forum JCreatorReplies: 2Last Post: 06-30-2007, 04:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks