XStream
  1. XStream
  2. XSTR-72

Serialies inner class that access outer class variable gets null value

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.0
    • Fix Version/s: 1.1.1
    • Component/s: Core
    • Labels:
      None

      Description

      If you have an inner class like this:

      public class TodayFilterCommand implements FilterCommand {
      private final DateWithoutTimeComparator comparator = new DateWithoutTimeComparator();

      public class TodayFilterComparator implements Filter {
      public boolean matches(Object cell)

      { return comparator.compare(cell, new Date()) == 0; }

      }
      }

      In java you can only write a test that instantiates TodayFilterComparator by instantiating the outer class (e.g. TodayFilterCommand.new TodayFilterComparator) - XStream must use reflection and bypass this... in the test below we have to make "comparator" static to avoid the error, but its easy to miss this.

      public void testWeCanMarshallObjectsWithInnerClassesAndOuterVariables()

      { TodayFilterCommand todayFilterCommand = new TodayFilterCommand(); TodayFilterCommand.TodayFilterComparator todayFilterComparator = todayFilterCommand.new TodayFilterComparator(); byte[] data = marshaller.marshal(todayFilterComparator); assertNotNull("Should have marshal data", data); TodayFilterCommand.TodayFilterComparator unmarshalledFilter = (TodayFilterCommand.TodayFilterComparator) marshaller.unmarshal(data); assertNotNull("Should have returned unmarshal data", unmarshalledFilter); assertFalse(unmarshalledFilter.matches(null)); }

        People

        • Assignee:
          Unassigned
          Reporter:
          Tim Mackinnon
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: