Issue Details (XML | Word | Printable)

Key: XSTR-72
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Unassigned
Reporter: Tim Mackinnon
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
XStream

Serialies inner class that access outer class variable gets null value

Created: 17/May/04 10:36 AM   Updated: 16/Jan/06 02:05 AM
Component/s: Core
Affects Version/s: 1.0
Fix Version/s: 1.1.1


 Description  « Hide
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)); }



 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Joerg Schaible added a comment - 16/Jan/06 02:05 AM
No further comments, closing.