Title: [2031] trunk: Support replacement of default converter in any case (XSTR-719).

Diff

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/XStream.java (2030 => 2031)


--- trunk/xstream/src/java/com/thoughtworks/xstream/XStream.java	2013-02-28 01:45:20 UTC (rev 2030)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/XStream.java	2013-03-01 22:27:46 UTC (rev 2031)
@@ -102,7 +102,6 @@
 import com.thoughtworks.xstream.converters.reflection.ExternalizableConverter;
 import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
 import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
-import com.thoughtworks.xstream.converters.reflection.SelfStreamingInstanceChecker;
 import com.thoughtworks.xstream.converters.reflection.SerializableConverter;
 import com.thoughtworks.xstream.core.DefaultConverterLookup;
 import com.thoughtworks.xstream.core.JVM;
@@ -114,6 +113,7 @@
 import com.thoughtworks.xstream.core.util.CompositeClassLoader;
 import com.thoughtworks.xstream.core.util.CustomObjectInputStream;
 import com.thoughtworks.xstream.core.util.CustomObjectOutputStream;
+import com.thoughtworks.xstream.core.util.SelfStreamingInstanceChecker;
 import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
@@ -645,9 +645,8 @@
     }
 
     protected void setupConverters() {
-        final ReflectionConverter reflectionConverter = new ReflectionConverter(
-            mapper, reflectionProvider);
-        registerConverter(reflectionConverter, PRIORITY_VERY_LOW);
+        registerConverter(
+            new ReflectionConverter(mapper, reflectionProvider), PRIORITY_VERY_LOW);
 
         registerConverter(
             new SerializableConverter(mapper, reflectionProvider, classLoaderReference), PRIORITY_LOW);
@@ -713,8 +712,8 @@
                 PRIORITY_NORMAL, new Class[]{Mapper.class}, new Object[]{mapper});
             registerConverterDynamically(
                 "com.thoughtworks.xstream.converters.extended.ThrowableConverter",
-                PRIORITY_NORMAL, new Class[]{Converter.class},
-                new Object[]{reflectionConverter});
+                PRIORITY_NORMAL, new Class[]{ConverterLookup.class},
+                new Object[]{converterLookup});
             registerConverterDynamically(
                 "com.thoughtworks.xstream.converters.extended.StackTraceElementConverter",
                 PRIORITY_NORMAL, null, null);
@@ -723,8 +722,7 @@
                 PRIORITY_NORMAL, null, null);
             registerConverterDynamically(
                 "com.thoughtworks.xstream.converters.extended.RegexPatternConverter",
-                PRIORITY_NORMAL, new Class[]{Converter.class},
-                new Object[]{reflectionConverter});
+                PRIORITY_NORMAL, null, null);
             registerConverterDynamically(
                 "com.thoughtworks.xstream.converters.extended.CharsetConverter",
                 PRIORITY_NORMAL, null, null);
@@ -755,7 +753,7 @@
         }
 
         registerConverter(
-            new SelfStreamingInstanceChecker(reflectionConverter, this), PRIORITY_NORMAL);
+            new SelfStreamingInstanceChecker(converterLookup, this), PRIORITY_NORMAL);
     }
 
     private void registerConverterDynamically(String className, int priority,

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/RegexPatternConverter.java (2030 => 2031)


--- trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/RegexPatternConverter.java	2013-02-28 01:45:20 UTC (rev 2030)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/RegexPatternConverter.java	2013-03-01 22:27:46 UTC (rev 2031)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2004, 2005 Joe Walnes.
- * Copyright (C) 2006, 2007 XStream Committers.
+ * Copyright (C) 2006, 2007, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -21,13 +21,22 @@
 
 /**
  * Ensures java.util.regex.Pattern is compiled upon deserialization.
+ * 
+ * @author Joe Walnes
+ * @author Jörg Schaible
  */
 public class RegexPatternConverter implements Converter {
 
-    private Converter defaultConverter;
+    /**
+     * @since upcoming
+     */
+    public RegexPatternConverter() {
+    }
 
+    /**
+     * @deprecated As of upcoming, use {@link #RegexPatternConverter()} instead
+     */
     public RegexPatternConverter(Converter defaultConverter) {
-        this.defaultConverter = defaultConverter;
     }
 
     public boolean canConvert(final Class type) {
@@ -35,12 +44,23 @@
     }
 
     public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
-        defaultConverter.marshal(source, writer, context);
+        Pattern pattern = (Pattern)source;
+        writer.startNode("pattern");
+        writer.setValue(pattern.pattern());
+        writer.endNode();
+        writer.startNode("flags");
+        writer.setValue(String.valueOf(pattern.flags()));
+        writer.endNode();
     }
 
     public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
-        Pattern notCompiled = (Pattern) defaultConverter.unmarshal(reader, context);
-        return Pattern.compile(notCompiled.pattern(), notCompiled.flags());
+        reader.moveDown();
+        String pattern = reader.getValue();
+        reader.moveUp();
+        reader.moveDown();
+        int flags = Integer.parseInt(reader.getValue());
+        reader.moveUp();
+        return Pattern.compile(pattern, flags);
     }
 
 }

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/ThrowableConverter.java (2030 => 2031)


--- trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/ThrowableConverter.java	2013-02-28 01:45:20 UTC (rev 2030)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/ThrowableConverter.java	2013-03-01 22:27:46 UTC (rev 2031)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2004 Joe Walnes.
- * Copyright (C) 2006, 2007 XStream Committers.
+ * Copyright (C) 2006, 2007, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -12,25 +12,39 @@
 package com.thoughtworks.xstream.converters.extended;
 
 import com.thoughtworks.xstream.converters.Converter;
+import com.thoughtworks.xstream.converters.ConverterLookup;
 import com.thoughtworks.xstream.converters.MarshallingContext;
 import com.thoughtworks.xstream.converters.UnmarshallingContext;
 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 
 /**
- * Converter for Throwable (and Exception) that retains stack trace, for JDK1.4 only.
+ * Converter for Throwable (and Exception) that retains stack trace.
  *
  * @author <a href="" K. Oxley (binkley)</a>
  * @author Joe Walnes
+ * @author J&ouml;rg Schaible
  */
 public class ThrowableConverter implements Converter {
     
     private Converter defaultConverter;
+    private final ConverterLookup lookup;
 
+    /**
+     * @deprecated As of upcoming use {@link #ThrowableConverter(ConverterLookup)}
+     */
     public ThrowableConverter(Converter defaultConverter) {
         this.defaultConverter = defaultConverter;
+        lookup = null;
     }
 
+    /**
+     * @since upcoming
+     */
+    public ThrowableConverter(ConverterLookup lookup) {
+        this.lookup = lookup;
+    }
+
     public boolean canConvert(final Class type) {
         return Throwable.class.isAssignableFrom(type);
     }
@@ -45,10 +59,14 @@
             }
         }
         throwable.getStackTrace(); // Force stackTrace field to be lazy loaded by special JVM native witchcraft (outside our control).
-        defaultConverter.marshal(throwable, writer, context);
+        getConverter().marshal(throwable, writer, context);
     }
 
+    private Converter getConverter() {
+        return defaultConverter != null ? defaultConverter : lookup.lookupConverterForType(Object.class);
+    }
+
     public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
-        return defaultConverter.unmarshal(reader, context);
+        return getConverter().unmarshal(reader, context);
     }
 }

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/converters/reflection/SelfStreamingInstanceChecker.java (2030 => 2031)


--- trunk/xstream/src/java/com/thoughtworks/xstream/converters/reflection/SelfStreamingInstanceChecker.java	2013-02-28 01:45:20 UTC (rev 2030)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/converters/reflection/SelfStreamingInstanceChecker.java	2013-03-01 22:27:46 UTC (rev 2031)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006, 2007 XStream Committers.
+ * Copyright (C) 2006, 2007, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -10,12 +10,7 @@
  */
 package com.thoughtworks.xstream.converters.reflection;
 
-import com.thoughtworks.xstream.converters.ConversionException;
 import com.thoughtworks.xstream.converters.Converter;
-import com.thoughtworks.xstream.converters.MarshallingContext;
-import com.thoughtworks.xstream.converters.UnmarshallingContext;
-import com.thoughtworks.xstream.io.HierarchicalStreamReader;
-import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 
 /**
  * A special converter that prevents self-serialization. The serializing XStream instance
@@ -24,30 +19,12 @@
  * 
  * @author J&ouml;rg Schaible
  * @since 1.2
+ * @deprecated As of upcoming use {@link com.thoughtworks.xstream.core.util.SelfStreamingInstanceChecker}
  */
-public class SelfStreamingInstanceChecker implements Converter {
+public class SelfStreamingInstanceChecker extends com.thoughtworks.xstream.core.util.SelfStreamingInstanceChecker {
 
-    private final Object self;
-    private Converter defaultConverter;
-
     public SelfStreamingInstanceChecker(Converter defaultConverter, Object xstream) {
-        this.defaultConverter = defaultConverter;
-        this.self = xstream;
+        super(defaultConverter, xstream);
     }
 
-    public boolean canConvert(Class type) {
-        return type == self.getClass();
-    }
-
-    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
-        if (source == self) {
-            throw new ConversionException("Cannot marshal the XStream instance in action");
-        }
-        defaultConverter.marshal(source, writer, context);
-    }
-
-    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
-        return defaultConverter.unmarshal(reader, context);
-    }
-
 }

