Index: xstream/src/java/com/thoughtworks/xstream/converters/enums/EnumSetConverter.java =================================================================== --- xstream/src/java/com/thoughtworks/xstream/converters/enums/EnumSetConverter.java (revision 1647) +++ xstream/src/java/com/thoughtworks/xstream/converters/enums/EnumSetConverter.java (working copy) @@ -37,8 +37,11 @@ */ public class EnumSetConverter implements Converter { - private final static Field typeField; - static { + private static Field typeField; + private static void doLazyStaticInit() { + if (typeField != null) { + return; + } // field name is "elementType" in Sun JDK, but different in Harmony Field assumedTypeField = null; Field[] fields = EnumSet.class.getDeclaredFields(); @@ -67,6 +70,7 @@ } public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { + doLazyStaticInit(); EnumSet set = (EnumSet) source; Class enumTypeForSet = (Class) Fields.read(typeField, set); String attributeName = mapper.aliasForSystemAttribute("enum-type"); Index: xstream/src/java/com/thoughtworks/xstream/converters/enums/EnumMapConverter.java =================================================================== --- xstream/src/java/com/thoughtworks/xstream/converters/enums/EnumMapConverter.java (revision 1647) +++ xstream/src/java/com/thoughtworks/xstream/converters/enums/EnumMapConverter.java (working copy) @@ -35,8 +35,11 @@ */ public class EnumMapConverter extends MapConverter { - private final static Field typeField; - static { + private static Field typeField; + private static void doLazyStaticInit() { + if (typeField != null) { + return; + } // field name is "keyType" in Sun JDK, but different in IKVM Field assumedTypeField = null; Field[] fields = EnumMap.class.getDeclaredFields(); @@ -63,6 +66,7 @@ } public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { + doLazyStaticInit(); Class type = (Class) Fields.read(typeField, source); String attributeName = mapper().aliasForSystemAttribute("enum-type"); if (attributeName != null) { Index: xstream/src/java/com/thoughtworks/xstream/converters/reflection/Sun14ReflectionProvider.java =================================================================== --- xstream/src/java/com/thoughtworks/xstream/converters/reflection/Sun14ReflectionProvider.java (revision 1647) +++ xstream/src/java/com/thoughtworks/xstream/converters/reflection/Sun14ReflectionProvider.java (working copy) @@ -32,9 +32,12 @@ */ public class Sun14ReflectionProvider extends PureJavaReflectionProvider { - private final static Unsafe unsafe; - private final static Exception exception; - static { + private static Unsafe unsafe; + private static Exception exception; + private static void doLazyStaticInit() { + if (unsafe != null || exception != null) { + return; + } Unsafe u = null; Exception ex = null; try { @@ -57,7 +60,7 @@ unsafe = u; } - private transient ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory(); + private transient ReflectionFactory reflectionFactory; private transient Map constructorCache = Collections.synchronizedMap(new WeakHashMap()); public Sun14ReflectionProvider() { @@ -91,17 +94,25 @@ final WeakReference ref = (WeakReference)constructorCache.get(type); Constructor ctor = (Constructor)(ref == null ? null : ref.get()); if (ctor == null) { - ctor = reflectionFactory.newConstructorForSerialization(type, Object.class.getDeclaredConstructor(new Class[0])); + ctor = getLazyRefectionFactory().newConstructorForSerialization(type, Object.class.getDeclaredConstructor(new Class[0])); constructorCache.put(type, new WeakReference(ctor)); } return ctor; } + private ReflectionFactory getLazyRefectionFactory() { + if (reflectionFactory == null) { + reflectionFactory = ReflectionFactory.getReflectionFactory(); + } + return reflectionFactory; + } + public void writeField(Object object, String fieldName, Object value, Class definedIn) { write(fieldDictionary.field(object.getClass(), fieldName, definedIn), object, value); } private void write(Field field, Object object, Object value) { + doLazyStaticInit(); if (exception != null) { throw new ObjectAccessException("Could not set field " + object.getClass() + "." + field.getName(), exception); } @@ -146,7 +157,6 @@ protected Object readResolve() { super.readResolve(); constructorCache = Collections.synchronizedMap(new WeakHashMap()); - reflectionFactory = ReflectionFactory.getReflectionFactory(); return this; } } Index: xstream/src/java/com/thoughtworks/xstream/converters/reflection/HarmonyReflectionProvider.java =================================================================== --- xstream/src/java/com/thoughtworks/xstream/converters/reflection/HarmonyReflectionProvider.java (revision 1647) +++ xstream/src/java/com/thoughtworks/xstream/converters/reflection/HarmonyReflectionProvider.java (working copy) @@ -32,9 +32,12 @@ * @author Joe Walnes */ public class HarmonyReflectionProvider extends PureJavaReflectionProvider { - private final static ObjectAccessor objectAccess; - private final static Exception exception; - static { + private static ObjectAccessor objectAccess; + private static Exception exception; + private static void doLazyStaticInit() { + if (objectAccess!=null || exception != null) { + return; + } ObjectAccessor accessor = null; Exception ex = null; Method method; @@ -62,6 +65,7 @@ } public Object newInstance(Class type) { + doLazyStaticInit(); if (exception != null) { throw new ObjectAccessException("Cannot construct " + type.getName(), exception); } @@ -79,6 +83,7 @@ } private void write(Field field, Object object, Object value) { + doLazyStaticInit(); if (exception != null) { throw new ObjectAccessException("Could not set field " + object.getClass() Index: xstream/src/java/com/thoughtworks/xstream/converters/reflection/AbstractAttributedCharacterIteratorAttributeConverter.java =================================================================== --- xstream/src/java/com/thoughtworks/xstream/converters/reflection/AbstractAttributedCharacterIteratorAttributeConverter.java (revision 1647) +++ xstream/src/java/com/thoughtworks/xstream/converters/reflection/AbstractAttributedCharacterIteratorAttributeConverter.java (working copy) @@ -32,8 +32,11 @@ public class AbstractAttributedCharacterIteratorAttributeConverter extends AbstractSingleValueConverter { - private static final Method getName; - static { + private static Method getName; + private static void doLazyStaticInit() { + if (getName != null) { + return; + } try { getName = AttributedCharacterIterator.Attribute.class.getDeclaredMethod( "getName", (Class[])null); @@ -47,9 +50,7 @@ private transient FieldDictionary fieldDictionary; public AbstractAttributedCharacterIteratorAttributeConverter(final Class type) { - super(); this.type = type; - readResolve(); } public boolean canConvert(final Class type) { @@ -57,6 +58,7 @@ } public String toString(final Object source) { + doLazyStaticInit(); AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute)source; try { if (!getName.isAccessible()) { @@ -74,10 +76,14 @@ } public Object fromString(final String str) { + readResolve(); return attributeMap.get(str); } private Object readResolve() { + if (fieldDictionary != null || attributeMap != null) { + return this; + } fieldDictionary = new FieldDictionary(); attributeMap = new HashMap(); for (final Iterator iterator = fieldDictionary.fieldsFor(type); iterator Index: xstream/src/java/com/thoughtworks/xstream/converters/collections/PropertiesConverter.java =================================================================== --- xstream/src/java/com/thoughtworks/xstream/converters/collections/PropertiesConverter.java (revision 1647) +++ xstream/src/java/com/thoughtworks/xstream/converters/collections/PropertiesConverter.java (working copy) @@ -38,8 +38,11 @@ */ public class PropertiesConverter implements Converter { - private final static Field defaultsField; - static { + private static Field defaultsField; + static void doLazyStaticInit() { + if (defaultsField != null) { + return; + } try { defaultsField = Fields.find(Properties.class, "defaults"); } catch (RuntimeException ex) { @@ -61,6 +64,7 @@ } public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { + doLazyStaticInit(); Properties properties = (Properties) source; Map map = sort ? (Map)new TreeMap(properties) : (Map)properties; for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { @@ -79,6 +83,7 @@ } public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { + doLazyStaticInit(); Properties properties = new Properties(); while (reader.hasMoreChildren()) { reader.moveDown();