aspose file tools*
The moose likes Java in General and the fly likes Wanted: Help serializing Generic Maps! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Wanted: Help serializing Generic Maps!" Watch "Wanted: Help serializing Generic Maps!" New topic
Author

Wanted: Help serializing Generic Maps!

Siegfried Heintze
Ranch Hand

Joined: Aug 11, 2000
Posts: 376
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
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

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

Joined: Aug 11, 2000
Posts: 376
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
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

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

Joined: Aug 11, 2000
Posts: 376
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


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wanted: Help serializing Generic Maps!
 
Similar Threads
more generics
Generics and Colletions
Generics are going to be the death of me.
How to implement and serialize nested Maps?
How to Serialize Maps?