• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wanted: Help serializing Generic Maps!

 
Siegfried Heintze
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The previous version of this code works properly but does not use generics. I want to use generics.

I don't know how to deal with generic arrays like Item<K,V>[]. When I tried using ArrayList<Item><K,V>> instead of Item<K,V>[] the warning and the stack trace went away -- and so did the serialization!

Can someone kindly help me fix these problems so it will serialize properly?
Can someone kindly explain why I can say new ArrayList<Item><K,V>> but not new Item<K,V>[]. Thru google searching I've found such explanations as "K and V don't exist at run time so we cannot define an array of Item<K,V>, instead we can only define an array of Item." Is there basically just an inconsistency between arrays of Item<K,V> and Vectors, Sets and ArrayLists of Item<K,V>? It seems to me they should have been consistent instead of making arrays a special case. Perhaps someone can explain why sun was inconsistent here?

thanks,
Siegfried



Here is the stack trace and comilation warning:
 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siegfried Heintze wrote:I don't know how to deal with generic arrays like Item<K,V>[].


So read the original Generics tutorial and on page 15 it explains why you can't have generic arrays.
 
Siegfried Heintze
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!

So I tried to apply page 15 and I'm still getting warnings on lines 27 and 28. Why cannot I cast without a warning. The examples on page 15 have an example of storing an element in an array, and examples of how not to extract an element in an array, and no examples of how to properly extract an element from an array!

Why cannot I cast on lines 27-28?

Thanks,
Siegfried

 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't you cast without a warning in that context? Because as that reference says, generic arrays are inherently not type-safe.
 
Siegfried Heintze
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code below works. However, it won't let me easily accommodate different types via generics. When I change the data members of Item2 to type Object, I get the following error:

@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.

Well that kinda makes sense. It seems like an unnecessary constraint, however, since I'm writing the code to convert strings to/from objects using casts.

Hmmm....

When I change Item2 to be the following, I get the same error:



I guess that makes sense too if I pass Item2<K,V> here:



Apparently the marshallar is does not know that K and V have been expanded to String and String (respectively) and is seeing K and V as objects.

So how do I get around this problem?

Thanks,
Siegfried


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic