XStream

Add new annotation "XStreamInclude" for use by base classes to improve annotation processing when deserializing

Details

  • Type: New Feature New Feature
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 1.3.1
  • Component/s: Annotations, Core
  • Labels:
    None

Description

Hi Steve,

________________________________

From: Sparling, Steve (GE Infra, Energy)
Sent: Thursday, September 04, 2008 2:42 PM
Subject: [xstream-dev] Ability to deserialize when only base class is found during annotation processing

Hi,

I have a class which has a field as follows:-

@XStreamImplicit
public List<Animal> animals;

where Animal is defined as:-

public abstract class Animal
{
}

@XStreamAlias("Cat")
public class Cat extends Animal
{
}

@XStreamAlias("Dog")
public class Dog extends Animal
{
}

If the field is populate with Cats & Dogs, then the XML output is as expected - GREAT.

Now, the only way that I can deserialize the XML back is to explicitly process the annotations for Cat.class and Dog.class.

Would really like the ability to annotate the base class as such:-

@XStreamInclude({Cat.class, Dog.class})
public abstract class Animal
{
}

so that I don't have to explicitly deal with Cat or Dog - or any future animals that may come along in the future - all that would
be required is to add an extra class to the new annotation eg:-

@XStreamInclude({Cat.class, Dog.class, Elephant.class})
public abstract class Animal
{
}

The following is an svn diff that demonstrates the functionality:-

Index: xstream/src/java/com/thoughtworks/xstream/mapper/AnnotationMapper.java
===================================================================
— xstream/src/java/com/thoughtworks/xstream/mapper/AnnotationMapper.java (revision 1495)
+++ xstream/src/java/com/thoughtworks/xstream/mapper/AnnotationMapper.java (working copy)
@@ -18,6 +18,7 @@
import com.thoughtworks.xstream.annotations.XStreamConverters;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamImplicitCollection;
+import com.thoughtworks.xstream.annotations.XStreamInclude;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.ConverterRegistry;
@@ -465,7 +466,19 @@
if (name.startsWith("java.") || name.startsWith("java.")) { <-------- BTW: Is this correct ? return false; }

  • return annotatedTypes.contains(type) ? false : super.add(type);
    + boolean ret = annotatedTypes.contains(type) ? false : super.add(type);
    + if (ret) {
    + XStreamInclude inc = type.getAnnotation(XStreamInclude.class);
    + if (inc != null) {
    + Class<?>[] incTypes = inc.value();
    + if (incTypes != null)
    Unknown macro: { + for (Class<?> incType }

    + }
    + }
    + return ret;
    }
    }

Index: xstream/src/java/com/thoughtworks/xstream/annotations/XStreamInclude.java
===================================================================
— xstream/src/java/com/thoughtworks/xstream/annotations/XStreamInclude.java (revision 0)
+++ xstream/src/java/com/thoughtworks/xstream/annotations/XStreamInclude.java (revision 0)
@@ -0,0 +1,13 @@
+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;
+
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.TYPE)
+public @interface XStreamInclude
+{ + public Class<?>[] value(); +}

I'd appreciate you looking at this for me.
BTW: spot the BTW in the diff

Regards
Steve
________________________________

Can you please open a JIRA enhancement issue for this? It will help us to keep track of this and it looks like a useful thing ...

Thanks,
Jörg

BTW: Thanks for the BTW

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Activity

Hide
Joerg Schaible added a comment -

Applied. Thanks!

Show
Joerg Schaible added a comment - Applied. Thanks!
Hide
Joerg Schaible added a comment -

Set correct fix version.

Show
Joerg Schaible added a comment - Set correct fix version.
Hide
Joerg Schaible added a comment -

Fixed for upcoming release.

Show
Joerg Schaible added a comment - Fixed for upcoming release.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: