01: /*
02: * Copyright (C) 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 31. January 2005 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.mapper;
13:
14: import com.thoughtworks.xstream.alias.ClassMapper;
15:
16: /**
17: * Mapper that uses a more meaningful alias for the field in an inner class (this$0) that refers to the outer class.
18: *
19: * @author Joe Walnes
20: */
21: public class OuterClassMapper extends MapperWrapper {
22:
23: private final String alias;
24:
25: public OuterClassMapper(Mapper wrapped) {
26: this (wrapped, "outer-class");
27: }
28:
29: public OuterClassMapper(Mapper wrapped, String alias) {
30: super (wrapped);
31: this .alias = alias;
32: }
33:
34: /**
35: * @deprecated As of 1.2, use {@link #OuterClassMapper(Mapper)}
36: */
37: public OuterClassMapper(ClassMapper wrapped) {
38: this ((Mapper) wrapped);
39: }
40:
41: /**
42: * @deprecated As of 1.2, use {@link #OuterClassMapper(Mapper, String)}
43: */
44: public OuterClassMapper(ClassMapper wrapped, String alias) {
45: this ((Mapper) wrapped, alias);
46: }
47:
48: public String serializedMember(Class type, String memberName) {
49: if (memberName.equals("this$0")) {
50: return alias;
51: } else {
52: return super .serializedMember(type, memberName);
53: }
54: }
55:
56: public String realMember(Class type, String serialized) {
57: if (serialized.equals(alias)) {
58: return "this$0";
59: } else {
60: return super.realMember(type, serialized);
61: }
62: }
63: }
|