01: /*
02: * Copyright (C) 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007, 2008 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.InitializationException;
15: import com.thoughtworks.xstream.alias.ClassMapper;
16:
17: import java.lang.reflect.Modifier;
18: import java.util.HashMap;
19: import java.util.Iterator;
20: import java.util.Map;
21:
22: /**
23: * Mapper that resolves default implementations of classes. For example,
24: * mapper.serializedClass(ArrayList.class) will return java.util.List. Calling
25: * mapper.defaultImplementationOf(List.class) will return ArrayList.
26: *
27: * @author Joe Walnes
28: */
29: public class DefaultImplementationsMapper extends MapperWrapper {
30:
31: private final Map typeToImpl = new HashMap();
32: private transient Map implToType = new HashMap();
33:
34: public DefaultImplementationsMapper(Mapper wrapped) {
35: super (wrapped);
36: addDefaults();
37: }
38:
39: /**
40: * @deprecated As of 1.2, use {@link #DefaultImplementationsMapper(Mapper)}
41: */
42: public DefaultImplementationsMapper(ClassMapper wrapped) {
43: this ((Mapper) wrapped);
44: }
45:
46: protected void addDefaults() {
47: // null handling
48: addDefaultImplementation(null, Mapper.Null.class);
49: // register primitive types
50: addDefaultImplementation(Boolean.class, boolean.class);
51: addDefaultImplementation(Character.class, char.class);
52: addDefaultImplementation(Integer.class, int.class);
53: addDefaultImplementation(Float.class, float.class);
54: addDefaultImplementation(Double.class, double.class);
55: addDefaultImplementation(Short.class, short.class);
56: addDefaultImplementation(Byte.class, byte.class);
57: addDefaultImplementation(Long.class, long.class);
58: }
59:
60: public void addDefaultImplementation(Class defaultImplementation,
61: Class ofType) {
62: if (defaultImplementation != null
63: && defaultImplementation.isInterface()) {
64: throw new InitializationException(
65: "Default implementation is not a concrete class: "
66: + defaultImplementation.getName());
67: }
68: typeToImpl.put(ofType, defaultImplementation);
69: implToType.put(defaultImplementation, ofType);
70: }
71:
72: public String serializedClass(Class type) {
73: Class baseType = (Class) implToType.get(type);
74: return baseType == null ? super .serializedClass(type) : super
75: .serializedClass(baseType);
76: }
77:
78: public Class defaultImplementationOf(Class type) {
79: if (typeToImpl.containsKey(type)) {
80: return (Class) typeToImpl.get(type);
81: } else {
82: return super .defaultImplementationOf(type);
83: }
84: }
85:
86: private Object readResolve() {
87: implToType = new HashMap();
88: for (final Iterator iter = typeToImpl.keySet().iterator(); iter
89: .hasNext();) {
90: final Object type = iter.next();
91: implToType.put(typeToImpl.get(type), type);
92: }
93: return this;
94: }
95:
96: }
|