XStream
  1. XStream
  2. XSTR-731

JavaClassConverter does not use aliasing

    Details

    • Type: Wish Wish
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Duplicate
    • Affects Version/s: 1.4.4
    • Fix Version/s: 1.4.4
    • Component/s: Converters
    • Labels:
      None
    • JDK version and platform:
      Oracle 1.7.0_17 for Windows

      Description

      JavaClassConverter does not use the class aliasing mapper. It writes and reads non-aliased class names. Example:

      public class C1 implements Serializable

      { private static final long serialVersionUID = 1L; }

      public class C2 implements Serializable {
      static private final long serialVersionUID = 1L;

      public HashMap<Class<?>, Integer> f = new HashMap<Class<?>, Integer>();

      public C2()

      { f.put(C1.class, Integer.valueOf(1)); }

      }

      public class Test {
      public static void main(String[] args) throws IOException

      { XStream xstream = new XStream(new DomDriver()); xstream.alias("a1", C1.class); xstream.alias("a2", C2.class); File file = new File("test.xml"); serialize(xstream, file); }

      public static void serialize(XStream xstream, File file) throws IOException {
      C2 c2 = new C2();
      OutputStream os = new FileOutputStream(file);

      try

      { xstream.toXML(c2, os); }

      finally

      { os.close(); }

      }
      }

      writes

      <a2>
      <f>
      <entry>
      <java-class>javaClass.C1</java-class>
      <int>1</int>
      </entry>
      </f>
      </a2>

      and not

      <a2>
      <f>
      <entry>
      <java-class>javaClass.a1</java-class>
      <int>1</int>
      </entry>
      </f>
      </a2>

      For my needs, it is sufficient to perform the following changes in version 1.4.4:

      JavaClassConverter:

      private ClassLoader classLoader;
      private ClassAliasingMapper classAliasingMapper;

      /**

      • @param classAliasingMapper may be null
        */
        public JavaClassConverter(ClassLoader classLoader, ClassAliasingMapper classAliasingMapper) { this.classLoader = classLoader; this.classAliasingMapper = classAliasingMapper; }

      public String toString(Object obj) {
      Class c = (Class) obj;
      if (classAliasingMapper == null)

      { return (c.getName()); }

      return classAliasingMapper.serializedClass(c);
      }

      public Object fromString(String str) {
      try

      { return loadClass(str); }

      catch (ClassNotFoundException e) {
      if (classAliasingMapper == null)

      { throw new ConversionException("Cannot load java class " + str, e); }

      return classAliasingMapper.realClass(str);
      }
      }

      JavaFieldConverter:

      public JavaFieldConverter(ClassLoader classLoader, ClassAliasingMapper classAliasingMapper)

      { this.javaClassConverter = new JavaClassConverter(classLoader, classAliasingMapper); }

      JavaMethodConverter:

      public JavaMethodConverter(ClassLoader classLoader, ClassAliasingMapper classAliasingMapper) { this.javaClassConverter = new JavaClassConverter(classLoader, classAliasingMapper); }

      XStream:

      registerConverter(new JavaClassConverter(classLoaderReference, classAliasingMapper), PRIORITY_NORMAL);
      registerConverter(new JavaMethodConverter(classLoaderReference, classAliasingMapper), PRIORITY_NORMAL);
      registerConverter(new JavaFieldConverter(classLoaderReference, classAliasingMapper), PRIORITY_NORMAL);

        Issue Links

          People

          • Assignee:
            Jörg Schaible
            Reporter:
            Till Wenzinger
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: