Title: [2099] trunk: New annotation XStreamAliasType to support declarative definition of XStream.aliasType() (XSTR-726).
Revision
2099
Author
joehni
Date
2013-07-12 15:22:08 -0500 (Fri, 12 Jul 2013)

Log Message

New annotation XStreamAliasType to support declarative definition of XStream.aliasType() (XSTR-726).

Modified Paths

Added Paths

Diff

Added: trunk/xstream/src/java/com/thoughtworks/xstream/annotations/XStreamAliasType.java (0 => 2099)


--- trunk/xstream/src/java/com/thoughtworks/xstream/annotations/XStreamAliasType.java	                        (rev 0)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/annotations/XStreamAliasType.java	2013-07-12 20:22:08 UTC (rev 2099)
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2013 XStream Committers.
+ * All rights reserved.
+ *
+ * Created on 12.07.2013 by Joerg Schaible
+ */
+package com.thoughtworks.xstream.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+/**
+ * Annotation used to define an XStream type alias.
+ * 
+ * @author Jörg Schaible
+ * @since upcoming
+ * @see com.thoughtworks.xstream.XStream#aliasType(String, Class)
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface XStreamAliasType {
+    /**
+     * The name of the type alias.
+     */
+    public String value();
+}
Property changes on: trunk/xstream/src/java/com/thoughtworks/xstream/annotations/XStreamAliasType.java
___________________________________________________________________

Added: svn:keywords

Added: svn:eol-style

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/mapper/AnnotationMapper.java (2098 => 2099)


--- trunk/xstream/src/java/com/thoughtworks/xstream/mapper/AnnotationMapper.java	2013-07-12 17:18:59 UTC (rev 2098)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/mapper/AnnotationMapper.java	2013-07-12 20:22:08 UTC (rev 2099)
@@ -33,6 +33,7 @@
 
 import com.thoughtworks.xstream.InitializationException;
 import com.thoughtworks.xstream.annotations.XStreamAlias;
+import com.thoughtworks.xstream.annotations.XStreamAliasType;
 import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
 import com.thoughtworks.xstream.annotations.XStreamConverter;
 import com.thoughtworks.xstream.annotations.XStreamConverters;
@@ -192,6 +193,7 @@
     
                     processConverterAnnotations(type);
                     processAliasAnnotation(type, types);
+                    processAliasTypeAnnotation(type);
     
                     if (type.isInterface()) {
                         continue;
@@ -319,20 +321,30 @@
                     + ClassAliasingMapper.class.getName()
                     + " available");
             }
+            classAliasingMapper.addClassAlias(aliasAnnotation.value(), type);
             if (aliasAnnotation.impl() != Void.class) {
                 // Alias for Interface/Class with an impl
-                classAliasingMapper.addClassAlias(aliasAnnotation.value(), type);
                 defaultImplementationsMapper.addDefaultImplementation(
                     aliasAnnotation.impl(), type);
                 if (type.isInterface()) {
                     types.add(aliasAnnotation.impl()); // alias Interface's impl
                 }
-            } else {
-                classAliasingMapper.addClassAlias(aliasAnnotation.value(), type);
             }
         }
     }
 
+    private void processAliasTypeAnnotation(final Class<?> type) {
+        final XStreamAliasType aliasAnnotation = type.getAnnotation(XStreamAliasType.class);
+        if (aliasAnnotation != null) {
+            if (classAliasingMapper == null) {
+                throw new InitializationException("No "
+                    + ClassAliasingMapper.class.getName()
+                    + " available");
+            }
+            classAliasingMapper.addTypeAlias(aliasAnnotation.value(), type);
+        }
+    }
+
     @Deprecated
     private void processImplicitCollectionAnnotation(final Class<?> type) {
         final XStreamImplicitCollection implicitColAnnotation = type

Modified: trunk/xstream/src/test/com/thoughtworks/acceptance/annotations/AliasTest.java (2098 => 2099)


--- trunk/xstream/src/test/com/thoughtworks/acceptance/annotations/AliasTest.java	2013-07-12 17:18:59 UTC (rev 2098)
+++ trunk/xstream/src/test/com/thoughtworks/acceptance/annotations/AliasTest.java	2013-07-12 20:22:08 UTC (rev 2099)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007 XStream Committers.
+ * Copyright (C) 2007, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -13,13 +13,17 @@
 import com.thoughtworks.acceptance.AbstractAcceptanceTest;
 import com.thoughtworks.xstream.XStream;
 import com.thoughtworks.xstream.annotations.XStreamAlias;
+import com.thoughtworks.xstream.annotations.XStreamAliasType;
 import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
 import com.thoughtworks.xstream.annotations.XStreamConverter;
+import com.thoughtworks.xstream.converters.ConversionException;
 import com.thoughtworks.xstream.converters.Converter;
 import com.thoughtworks.xstream.converters.MarshallingContext;
+import com.thoughtworks.xstream.converters.SingleValueConverter;
 import com.thoughtworks.xstream.converters.UnmarshallingContext;
 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+import com.thoughtworks.xstream.mapper.Mapper;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -290,4 +294,47 @@
         String xml = "<thing age=\"99\" name=\"Name\"/>";
         assertBothWays(new AgedThing("Name", 99), xml);
     }
+    
+    @XStreamAliasType("any")
+    public static abstract class Base {
+        String type = getClass().getName();
+    }
+    public static class A extends Base {
+    }
+    public static class B extends Base {
+    }
+    public static class BB extends B {
+    }
+    
+    public void testAnnotationForATypeAlias() {
+        xstream.registerConverter(new SingleValueConverter() {
+            Mapper mapper = xstream.getMapper();
+            public boolean canConvert(Class type) {
+                return Base.class.isAssignableFrom(type);
+            }
+            public String toString(Object obj) {
+                return ((Base)obj).type;
+            }
+            public Object fromString(String str) {
+                Class realClass = mapper.realClass(str);
+                try {
+                    return realClass.newInstance();
+                } catch (InstantiationException e) {
+                    throw new ConversionException(e);
+                } catch (IllegalAccessException e) {
+                    throw new ConversionException(e);
+                }
+            }
+        });
+
+        Base[] array = new Base[]{ new A(), new B(), new BB()};
+
+        String expectedXml = ""
+                + "<any-array>\n"
+                + "  <any>com.thoughtworks.acceptance.annotations.AliasTest$A</any>\n"
+                + "  <any>com.thoughtworks.acceptance.annotations.AliasTest$B</any>\n"
+                + "  <any>com.thoughtworks.acceptance.annotations.AliasTest$BB</any>\n"
+                + "</any-array>";
+        assertBothWays(array, expectedXml);
+    }
 }

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


--- trunk/xstream-distribution/src/content/changes.html	2013-07-12 17:18:59 UTC (rev 2098)
+++ trunk/xstream-distribution/src/content/changes.html	2013-07-12 20:22:08 UTC (rev 2099)
@@ -54,6 +54,7 @@
     	<li>JIRA:XSTR-728: XStream creates invalid JSON with JsonHierarchicalStreamDriver for custom converters since
     	XStream 1.4.</li>
     	<li>JIRA:XSTR-300: New EnumToStringConverter to support custom string representations of Enum values.</li>
+    	<li>JIRA:XSTR-726: New annotation XStreamAliasType to support declarative definition of XStream.aliasType().</li>
     	<li>JIRA:XSTR-735: Support for JDOM2 with JDom2Driver, JDom2Reader and JDom2Writer.</li>
     	<li>Fix: ToAttributedValueConverter silently appends fields without attribute support to the value.</li>
     </ul>

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to