1 package com.thoughtworks.xstream.core; 2 3 import com.thoughtworks.xstream.alias.CannotResolveClassException; 4 import com.thoughtworks.xstream.alias.ClassMapper; 5 6 import java.util.HashMap ; 7 import java.util.Map ; 8 import java.lang.reflect.Proxy ; 9 10 public class DefaultClassMapper implements ClassMapper { 11 12 protected Map typeToNameMap = new HashMap (); 13 protected Map nameToTypeMap = new HashMap (); 14 protected Map baseTypeToDefaultTypeMap = new HashMap (); 15 private Map lookupTypeCache = new HashMap (); 16 17 public DefaultClassMapper() { 18 baseTypeToDefaultTypeMap.put(boolean.class, Boolean .class); 20 baseTypeToDefaultTypeMap.put(char.class, Character .class); 21 baseTypeToDefaultTypeMap.put(int.class, Integer .class); 22 baseTypeToDefaultTypeMap.put(float.class, Float .class); 23 baseTypeToDefaultTypeMap.put(double.class, Double .class); 24 baseTypeToDefaultTypeMap.put(short.class, Short .class); 25 baseTypeToDefaultTypeMap.put(byte.class, Byte .class); 26 baseTypeToDefaultTypeMap.put(long.class, Long .class); 27 } 28 29 public String mapNameToXML( String javaName ) { 30 return javaName; 31 } 32 33 public String mapNameFromXML( String xmlName ) { 34 return xmlName; 35 } 36 37 public void alias(String elementName, Class type, Class defaultImplementation) { 38 nameToTypeMap.put(elementName, type.getName()); 39 typeToNameMap.put(type, elementName); 40 if (!type.equals(defaultImplementation)) { 41 typeToNameMap.put(defaultImplementation, elementName); 42 } 43 baseTypeToDefaultTypeMap.put(type, defaultImplementation); 44 } 45 46 public String lookupName(Class type) { 47 boolean isArray = type.isArray(); 48 if (type.isArray()) { 49 type = type.getComponentType(); 50 } 51 String result = (String ) typeToNameMap.get(type); 52 if (result == null && Proxy.isProxyClass(type)) { 53 result = (String ) typeToNameMap.get(DynamicProxy.class); 54 } 55 if (result == null) { 56 result = type.getName().replace('$', '-'); 58 if (result.charAt(0) == '-') { 59 result = "default" + result; 61 } 62 } 63 if (isArray) { 64 result += "-array"; 65 } 66 return result; 67 } 68 69 70 private static Class primitiveClassNamed(String name) { 71 return 72 name.equals("void") ? Void.TYPE : 73 name.equals("boolean") ? Boolean.TYPE : 74 name.equals("byte") ? Byte.TYPE : 75 name.equals("char") ? Character.TYPE : 76 name.equals("short") ? Short.TYPE : 77 name.equals("int") ? Integer.TYPE : 78 name.equals("long") ? Long.TYPE : 79 name.equals("float") ? Float.TYPE : 80 name.equals("double") ? Double.TYPE : 81 null; 82 } 83 84 public Class lookupType(String elementName) { 85 final String key = elementName; 86 if (elementName.equals("null")) { 87 return null; 88 } 89 if (lookupTypeCache.containsKey(key)) { 90 return (Class ) lookupTypeCache.get(key); 91 } 92 93 boolean isArray = elementName.endsWith("-array"); 94 95 Class primvCls = null; 96 if (isArray) { 97 elementName = elementName.substring(0, elementName.length() - 6); 99 primvCls = primitiveClassNamed(elementName); 101 } 102 103 String mappedName = null; 104 105 if (primvCls == null) { 107 mappedName = (String ) nameToTypeMap.get(mapNameFromXML(elementName)); 108 } 109 110 if (mappedName != null) { 111 elementName = mappedName; 112 } 113 114 115 elementName = elementName.replace('-', '$'); 117 if (elementName.startsWith("default$")) { 118 elementName = elementName.substring(7); 120 } 121 122 Class result; 123 124 try { 125 if (isArray) { 126 127 if (primvCls != null) { 129 result = 130 (primvCls == boolean.class) ? boolean[].class : 131 (primvCls == byte.class) ? byte[].class : 132 (primvCls == char.class) ? char[].class : 133 (primvCls == short.class) ? short[].class : 134 (primvCls == int.class) ? int[].class : 135 (primvCls == long.class) ? long[].class : 136 (primvCls == float.class) ? float[].class : 137 (primvCls == double.class) ? double[].class : 138 null; 139 140 } else { 142 result = Class.forName("[L" + elementName + ";"); 143 } 144 } else { 145 result = Class.forName(elementName); 146 } 147 } catch (ClassNotFoundException e) { 148 throw new CannotResolveClassException(elementName); 149 } 150 lookupTypeCache.put(key, result); 151 return result; 152 } 153 154 public Class lookupDefaultType(Class baseType) { 155 Class result = (Class ) baseTypeToDefaultTypeMap.get(baseType); 156 return result == null ? baseType : result; 157 } 158 159 } 160 | Popular Tags |