XStream
  1. XStream
  2. XSTR-107

Allow deserialisation of objects with private constructors

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.1
    • Component/s: None
    • Labels:
      None

      Description

      I like to keep the access level of all methods/constructors down to the minimum possible, but XStream forced to put public constructors on all the objects I wanted to serialize.

      Is there any reason why getDeclaredConstructors and setAccessible isn't used instead of type.newInstance().

      The only problem that I can see is a slight speed impact.

      XStream stream = new XStream(new PureJavaReflectionProvider() {
      public Object newInstance(Class type) {
      try {
      Constructor[] constructors = type.getDeclaredConstructors();
      for(int i=0; i < constructors.length; i++) {
      if(constructors[i].getParameterTypes().length == 0) {
      if(!Modifier.isPublic(constructors[i].getModifiers()))

      { constructors[i].setAccessible(true); } return constructors[i].newInstance(new Object[0]); }

      }
      throw new ObjectAccessException("Cannot construct " + type.getName());
      } catch (InstantiationException e)

      { throw new ObjectAccessException("Cannot construct " + type.getName(), e); } catch (IllegalAccessException e) { throw new ObjectAccessException("Cannot construct " + type.getName(), e); }

      catch (InvocationTargetException e)

      { throw new ObjectAccessException("Cannot construct " + type.getName(), e); }

      }
      });

        People

        • Assignee:
          Unassigned
          Reporter:
          Daniel Sheppard
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: