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 22. January 2005 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.mapper;
13:
14: import com.thoughtworks.xstream.alias.ClassMapper;
15:
16: import java.lang.reflect.Proxy;
17:
18: /**
19: * Mapper for handling special cases of aliasing dynamic proxies. The alias property specifies the name an instance
20: * of a dynamic proxy should be serialized with.
21: *
22: * @author Joe Walnes
23: */
24: public class DynamicProxyMapper extends MapperWrapper {
25:
26: private String alias;
27:
28: public DynamicProxyMapper(Mapper wrapped) {
29: this (wrapped, "dynamic-proxy");
30: }
31:
32: public DynamicProxyMapper(Mapper wrapped, String alias) {
33: super (wrapped);
34: this .alias = alias;
35: }
36:
37: /**
38: * @deprecated As of 1.2, use {@link #DynamicProxyMapper(Mapper)}
39: */
40: public DynamicProxyMapper(ClassMapper wrapped) {
41: this ((Mapper) wrapped);
42: }
43:
44: /**
45: * @deprecated As of 1.2, use {@link #DynamicProxyMapper(Mapper, String)}
46: */
47: public DynamicProxyMapper(ClassMapper wrapped, String alias) {
48: this ((Mapper) wrapped, alias);
49: }
50:
51: public String getAlias() {
52: return alias;
53: }
54:
55: public void setAlias(String alias) {
56: this .alias = alias;
57: }
58:
59: public String serializedClass(Class type) {
60: if (Proxy.isProxyClass(type)) {
61: return alias;
62: } else {
63: return super .serializedClass(type);
64: }
65: }
66:
67: public Class realClass(String elementName) {
68: if (elementName.equals(alias)) {
69: return DynamicProxy.class;
70: } else {
71: return super .realClass(elementName);
72: }
73: }
74:
75: /**
76: * Place holder type used for dynamic proxies.
77: */
78: public static class DynamicProxy {
79: }
80:
81: }
|