Copied: trunk/xstream/src/java/com/thoughtworks/xstream/core/util/SelfStreamingInstanceChecker.java (from rev 2004, trunk/xstream/src/java/com/thoughtworks/xstream/converters/reflection/SelfStreamingInstanceChecker.java) (0 => 2031)


--- trunk/xstream/src/java/com/thoughtworks/xstream/core/util/SelfStreamingInstanceChecker.java	                        (rev 0)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/core/util/SelfStreamingInstanceChecker.java	2013-03-01 22:27:46 UTC (rev 2031)
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2006, 2007, 2013 XStream Committers.
+ * All rights reserved.
+ *
+ * The software in this package is published under the terms of the BSD
+ * style license a copy of which has been included with this distribution in
+ * the LICENSE.txt file.
+ * 
+ * Created on 01. March 2013 by Joerg Schaible, moved from package
+ * com.thoughtworks.xstream.converters.reflection.
+ */
+package com.thoughtworks.xstream.core.util;
+
+import com.thoughtworks.xstream.converters.ConversionException;
+import com.thoughtworks.xstream.converters.Converter;
+import com.thoughtworks.xstream.converters.ConverterLookup;
+import com.thoughtworks.xstream.converters.MarshallingContext;
+import com.thoughtworks.xstream.converters.UnmarshallingContext;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+
+/**
+ * A special converter that prevents self-serialization. The serializing XStream instance
+ * adds a converter of this type to prevent self-serialization and will throw an
+ * exception instead.
+ * 
+ * @author J&ouml;rg Schaible
+ * @since 1.2
+ */
+public class SelfStreamingInstanceChecker implements Converter {
+
+    private final Object self;
+    private Converter defaultConverter;
+    private final ConverterLookup lookup;
+
+    /**
+     * @since upcoming
+     */
+    public SelfStreamingInstanceChecker(ConverterLookup lookup, Object xstream) {
+        this.lookup = lookup;
+        this.self = xstream;
+    }
+
+    /**
+     * @deprecated As of upcoming use {@link #SelfStreamingInstanceChecker(ConverterLookup, Object)}
+     */
+    public SelfStreamingInstanceChecker(Converter defaultConverter, Object xstream) {
+        this.defaultConverter = defaultConverter;
+        this.self = xstream;
+        lookup = null;
+    }
+
+    public boolean canConvert(Class type) {
+        return type == self.getClass();
+    }
+
+    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
+        if (source == self) {
+            throw new ConversionException("Cannot marshal the XStream instance in action");
+        }
+        getConverter().marshal(source, writer, context);
+    }
+
+    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
+        return getConverter().unmarshal(reader, context);
+    }
+
+    private Converter getConverter() {
+        return defaultConverter != null ? defaultConverter : lookup.lookupConverterForType(Object.class);
+    }
+}

Modified: trunk/xstream-distribution/src/content/changes.html (2030 => 2031)


--- trunk/xstream-distribution/src/content/changes.html	2013-02-28 01:45:20 UTC (rev 2030)
+++ trunk/xstream-distribution/src/content/changes.html	2013-03-01 22:27:46 UTC (rev 2031)
@@ -53,9 +53,21 @@
     <h2>Minor changes</h2>
     
     <ul>
+    	<li>JIRA:XSTR-719: Support replacement of default converter in any case.</li>
     	<li>Current IBM JDK for Java 1.4.2 no longer has a reverse field ordering.</li>
     </ul>
 
+    <h2>API changes</h2>
+
+    <ul>
+    	<li>Deprecated constructor c.t.x.converters.extended.RegexPatternConverter(Converter)
+    	in favor of c.t.x.converters.extended.RegexPatternConverter().</li>
+    	<li>Deprecated constructor c.t.x.converters.extended.ThrowableConverter(Converter)
+    	in favor of c.t.x.converters.extended.ThrowableConverter(ConverterLookup).</li>
+    	<li>Deprecated class c.t.x.converters.reflection.SelfStreamingInstanceChecker and moved
+    	original implementation into c.t.x.core.util, since it is internal.</li>
+    </ul>
+
     <h1 id="1.4.4">1.4.4</h1>
 
     <p>Released 19. January 2013.</p>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to