Title: [2330] branches/v-1.4.x: Merge fix for XSTR-761 from trunk.

Diff

Property changes: branches/v-1.4.x


Modified: svn:mergeinfo

+ /trunk:2151-2152,2154-2156,2158-2163,2165,2172,2175,2177,2188-2189,2197,2199-2201,2204,2206,2210,2212,2214-2217,2226,2229,2231,2233-2234,2236-2238,2247-2249,2279-2280,2285,2291-2292,2294,2298-2299,2301,2303,2306-2310,2312,2314,2316-2318,2320,2322,2324,2326,2329

Modified: branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/converters/reflection/SerializableConverter.java (2329 => 2330)


--- branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/converters/reflection/SerializableConverter.java	2015-02-12 22:40:06 UTC (rev 2329)
+++ branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/converters/reflection/SerializableConverter.java	2015-02-12 22:46:38 UTC (rev 2330)
@@ -390,6 +390,10 @@
             }
 
             public void defaultReadObject() {
+                if (serializationMethodInvoker.getSerializablePersistentFields(currentType[0]) != null) {
+                    readFieldsFromStream();
+                    return;
+                }
                 if (!reader.hasMoreChildren()) {
                     return;
                 }

Modified: branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/converters/reflection/SerializationMethodInvoker.java (2329 => 2330)


--- branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/converters/reflection/SerializationMethodInvoker.java	2015-02-12 22:40:06 UTC (rev 2329)
+++ branches/v-1.4.x/xstream/src/java/com/thoughtworks/xstream/converters/reflection/SerializationMethodInvoker.java	2015-02-12 22:46:38 UTC (rev 2330)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2004, 2005 Joe Walnes.
- * Copyright (C) 2006, 2007, 2008, 2010, 2011, 2014 XStream Committers.
+ * Copyright (C) 2006, 2007, 2008, 2010, 2011, 2014, 2015 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -17,6 +17,8 @@
 
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.io.ObjectStreamField;
+import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
@@ -41,6 +43,8 @@
     }).getClass().getDeclaredMethods()[0];
     private static final Object[] EMPTY_ARGS = new Object[0];
     private static final Class[] EMPTY_CLASSES = new Class[0];
+    private static final Map<String, ObjectStreamField> NO_FIELDS = Collections.emptyMap();
+    private static final int PERSISTENT_FIELDS_MODIFIER = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
     private static final FastField[] OBJECT_TYPE_FIELDS = {
         new FastField(Object.class, "readResolve"), 
         new FastField(Object.class, "writeReplace"), 
@@ -49,6 +53,7 @@
     };
     private Map declaredCache = Collections.synchronizedMap(new HashMap());
     private Map resRepCache = Collections.synchronizedMap(new HashMap());
+    private final Map fieldCache = Collections.synchronizedMap(new HashMap());
     {
         for(int i = 0; i < OBJECT_TYPE_FIELDS.length; ++i) {
             declaredCache.put(OBJECT_TYPE_FIELDS[i], NO_METHOD);
@@ -200,6 +205,38 @@
         return result == NO_METHOD ? null : result;
     }
 
+    public Map getSerializablePersistentFields(final Class type) {
+        if (type == null) {
+            return null;
+        }
+        Map result = (Map)fieldCache.get(type.getName());
+        if (result == null) {
+            try {
+                final Field field = type.getDeclaredField("serialPersistentFields");
+                if ((field.getModifiers() & PERSISTENT_FIELDS_MODIFIER) == PERSISTENT_FIELDS_MODIFIER) {
+                    field.setAccessible(true);
+                    final ObjectStreamField[] fields = (ObjectStreamField[])field.get(null);
+                    if (fields != null) {
+                        result = new HashMap();
+                        for (final ObjectStreamField f : fields) {
+                            result.put(f.getName(), f);
+                        }
+                    }
+                }
+            } catch (final NoSuchFieldException e) {
+            } catch (final IllegalAccessException e) {
+                throw new ObjectAccessException("Cannot get " + type.getName() + ".serialPersistentFields.", e);
+            } catch (final ClassCastException e) {
+                throw new ObjectAccessException("Cannot get " + type.getName() + ".serialPersistentFields.", e);
+            }
+            if (result == null) {
+                result = NO_FIELDS;
+            }
+            fieldCache.put(type.getName(), result);
+        }
+        return result == NO_FIELDS ? null : result;
+    }
+
     public void flushCache() {
         declaredCache.keySet().retainAll(Arrays.asList(OBJECT_TYPE_FIELDS));
         resRepCache.keySet().retainAll(Arrays.asList(OBJECT_TYPE_FIELDS));

Modified: branches/v-1.4.x/xstream/src/test/com/thoughtworks/acceptance/Concurrent15TypesTest.java (2329 => 2330)


--- branches/v-1.4.x/xstream/src/test/com/thoughtworks/acceptance/Concurrent15TypesTest.java	2015-02-12 22:40:06 UTC (rev 2329)
+++ branches/v-1.4.x/xstream/src/test/com/thoughtworks/acceptance/Concurrent15TypesTest.java	2015-02-12 22:46:38 UTC (rev 2330)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 XStream Committers.
+ * Copyright (C) 2012, 2015 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -10,16 +10,20 @@
  */
 package com.thoughtworks.acceptance;
 
+import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
+import com.thoughtworks.xstream.core.JVM;
+
+
 public class Concurrent15TypesTest extends AbstractAcceptanceTest {
-    
+
     public void testConcurrentHashMap() {
         ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>();
         map.put("walnes", "joe");
         String xml = xstream.toXML(map);
-        String expected = 
-               "<concurrent-hash-map>\n"
+        String expected = ""
+            + "<concurrent-hash-map>\n"
             + "  <entry>\n"
             + "    <string>walnes</string>\n"
             + "    <string>joe</string>\n"
@@ -27,7 +31,36 @@
             + "</concurrent-hash-map>";
         assertEquals(xml, expected);
         @SuppressWarnings("unchecked")
-        ConcurrentHashMap<String, String> out = (ConcurrentHashMap<String, String>) xstream.fromXML(xml);
+        ConcurrentHashMap<String, String> out = (ConcurrentHashMap<String, String>)xstream.fromXML(xml);
         assertEquals("{walnes=joe}", out.toString());
     }
+
+    public static class DerivedConcurrentHashMap extends ConcurrentHashMap<Object, Object> {
+        private static final long serialVersionUID = 1L;
+    }
+
+    public void testDerivedConcurrentHashMap() {
+        if (JVM.is18()) {
+            xstream.alias("derived-map", DerivedConcurrentHashMap.class);
+
+            Map<Object, Object> map = new DerivedConcurrentHashMap();
+            map.put("test", "JUnit");
+
+            String xml = ""
+                + "<derived-map serialization=\"custom\">\n"
+                + "  <unserializable-parents/>\n"
+                + "  <concurrent-hash-map>\n"
+                + "    <default>\n"
+                + "      <segmentMask>15</segmentMask>\n"
+                + "    </default>\n"
+                + "    <string>test</string>\n"
+                + "    <string>JUnit</string>\n"
+                + "    <null/>\n"
+                + "    <null/>\n"
+                + "  </concurrent-hash-map>\n"
+                + "</derived-map>";
+
+            assertBothWays(map, xml);
+        }
+    }
 }

Modified: branches/v-1.4.x/xstream/src/test/com/thoughtworks/acceptance/CustomSerializationTest.java (2329 => 2330)


--- branches/v-1.4.x/xstream/src/test/com/thoughtworks/acceptance/CustomSerializationTest.java	2015-02-12 22:40:06 UTC (rev 2329)
+++ branches/v-1.4.x/xstream/src/test/com/thoughtworks/acceptance/CustomSerializationTest.java	2015-02-12 22:46:38 UTC (rev 2330)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2004, 2005 Joe Walnes.
- * Copyright (C) 2006, 2007 XStream Committers.
+ * Copyright (C) 2006, 2007, 2015 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -519,4 +519,42 @@
 
         assertBothWays(input, expectedXml);
     }
+    
+    public static class ObjectThatWritesCustomFieldsButDoesNotReadThem extends StandardObject implements Serializable {
+
+        private static final ObjectStreamField[] serialPersistentFields = {
+            new ObjectStreamField("number", int.class),
+            new ObjectStreamField("name", String.class),
+        };
+
+        private void writeObject(ObjectOutputStream out) throws IOException {
+            ObjectOutputStream.PutField fields = out.putFields();
+            fields.put("name", "test");
+            fields.put("number", 42);
+            out.writeFields();
+        }
+
+        private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
+            in.defaultReadObject();
+        }
+
+    }
+
+    public void testSupportsPutFieldsWithoutGetFields() {
+        xstream.alias("an-object", ObjectThatWritesCustomFieldsButDoesNotReadThem.class);
+
+        ObjectThatWritesCustomFieldsButDoesNotReadThem input = new ObjectThatWritesCustomFieldsButDoesNotReadThem();
+
+        String expectedXml = ""
+                + "<an-object serialization=\"custom\">\n"
+                + "  <an-object>\n"
+                + "    <default>\n"
+                + "      <name>test</name>\n"
+                + "      <number>42</number>\n"
+                + "    </default>\n"
+                + "  </an-object>\n"
+                + "</an-object>";
+
+        assertBothWays(input, expectedXml);
+    }
 }

Modified: branches/v-1.4.x/xstream-distribution/src/content/changes.html (2329 => 2330)


--- branches/v-1.4.x/xstream-distribution/src/content/changes.html	2015-02-12 22:40:06 UTC (rev 2329)
+++ branches/v-1.4.x/xstream-distribution/src/content/changes.html	2015-02-12 22:46:38 UTC (rev 2330)
@@ -42,6 +42,7 @@
     	<li>Detect Java 9 runtime.</li>
     	<li>XSTR-767: Deserialization of referenced lambda expressions fail.</li>
     	<li>XSTR-762: Private method readResolve() called on base classes.</li>
+    	<li>XSTR-761: Support ignored serialPersistentField at deserialization time.</li>
     	<li>XSTR-755: ExternalizableConverter does not respect writeReplace and readResolve.</li>
     	<li>XSTR-757: Deserialized TreeSet does not honor remove(Object) return value contract.</li>
     	<li>Fix: DateConverter ignores provided locale.</li>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to