// ***** READ THIS *****
// This class will only compile with JDK 1.5.0 or above as it test Java enums.
// If you are using an earlier version of Java, just don't try to build this class. XStream should work fine without it.

package com.thoughtworks.xstream.converters.enums;

import com.thoughtworks.xstream.converters.collections.MapConverter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.core.util.Fields;

import java.util.EnumMap;
import java.lang.reflect.Field;

/**
 * Serializes an Java 5 EnumMap, including the type of Enum it's for.
 *
 * @author Joe Walnes
 */
public class EnumMapConverter extends MapConverter {

    private final Field typeField;

    public EnumMapConverter(Mapper mapper) {
        super(mapper);
        typeField = Fields.find(EnumMap.class, "keyType");
    }

    public boolean canConvert(Class type) {
        return type == EnumMap.class;
    }

    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
        Class type = (Class) Fields.read(typeField, source);
        writer.addAttribute(mapper().aliasForAttribute("enum-type"), mapper().serializedClass(type));
        super.marshal(source, writer, context);
    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        Class type = mapper().realClass(reader.getAttribute(mapper().aliasForAttribute("enum-type")));
        EnumMap map = new EnumMap(type);
        populateMap(reader, context, map);
        return map;
    }
}

@@ -922,16 +922,23 @@
 e final 
+static 
 Field ty
@@ -950,77 +950,625 @@
 ld;%0A
-%0A    public EnumMapConverter(Mapper mapper) %7B%0A        super(mapper
+    static %7B%0A        // field name is %22keyType%22 in Sun JDK, but different in IKVM %0A        Field assumedTypeField = null;%0A        Field%5B%5D fields = EnumMap.class.getDeclaredFields();%0A        for (int i = 0; i %3C fields.length; i++) %7B%0A            if (fields%5Bi%5D.getType() == Class.class) %7B%0A                // take the fist member of type %22Class%22%0A                assumedTypeField = fields%5Bi%5D;%0A                assumedTypeField.setAccessible(true);%0A                break;%0A            %7D%0A        %7D%0A        if (assumedTypeField == null) %7B%0A            throw new InternalError(%22Cannot detect element type of EnumMap%22
 );%0A
+        %7D%0A
     
@@ -1587,44 +1587,98 @@
 d = 
-Fields.find(EnumMap.class, %22keyType%22
+assumedTypeField;%0A    %7D%0A%0A    public EnumMapConverter(Mapper mapper) %7B%0A        super(mapper
 );%0A 

Use first member of type Class as field keeping the type in an EnumMap (support for IKVM).