Title: [1971] trunk: JavaBeanConverter supports now instantiation for a specific type and can therefore be used in @XStreamConverter annotation.
Revision
1971
Author
joehni
Date
2012-03-29 19:09:53 -0500 (Thu, 29 Mar 2012)

Log Message

JavaBeanConverter supports now instantiation for a specific type and can therefore be used in @XStreamConverter annotation.

Modified Paths

Diff

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/converters/javabean/JavaBeanConverter.java (1970 => 1971)


--- trunk/xstream/src/java/com/thoughtworks/xstream/converters/javabean/JavaBeanConverter.java	2012-03-30 00:07:23 UTC (rev 1970)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/converters/javabean/JavaBeanConverter.java	2012-03-30 00:09:53 UTC (rev 1971)
@@ -40,18 +40,29 @@
      */
     protected final Mapper mapper;
     protected final JavaBeanProvider beanProvider;
+    private final Class type;
+    
     /**
      * @deprecated As of 1.3, no necessity for field anymore.
      */
     private String classAttributeIdentifier;
 
     public JavaBeanConverter(Mapper mapper) {
-        this(mapper, new BeanProvider());
+        this(mapper, (Class)null);
     }
 
+    public JavaBeanConverter(Mapper mapper, Class type) {
+        this(mapper, new BeanProvider(), type);
+    }
+
     public JavaBeanConverter(Mapper mapper, JavaBeanProvider beanProvider) {
+        this(mapper,beanProvider, null);
+    }
+
+    public JavaBeanConverter(Mapper mapper, JavaBeanProvider beanProvider, Class type) {
         this.mapper = mapper;
         this.beanProvider = beanProvider;
+        this.type = type;
     }
 
     /**
@@ -67,7 +78,7 @@
      * If you need stricter checks, subclass JavaBeanConverter
      */
     public boolean canConvert(Class type) {
-        return beanProvider.canInstantiate(type);
+        return (this.type == null || this.type==type) &&  beanProvider.canInstantiate(type);
     }
 
     public void marshal(final Object source, final HierarchicalStreamWriter writer, final MarshallingContext context) {

Modified: trunk/xstream/src/test/com/thoughtworks/acceptance/annotations/ParametrizedConverterTest.java (1970 => 1971)


--- trunk/xstream/src/test/com/thoughtworks/acceptance/annotations/ParametrizedConverterTest.java	2012-03-30 00:07:23 UTC (rev 1970)
+++ trunk/xstream/src/test/com/thoughtworks/acceptance/annotations/ParametrizedConverterTest.java	2012-03-30 00:09:53 UTC (rev 1971)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008, 2009, 2011 XStream Committers.
+ * Copyright (C) 2008, 2009, 2011, 2012 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -14,6 +14,7 @@
 import java.util.HashMap;
 
 import com.thoughtworks.acceptance.AbstractAcceptanceTest;
+import com.thoughtworks.acceptance.objects.StandardObject;
 import com.thoughtworks.xstream.XStream;
 import com.thoughtworks.xstream.annotations.XStreamAlias;
 import com.thoughtworks.xstream.annotations.XStreamConverter;
@@ -22,6 +23,7 @@
 import com.thoughtworks.xstream.converters.collections.MapConverter;
 import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;
 import com.thoughtworks.xstream.converters.extended.ToStringConverter;
+import com.thoughtworks.xstream.converters.javabean.JavaBeanConverter;
 import com.thoughtworks.xstream.mapper.Mapper;
 
 
@@ -48,6 +50,7 @@
         xstream.alias("type", Type.class);
         xstream.processAnnotations(MyMap.class);
         xstream.processAnnotations(DerivedType.class);
+        xstream.processAnnotations(SimpleBean.class);
     }
 
     public void testAnnotationForConvertersWithParameters() {
@@ -87,7 +90,7 @@
     /**
      * Tests a class-level XStreamConverter annotation subclassed from BigDecimal
      */
-    public void testCanUseCurrentTypAsParameter() {
+    public void testCanUseCurrentTypeAsParameter() {
         final Decimal value = new Decimal("5.5");
         String expected = "<decimal>5.5</decimal>";
 
@@ -147,4 +150,29 @@
         }
         
     }
+
+    public void testAnnotatedJavaBeanConverter() {
+        final SimpleBean value = new SimpleBean();
+        value.setName("joe");
+        String expected = ""
+                + "<bean>\n"
+                + "  <name>joe</name>\n"
+                + "</bean>";
+        assertBothWays(value, expected);
+    }
+    
+    
+    @XStreamAlias("bean")
+    @XStreamConverter(JavaBeanConverter.class)
+    public static class SimpleBean extends StandardObject {
+        private String myName;
+
+        public String getName() {
+            return myName;
+        }
+
+        public void setName(String name) {
+            myName = name;
+        }
+    }
 }

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


--- trunk/xstream-distribution/src/content/changes.html	2012-03-30 00:07:23 UTC (rev 1970)
+++ trunk/xstream-distribution/src/content/changes.html	2012-03-30 00:09:53 UTC (rev 1971)
@@ -51,6 +51,8 @@
     	the real object's type.</li>
     	<li>JIRA:XSTR-685: Deserialization from file or URL keeps stream open.</li>
     	<li>JIRA:XSTR-684: XML 1.0 character validation fails for characters from 0x10 to 0x1f.</li>
+    	<li>JavaBeanConverter supports now instantiation for a specific type and can therefore be used in
+    	@XStreamConverter annotation.</li>
     	<li>SerializableConverter is broken if the serialized type is the default implementation.</li>
     	<li>Method marshalUnserializableParent of SerializableConverter is protected now to skip the default mechanism
     	in a derived converter that uses the default constructor to create the original type (as an alternative for

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to